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

Improvements to Button #144

Merged
merged 2 commits into from
Jan 29, 2024
Merged
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
96 changes: 73 additions & 23 deletions src/components/Button/Button.module.css
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 New Vector Ltd
Copyright 2023-2024 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -24,11 +24,22 @@ limitations under the License.
gap: var(--cpd-space-2x);
box-sizing: border-box;
font: var(--cpd-font-body-md-semibold);
transition-duration: 0.1s;
transition-property: color, background-color, border-color;
}

.button > svg {
transition: color 0.1s;
}

.button[aria-disabled="true"] {
cursor: not-allowed;
pointer-events: all !important;
color: var(--cpd-color-text-disabled) !important;
}

.button[aria-disabled="true"] > svg {
color: var(--cpd-color-icon-disabled) !important;
}

/**
Expand All @@ -47,12 +58,12 @@ limitations under the License.

.button[data-size="sm"] {
padding-block: var(--cpd-space-1x);
padding-inline: var(--cpd-space-6x);
padding-inline: var(--cpd-space-5x);
min-block-size: var(--cpd-space-9x);
}

.button[data-size="sm"].has-icon {
padding-inline-start: var(--cpd-space-5x);
padding-inline-start: var(--cpd-space-4x);
}

/**
Expand All @@ -65,6 +76,10 @@ limitations under the License.
border-width: 0;
}

.button[data-kind="primary"] > svg {
color: var(--cpd-color-icon-on-solid-primary);
}

@media (hover) {
.button[data-kind="primary"]:hover {
background: var(--cpd-color-bg-action-primary-hovered);
Expand All @@ -77,16 +92,36 @@ limitations under the License.
}

.button[data-kind="primary"][aria-disabled="true"] {
color: var(--cpd-color-text-disabled);
background: var(--cpd-color-bg-subtle-primary);
/* !important to override destructive background */
background: var(--cpd-color-bg-subtle-primary) !important;
}

.button[data-kind="primary"].destructive {
background: var(--cpd-color-bg-critical-primary);
}

@media (hover) {
.button[data-kind="primary"].destructive:hover {
background: var(--cpd-color-bg-critical-hovered);
}
}

.button[data-kind="primary"].destructive:active,
.button[data-kind="primary"].destructive[aria-expanded="true"] {
/* TODO: We're waiting for this value to be formalized as a semantic token */
background: var(--cpd-color-red-1100);
}

.button[data-kind="secondary"] {
border: 1px solid var(--cpd-color-border-interactive-primary);
border: 1px solid var(--cpd-color-border-interactive-secondary);
color: var(--cpd-color-text-primary);
background: var(--cpd-color-bg-canvas-default);
}

.button[data-kind="secondary"] > svg {
color: var(--cpd-color-icon-primary);
}

@media (hover) {
.button[data-kind="secondary"]:hover {
border-color: var(--cpd-color-border-interactive-hovered);
Expand All @@ -101,9 +136,31 @@ limitations under the License.
}

.button[data-kind="secondary"][aria-disabled="true"] {
border-color: var(--cpd-color-border-interactive-secondary);
color: var(--cpd-color-text-disabled);
background: var(--cpd-color-bg-subtle-secondary);
/* !important to override destructive values */
border-color: var(--cpd-color-border-interactive-secondary) !important;
background: var(--cpd-color-bg-subtle-secondary) !important;
}

.button[data-kind="secondary"].destructive {
border-color: var(--cpd-color-border-critical-subtle);
color: var(--cpd-color-text-critical-primary);
}

.button[data-kind="secondary"].destructive > svg {
color: var(--cpd-color-icon-critical-primary);
}

@media (hover) {
.button[data-kind="secondary"].destructive:hover {
border-color: var(--cpd-color-border-critical-hovered);
background: var(--cpd-color-bg-critical-subtle);
}
}

.button[data-kind="secondary"].destructive:active,
.button[data-kind="secondary"].destructive[aria-expanded="true"] {
border-color: var(--cpd-color-border-critical-hovered);
background: var(--cpd-color-bg-critical-subtle-hovered);
}

.button[data-kind="tertiary"] {
Expand All @@ -126,29 +183,22 @@ limitations under the License.

.button[data-kind="tertiary"][aria-disabled="true"] {
color: var(--cpd-color-text-disabled);

/* !important to override destructive background */
background: transparent !important;
}

.button[data-kind="destructive"] {
border: 1px solid var(--cpd-color-border-critical-primary);
.button[data-kind="tertiary"].destructive {
color: var(--cpd-color-text-critical-primary);
background: var(--cpd-color-bg-canvas-default);
}

@media (hover) {
.button[data-kind="destructive"]:hover {
border-color: var(--cpd-color-border-critical-hovered);
.button[data-kind="tertiary"].destructive:hover {
background: var(--cpd-color-bg-critical-subtle);
}
}

.button[data-kind="destructive"]:active,
.button[data-kind="destructive"][aria-expanded="true"] {
border-color: var(--cpd-color-border-critical-hovered);
.button[data-kind="tertiary"].destructive:active,
.button[data-kind="tertiary"].destructive[aria-expanded="true"] {
background: var(--cpd-color-bg-critical-subtle-hovered);
}

.button[data-kind="destructive"][aria-disabled="true"] {
border-color: var(--cpd-color-border-interactive-secondary);
color: var(--cpd-color-text-disabled);
background: var(--cpd-color-bg-subtle-secondary);
}
23 changes: 20 additions & 3 deletions src/components/Button/Button.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ export default {
control: { type: "inline-radio" },
},
kind: {
options: ["primary", "secondary", "tertiary", "destructive"],
options: ["primary", "secondary", "tertiary"],
control: { type: "inline-radio" },
},
destructive: { type: "boolean" },
disabled: {
type: "boolean",
},
Expand All @@ -55,6 +56,7 @@ export default {
args: {
size: "lg",
as: undefined,
destructive: false,
disabled: false,
children: "Click me!",
},
Expand Down Expand Up @@ -82,21 +84,36 @@ export const Primary = {
},
};

export const PrimaryDestructive = {
args: {
kind: "primary",
destructive: true,
},
};

export const Secondary = {
args: {
kind: "secondary",
},
};

export const SecondaryDestructive = {
args: {
kind: "secondary",
destructive: true,
},
};

export const Tertiary = {
args: {
kind: "tertiary",
},
};

export const Destructive = {
export const TertiaryDestructive = {
args: {
kind: "destructive",
kind: "tertiary",
destructive: true,
},
};

Expand Down
18 changes: 14 additions & 4 deletions src/components/Button/Button.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 New Vector Ltd
Copyright 2023-2024 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -27,9 +27,11 @@ const {
Default,
Small,
Primary,
PrimaryDestructive,
Secondary,
SecondaryDestructive,
Tertiary,
Destructive,
TertiaryDestructive,
WithIcon,
SmallWithIcon,
Disabled,
Expand All @@ -50,16 +52,24 @@ describe("Button", () => {
const { container } = render(<Primary />);
expect(container).toMatchSnapshot();
});
it("renders a Primary Destructive button", () => {
const { container } = render(<PrimaryDestructive />);
expect(container).toMatchSnapshot();
});
it("renders a Secondary button", () => {
const { container } = render(<Secondary />);
expect(container).toMatchSnapshot();
});
it("renders a Secondary Destructive button", () => {
const { container } = render(<SecondaryDestructive />);
expect(container).toMatchSnapshot();
});
it("renders a Tertiary button", () => {
const { container } = render(<Tertiary />);
expect(container).toMatchSnapshot();
});
it("renders a Destructive button", () => {
const { container } = render(<Destructive />);
it("renders a Tertiary Destructive button", () => {
const { container } = render(<TertiaryDestructive />);
expect(container).toMatchSnapshot();
});
it("renders a WithIcon button", () => {
Expand Down
21 changes: 18 additions & 3 deletions src/components/Button/Button.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2023 New Vector Ltd
Copyright 2023-2024 New Vector Ltd

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -37,8 +37,10 @@ interface ButtonComponent {
type ButtonOwnProps = PropsWithChildren<{
/**
* The type of button.
* Note: "destructive" is deprecated, please use the destructive prop in
* conjunction with another button kind.
*/
kind?: "primary" | "secondary" | "tertiary" | "destructive"; // TODO: Refine the naming
kind?: "primary" | "secondary" | "tertiary" | "destructive";
/**
* The t-shirt size of the button.
*/
Expand All @@ -47,6 +49,11 @@ type ButtonOwnProps = PropsWithChildren<{
* An icon to display within the button.
*/
Icon?: ComponentType<React.SVGAttributes<SVGElement>>;
/**
* Whether this button triggers a destructive action.
* @default false
*/
destructive?: boolean;
}>;

type ButtonPropsFor<C extends React.ElementType> = ButtonOwnProps &
Expand All @@ -61,18 +68,26 @@ export const Button = forwardRef(function Button<
>(
{
as,
kind = "primary",
kind: kindProp = "primary",
size = "lg",
children,
className,
Icon,
destructive: destructiveProp,
disabled,
...props
}: ButtonPropsFor<C> & { as?: C },
ref: ForwardedRef<C>,
): React.ReactElement {
// Fallback for the deprecated "destructive" kind
const [kind, destructive] =
kindProp === "destructive"
? ["secondary", true]
: [kindProp, destructiveProp];

const classes = classNames(styles.button, className, {
[styles["has-icon"]]: Icon,
[styles.destructive]: destructive,
});

return (
Expand Down
45 changes: 45 additions & 0 deletions src/components/Button/__snapshots__/Button.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,21 @@ exports[`Button > renders a LinkDisabled button 1`] = `
</div>
`;

exports[`Button > renders a Primary Destructive button 1`] = `
<div>
<button
aria-disabled="false"
class="_button_2a1efe _destructive_2a1efe"
data-kind="primary"
data-size="lg"
role="button"
tabindex="0"
>
Click me!
</button>
</div>
`;

exports[`Button > renders a Primary button 1`] = `
<div>
<button
Expand All @@ -92,6 +107,21 @@ exports[`Button > renders a Primary button 1`] = `
</div>
`;

exports[`Button > renders a Secondary Destructive button 1`] = `
<div>
<button
aria-disabled="false"
class="_button_2a1efe _destructive_2a1efe"
data-kind="secondary"
data-size="lg"
role="button"
tabindex="0"
>
Click me!
</button>
</div>
`;

exports[`Button > renders a Secondary button 1`] = `
<div>
<button
Expand Down Expand Up @@ -151,6 +181,21 @@ exports[`Button > renders a SmallWithIcon button 1`] = `
</div>
`;

exports[`Button > renders a Tertiary Destructive button 1`] = `
<div>
<button
aria-disabled="false"
class="_button_2a1efe _destructive_2a1efe"
data-kind="tertiary"
data-size="lg"
role="button"
tabindex="0"
>
Click me!
</button>
</div>
`;

exports[`Button > renders a Tertiary button 1`] = `
<div>
<button
Expand Down
Loading