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

WB-1635: Button - Allow styling the inner label element #2112

Merged
merged 2 commits into from
Nov 15, 2023
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
5 changes: 5 additions & 0 deletions .changeset/fluffy-beers-reply.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@khanacademy/wonder-blocks-button": minor
---

Add `labelStyle` to override the styles of the internal label
9 changes: 9 additions & 0 deletions __docs__/wonder-blocks-button/button.argtypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,15 @@ export default {
},
},
},
labelStyle: {
description: "Optional custom styles for the inner label.",
table: {
category: "Layout",
type: {
summary: "StyleType",
},
},
},
className: {
description: "Adds CSS classes to the Button.",
control: {type: "text"},
Expand Down
60 changes: 56 additions & 4 deletions __docs__/wonder-blocks-button/button.stories.tsx
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the test on line 124 is failing here because of the border/outline update. (It won't let me comment that line directly)

await expect(computedStyleButton.borderWidth).toBe("2px");

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks! I'll fix that.

Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ import Color from "@khanacademy/wonder-blocks-color";
import {View} from "@khanacademy/wonder-blocks-core";
import {Strut} from "@khanacademy/wonder-blocks-layout";
import Spacing from "@khanacademy/wonder-blocks-spacing";
import {LabelMedium, LabelLarge} from "@khanacademy/wonder-blocks-typography";
import {
LabelMedium,
LabelLarge,
HeadingSmall,
} from "@khanacademy/wonder-blocks-typography";

import Button from "@khanacademy/wonder-blocks-button";
import packageConfig from "../../packages/wonder-blocks-button/package.json";
Expand Down Expand Up @@ -71,6 +75,7 @@ export const Default: StoryComponentType = {
light: false,
disabled: false,
style: {maxWidth: 200},
labelStyle: {},
onClick: () => {
// eslint-disable-next-line no-alert
alert("Click!");
Expand Down Expand Up @@ -100,7 +105,7 @@ export const Tertiary: StoryComponentType = {

// Get HTML elements
const button = canvas.getByRole("button");
const computedStyleButton = getComputedStyle(button, ":after");
const computedStyleButton = getComputedStyle(button);
const innerLabel = canvas.getByTestId("test-button-inner-label");
const computedStyleLabel = getComputedStyle(innerLabel, ":after");

Expand All @@ -115,8 +120,10 @@ export const Tertiary: StoryComponentType = {

// Focus style
await fireEvent.focus(button);
await expect(computedStyleButton.borderColor).toBe("rgb(24, 101, 242)");
await expect(computedStyleButton.borderWidth).toBe("2px");
await expect(computedStyleButton.outlineColor).toBe(
"rgb(24, 101, 242)",
);
await expect(computedStyleButton.outlineWidth).toBe("2px");

// Active (mouse down) style
// eslint-disable-next-line testing-library/prefer-user-event
Expand Down Expand Up @@ -591,6 +598,51 @@ TruncatingLabels.parameters = {
},
};

/**
* Buttons can be styled with custom styles. This story shows a button with a
* custom width and height (using the `style` prop), and also a custom label
* style that prevents the label from being truncated (`labelStyle`).
*
* __NOTE:__ Please use this feature sparingly. This could be useful for simple
* cases like the one shown below, but it could cause some issues if used in
* more complex cases.
*/
export const CustomStyles = {
args: {
children: `This button does not truncate its label and can appear in multiple lines`,
disabled: false,
kind: "secondary",
onClick: () => {},
style: {
maxWidth: 200,
minHeight: 32,
height: "auto",
},
labelStyle: {
textOverflow: "initial",
whiteSpace: "normal",
},
},
render: (args: any) => (
Copy link
Contributor

Choose a reason for hiding this comment

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

Praise: Great time to use args!

<View style={{gap: Spacing.medium_16}}>
<HeadingSmall>Wonder Blocks theme (default)</HeadingSmall>
<View style={{flexDirection: "row", gap: Spacing.medium_16}}>
<Button {...args} kind="primary" />
<Button {...args} kind="secondary" />
<Button {...args} kind="tertiary" />
</View>
<HeadingSmall>Khanmigo theme</HeadingSmall>
<View style={{flexDirection: "row", gap: Spacing.medium_16}}>
<ThemeSwitcherContext.Provider value="khanmigo">
<Button {...args} kind="primary" />
<Button {...args} kind="secondary" />
<Button {...args} kind="tertiary" />
</ThemeSwitcherContext.Provider>
</View>
</View>
),
};

export const SubmittingForms: StoryComponentType = {
name: "Submitting forms",
render: () => (
Expand Down
Loading
Loading