Skip to content

Commit

Permalink
Merge pull request #407 from AppQuality/develop
Browse files Browse the repository at this point in the history
add color tokens
change profile modal settings
hide language selector
  • Loading branch information
iacopolea authored Sep 23, 2024
2 parents bb21023 + 8405ddc commit 57b67d8
Show file tree
Hide file tree
Showing 6 changed files with 157 additions and 29 deletions.
5 changes: 3 additions & 2 deletions src/stories/profile-modal/_types.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface UserMenuArgs extends LiHTMLAttributes<HTMLLIElement> {
languages: { [key: string]: Language };
currentLanguage: string;
logoutTitle?: string;
disableMenuLanguageSettings?: boolean;
privacy: {
title?: string;
url?: string;
Expand All @@ -50,7 +51,7 @@ export interface UserMenuArgs extends LiHTMLAttributes<HTMLLIElement> {
onLogout: () => void;
onCopyEmail?: () => void;
chatSupport?: boolean;
settingValue?: number;
settingValue?: string;
i18n?: {
settingsTitle?: string;
settingsIntroText?: string;
Expand All @@ -65,5 +66,5 @@ export interface UserMenuArgs extends LiHTMLAttributes<HTMLLIElement> {
off: string;
};
};
onSetSettings: (value: number) => void;
onSetSettings: (value: string) => void;
}
8 changes: 5 additions & 3 deletions src/stories/profile-modal/index.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ const defaultArgs: UserMenuArgs = {
off: "No",
},
},
settingValue: 0,
onSetSettings: (value: number) => {
alert(value === 1 ? "Notifications enabled" : "Notifications disabled");
settingValue: "0",
// set disableMenuLanguageSettings to false to show the language settings or omit the property
disableMenuLanguageSettings: true,
onSetSettings: (value: string) => {
alert(value === "1" ? "Notifications enabled" : "Notifications disabled");
},
};

Expand Down
13 changes: 7 additions & 6 deletions src/stories/profile-modal/items/languageMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,22 @@ interface LanguagesProps extends MenuItemProps {
currentLanguage: string;
currentLanguageLabel?: string;
onSelectLanguage: (lang: string) => void;
disabled?: boolean;
}


const StyledBody = styled.div`
margin: ${({ theme }) => theme.space.base * 6}px
${({ theme }) => theme.space.base * 4}px;
`;

const StyledButtonContainer = styled.div`
${flexStart}
& button {
& button {
justify-content: flex-start;
}
`;

export const LanguageItem = (props: LanguagesProps) => {

const content = (
<>
<PreviousButton onClick={() => props.setActive("")} isBasic>
Expand All @@ -55,15 +54,17 @@ export const LanguageItem = (props: LanguagesProps) => {
<Button.StartIcon>
{props.currentLanguage === key ? <CheckIcon /> : <EmptyIcon />}
</Button.StartIcon>
<Span isBold={props.currentLanguage === key}>{props.languages[key].label}</Span>
<Span isBold={props.currentLanguage === key}>
{props.languages[key].label}
</Span>
</Button>
))}
</StyledButtonContainer>
</StyledBody>
</>
);

return (
return props.disabled ? null : (
<>
<MenuItem
content={content}
Expand All @@ -76,7 +77,7 @@ export const LanguageItem = (props: LanguagesProps) => {
{props.title}
<MD style={{ color: theme.palette.grey[600] }}>
{props.currentLanguageLabel ?? "Now:"}
{ " " + props.languages[props.currentLanguage].label}
{" " + props.languages[props.currentLanguage].label}
</MD>
</MenuItemBody>
</MenuItem>
Expand Down
14 changes: 7 additions & 7 deletions src/stories/profile-modal/items/settingsMenuItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ import { useState } from "react";
import { MD, SM } from "../../typography/typescale";

interface SettingsProps extends MenuItemProps {
settingValue?: number;
settingValue?: string;

onSetSettings?: (value: number) => void;
onSetSettings?: (value: string) => void;
i18n?: {
settingsTitle?: string;
settingsIntroText?: string;
Expand Down Expand Up @@ -53,9 +53,9 @@ const SettingsOutroText = styled(Paragraph)`
`;

export const SettingsItem = (props: SettingsProps) => {
const [value, setValue] = useState(props.settingValue || 0);
const [value, setValue] = useState(props.settingValue || "0");

const onToggleSettings = (value: number) => {
const onToggleSettings = (value: string) => {
setValue(value);
props.onSetSettings && props.onSetSettings(value);
};
Expand All @@ -77,11 +77,11 @@ export const SettingsItem = (props: SettingsProps) => {
{props.i18n?.settingsToggle?.title}
</TriggerTitle>
<Toggle
checked={value === 1}
onChange={() => onToggleSettings(value === 1 ? 0 : 1)}
checked={value === "1"}
onChange={() => onToggleSettings(value === "1" ? "0" : "1")}
>
<Label>
{value === 1
{value === "1"
? props.i18n?.settingsToggle?.on
: props.i18n?.settingsToggle?.off}
</Label>
Expand Down
29 changes: 18 additions & 11 deletions src/stories/profile-modal/userMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ const StyledList = styled.ul`
export const UserMenu = (props: UserMenuArgs) => {
const [item, setActiveItem] = useState("");

const disableMenuLanguageSettings =
props.disableMenuLanguageSettings || false;

const toggleItem = (item: string) => {
setActiveItem(item);
};
Expand Down Expand Up @@ -70,23 +73,26 @@ export const UserMenu = (props: UserMenuArgs) => {
title={props.i18n?.settingsTitle || "Notifications Settings"}
i18n={{
settingsTitle:
props.i18n?.settingsTitle || "Notifications Settings",
props.i18n?.settingsTitle || "Notifications Settings",
settingsIntroText: props.i18n?.settingsIntroText,
settingsOutroText: props.i18n?.settingsOutroText,
settingsToggle: props.i18n?.settingsToggle,
}}
onSetSettings={props.onSetSettings}
/>
<LanguageItem
title={props.languageTitle || "Change Language"}
value={"language-selector"}
selectedItem={item}
setActive={(i) => toggleItem(i)}
languages={props.languages}
currentLanguage={props.currentLanguage}
currentLanguageLabel={props.currentLanguageLabel}
onSelectLanguage={(lang) => props.onSelectLanguage(lang)}
/>
{
<LanguageItem
disabled={disableMenuLanguageSettings}
title={props.languageTitle || "Change Language"}
value={"language-selector"}
selectedItem={item}
setActive={(i) => toggleItem(i)}
languages={props.languages}
currentLanguage={props.currentLanguage}
currentLanguageLabel={props.currentLanguageLabel}
onSelectLanguage={(lang) => props.onSelectLanguage(lang)}
/>
}
<MenuItem
selectedItem={item}
icon={<LockIcon color={theme.palette.blue[600]} />}
Expand All @@ -104,6 +110,7 @@ export const UserMenu = (props: UserMenuArgs) => {
>
{props.privacy?.title || "Privacy settings"}
</MenuItem>

<MenuItem
selectedItem={item}
icon={<ExitIcon color={theme.palette.red[600]} />}
Expand Down
117 changes: 117 additions & 0 deletions src/stories/theme/colors.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import { Story } from "@storybook/react";
import { MD } from "@zendeskgarden/react-typography";
import { HTMLAttributes } from "react";
import { styled } from "styled-components";
import { theme } from ".";

const ColorsLayout = styled.div`
display: grid;
grid-template-columns: repeat(2, 1fr);
grid-gap: ${(p) => p.theme.space.xl} ${(p) => p.theme.space.lg};
max-width: 768px;
`;

const Ul = styled.ul`
border-radius: ${(p) => p.theme.borderRadii.lg};
overflow: hidden;
height: fit-content;
`;
const Li = styled.li<{ hex: string }>`
border: 1px solid #00000045;
background-color: ${({ hex }) => hex};
color: ${({ hex }) => getTextColor(hex)};
padding: ${(p) => p.theme.space.sm};
`;

const ColorSpec = styled(MD)<{ color: string }>`
color: ${({ color }) => color};
display: flex;
justify-content: space-between;
align-items: center;
`;

interface ColorProps extends HTMLAttributes<HTMLDivElement> {}

const isHex = (color: string) => {
return color.startsWith("#") && (color.length === 7 || color.length === 4);
};

const getTextColor = (hex: string) => {
if (hex.indexOf("#") === 0) {
hex = hex.slice(1);
}
// convert 3-digit hex to 6-digits.
if (hex.length === 3) {
hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
}
if (hex.length !== 6) {
throw new Error("Invalid HEX color.");
}
var r = parseInt(hex.slice(0, 2), 16),
g = parseInt(hex.slice(2, 4), 16),
b = parseInt(hex.slice(4, 6), 16);

// https://stackoverflow.com/a/3943023/112731
return r * 0.299 + g * 0.587 + b * 0.114 > 186 ? "#000000" : "#FFFFFF";
};

const getPaletteColor = (color: string) => {
if (color.startsWith("#")) {
return color;
}
if (color in theme.palette) {
const c = theme.palette[color as keyof typeof theme.palette];
if (typeof c !== "string" && "600" in c) return c[600];
}
return null;
};

const Template: Story<ColorProps> = (props) => {
return (
<div>
<ColorsLayout>
{Object.entries(theme.colors).map(([name, value], i) => {
let hex = value;
let textName = value;
if (!isHex(value)) {
const paletteColor = getPaletteColor(value);
if (!paletteColor) return null;
hex = paletteColor;
textName = `${value}600 - ${hex.toUpperCase()}`;
}
if (!hex) return null;
return (
<>
<Ul key={i}>
<Li hex={hex}>
<ColorSpec color={getTextColor(hex)}>
<span>{name}</span>
<span>{textName}</span>
</ColorSpec>
</Li>
</Ul>
</>
);
})}
</ColorsLayout>
</div>
);
};

export const Default = Template.bind({});
Default.args = {};

export default {
title: "Theme/Colors",
argTypes: {
cards: {
table: {
disable: true,
},
},
},
parameters: {
// Sets a delay for the component's stories
chromatic: { delay: 300 },
},
};

0 comments on commit 57b67d8

Please sign in to comment.