Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Migrate Button component to semantic color tokens #1881

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 105 additions & 41 deletions packages/circuit-ui/components/Button/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export interface BaseProps {
*/
'disabled'?: boolean;
/**
* Change the color from blue to red to signal to the user that the action
* Change the color from accent to danger to signal to the user that the action
* is irreversible or otherwise dangerous.
*/
'destructive'?: boolean;
Expand Down Expand Up @@ -157,34 +157,93 @@ const Content = styled.span<{ isLoading: boolean }>(
contentLoadingStyles,
);

const COLOR_MAP = {
const PRIMARY_COLOR_MAP = {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we're using CSS variables, I'd refactor this code to move away from this object and replace it with a switch statement and explicit styles. Would help readability I think.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea! Just replaced the tokens for now, but you're right that this is a good opportunity to make this a bit clearer. Will address (in January)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was contemplating this again and realized it's actually a matrix between variant and destructive. Would keep it for now and refactor with the new button design.

default: {
default: 'p500',
hover: 'p700',
active: 'p900',
bg: {
idle: 'var(--cui-bg-accent-strong)',
hovered: 'var(--cui-bg-accent-strong-hovered)',
pressed: 'var(--cui-bg-accent-strong-pressed)',
},
fg: {
idle: 'var(--cui-fg-on-strong)',
hovered: 'var(--cui-fg-on-strong-hovered)',
pressed: 'var(--cui-fg-on-strong-pressed)',
},
border: {
idle: 'var(--cui-border-accent)',
hovered: 'var(--cui-border-accent-hovered)',
pressed: 'var(--cui-border-accent-pressed)',
},
},
destructive: {
default: 'alert',
hover: 'r700',
active: 'r900',
bg: {
idle: 'var(--cui-bg-danger-strong)',
hovered: 'var(--cui-bg-danger-strong-hovered)',
pressed: 'var(--cui-bg-danger-strong-pressed)',
},
fg: {
idle: 'var(--cui-fg-on-strong)',
hovered: 'var(--cui-fg-on-strong-hovered)',
pressed: 'var(--cui-fg-on-strong-pressed)',
},
border: {
idle: 'var(--cui-border-danger)',
hovered: 'var(--cui-border-danger-hovered)',
pressed: 'var(--cui-border-danger-pressed)',
},
},
} as const;

const SECONDARY_COLOR_MAP = {
default: {
text: 'black',
default: 'n500',
hover: 'n700',
active: 'n800',
bg: {
idle: 'var(--cui-bg-normal)',
hovered: 'var(--cui-bg-normal-hovered)',
pressed: 'var(--cui-bg-normal-pressed)',
},
fg: {
idle: 'var(--cui-fg-normal)',
hovered: 'var(--cui-fg-normal-hovered)',
pressed: 'var(--cui-fg-normal-pressed)',
},
border: {
idle: 'var(--cui-border-normal)',
hovered: 'var(--cui-border-normal-hovered)',
pressed: 'var(--cui-border-normal-pressed)',
},
},
destructive: {
text: 'alert',
default: 'alert',
hover: 'r700',
active: 'r900',
bg: {
idle: 'var(--cui-bg-normal)',
hovered: 'var(--cui-bg-normal-hovered)',
pressed: 'var(--cui-bg-normal-pressed)',
},
fg: {
idle: 'var(--cui-fg-danger)',
hovered: 'var(--cui-fg-danger-hovered)',
pressed: 'var(--cui-fg-danger-pressed)',
},
border: {
idle: 'var(--cui-border-danger)',
hovered: 'var(--cui-border-danger-hovered)',
pressed: 'var(--cui-border-danger-pressed)',
},
},
} as const;

const TERTIARY_COLOR_MAP = {
default: {
idle: 'var(--cui-fg-accent)',
hovered: 'var(--cui-fg-accent-hovered)',
pressed: 'var(--cui-fg-accent-pressed)',
},
destructive: {
idle: 'var(--cui-fg-danger)',
hovered: 'var(--cui-fg-danger-hovered)',
pressed: 'var(--cui-fg-danger-pressed)',
},
};

type StyledButtonProps = Pick<
ButtonProps,
'variant' | 'destructive' | 'size' | 'stretch'
Expand Down Expand Up @@ -215,40 +274,42 @@ const baseStyles = ({ theme }: StyleProps) => css`
`;

const primaryStyles = ({
theme,
variant = 'secondary',
destructive,
}: StyledButtonProps & StyleProps) => {
}: StyledButtonProps) => {
if (variant !== 'primary') {
return null;
}

const colors = destructive ? COLOR_MAP.destructive : COLOR_MAP.default;
const colors = destructive
? PRIMARY_COLOR_MAP.destructive
: PRIMARY_COLOR_MAP.default;

return css`
background-color: ${theme.colors[colors.default]};
border-color: ${theme.colors[colors.default]};
color: ${theme.colors.white};
background-color: ${colors.bg.idle};
border-color: ${colors.border.idle};
color: ${colors.fg.idle};

&:hover {
background-color: ${theme.colors[colors.hover]};
border-color: ${theme.colors[colors.hover]};
background-color: ${colors.bg.hovered};
border-color: ${colors.border.hovered};
color: ${colors.fg.hovered};
}

&:active,
&[aria-expanded='true'],
&[aria-pressed='true'] {
background-color: ${theme.colors[colors.active]};
border-color: ${theme.colors[colors.active]};
background-color: ${colors.bg.pressed};
border-color: ${colors.border.pressed};
color: ${colors.fg.pressed};
}
`;
};

export const secondaryStyles = ({
theme,
variant = 'secondary',
destructive,
}: StyledButtonProps & StyleProps) => {
}: StyledButtonProps) => {
if (variant !== 'secondary') {
return null;
}
Expand All @@ -258,52 +319,55 @@ export const secondaryStyles = ({
: SECONDARY_COLOR_MAP.default;

return css`
background-color: ${theme.colors.white};
border-color: ${theme.colors[colors.default]};
color: ${theme.colors[colors.text]};
background-color: ${colors.bg.idle};
border-color: ${colors.border.idle};
color: ${colors.fg.idle};

&:hover {
background-color: ${theme.colors.n100};
border-color: ${theme.colors[colors.hover]};
background-color: ${colors.bg.hovered};
border-color: ${colors.border.hovered};
color: ${colors.fg.hovered};
}

&:active,
&[aria-expanded='true'],
&[aria-pressed='true'] {
background-color: ${theme.colors.n200};
border-color: ${theme.colors[colors.active]};
background-color: ${colors.bg.pressed};
border-color: ${colors.border.pressed};
color: ${colors.fg.pressed};
}
`;
};

export const tertiaryStyles = ({
theme,
variant = 'secondary',
destructive,
}: StyledButtonProps & StyleProps) => {
}: StyledButtonProps) => {
if (variant !== 'tertiary') {
return null;
}

const colors = destructive ? COLOR_MAP.destructive : COLOR_MAP.default;
const colors = destructive
? TERTIARY_COLOR_MAP.destructive
: TERTIARY_COLOR_MAP.default;

return css`
background-color: transparent;
border-color: transparent;
color: ${theme.colors[colors.default]};
color: ${colors.idle};
padding-left: 0;
padding-right: 0;

&:hover {
color: ${theme.colors[colors.hover]};
color: ${colors.hovered};
background-color: transparent;
border-color: transparent;
}

&:active,
&[aria-expanded='true'],
&[aria-pressed='true'] {
color: ${theme.colors[colors.active]};
color: ${colors.pressed};
background-color: transparent;
border-color: transparent;
}
Expand Down
Loading