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-1853] Button: Refactor theme to support semanticColor tokens #2431

Merged
merged 13 commits into from
Jan 29, 2025

Conversation

jandrade
Copy link
Member

Summary:

Now that we have semanticColor tokens available, this PR is responsible for
refactoring the existing Button code to use these tokens instead of the
color primitive tokens. Also, it modifies the theme structure to follow
closer the semantic color tokens structure.

Also added a light.destructive.disabled variant to the Dark story as we didn't
include a snapshot for it.

Issue: WB-1853

Test plan:

Navigate to /?path=/docs/packages-button--docs and verify that the button
hasn't changed.

@jandrade jandrade self-assigned this Jan 16, 2025
Copy link

changeset-bot bot commented Jan 16, 2025

🦋 Changeset detected

Latest commit: 1b0bc80

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@khanacademy/wonder-blocks-button Patch
@khanacademy/wonder-blocks-banner Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@jandrade jandrade changed the title [WB-1853] [WB-1853] Button: Refactor theme to support semanticColor tokens Jan 16, 2025
Copy link
Contributor

github-actions bot commented Jan 16, 2025

A new build was pushed to Chromatic! 🚀

https://5e1bf4b385e3fb0020b7073c-buxylhokuj.chromatic.com/

Chromatic results:

Metric Total
Captured snapshots 378
Tests with visual changes 4
Total stories 520
Inherited (not captured) snapshots [TurboSnap] 0
Tests on the build 378

Copy link
Contributor

github-actions bot commented Jan 16, 2025

Size Change: +193 B (+0.2%)

Total Size: 98.2 kB

Filename Size Change
packages/wonder-blocks-button/dist/es/index.js 4.31 kB +193 B (+4.69%) 🔍
ℹ️ View Unchanged
Filename Size
packages/wonder-blocks-accordion/dist/es/index.js 3.77 kB
packages/wonder-blocks-banner/dist/es/index.js 1.53 kB
packages/wonder-blocks-birthday-picker/dist/es/index.js 1.77 kB
packages/wonder-blocks-breadcrumbs/dist/es/index.js 887 B
packages/wonder-blocks-cell/dist/es/index.js 2.01 kB
packages/wonder-blocks-clickable/dist/es/index.js 3.06 kB
packages/wonder-blocks-core/dist/es/index.js 2.9 kB
packages/wonder-blocks-data/dist/es/index.js 6.24 kB
packages/wonder-blocks-dropdown/dist/es/index.js 18.9 kB
packages/wonder-blocks-form/dist/es/index.js 6.05 kB
packages/wonder-blocks-grid/dist/es/index.js 1.36 kB
packages/wonder-blocks-icon-button/dist/es/index.js 2.95 kB
packages/wonder-blocks-icon/dist/es/index.js 871 B
packages/wonder-blocks-labeled-field/dist/es/index.js 1.84 kB
packages/wonder-blocks-layout/dist/es/index.js 1.82 kB
packages/wonder-blocks-link/dist/es/index.js 2.28 kB
packages/wonder-blocks-modal/dist/es/index.js 5.42 kB
packages/wonder-blocks-pill/dist/es/index.js 1.65 kB
packages/wonder-blocks-popover/dist/es/index.js 4.88 kB
packages/wonder-blocks-progress-spinner/dist/es/index.js 1.52 kB
packages/wonder-blocks-search-field/dist/es/index.js 1.34 kB
packages/wonder-blocks-switch/dist/es/index.js 1.92 kB
packages/wonder-blocks-testing-core/dist/es/index.js 3.74 kB
packages/wonder-blocks-testing/dist/es/index.js 1.07 kB
packages/wonder-blocks-theming/dist/es/index.js 693 B
packages/wonder-blocks-timing/dist/es/index.js 1.8 kB
packages/wonder-blocks-tokens/dist/es/index.js 2.53 kB
packages/wonder-blocks-toolbar/dist/es/index.js 905 B
packages/wonder-blocks-tooltip/dist/es/index.js 6.99 kB
packages/wonder-blocks-typography/dist/es/index.js 1.23 kB

compressed-size-action

@@ -228,50 +228,4 @@ describe("button with icon", () => {
expect(icon).toBeInTheDocument();
expect(icon).toHaveAttribute("aria-hidden", "true");
});

Copy link
Member Author

Choose a reason for hiding this comment

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

note: These tests no longer apply as tertiary buttons don't need any changes for the Khanmigo theme.

@@ -182,7 +182,7 @@ const ButtonCore: React.ForwardRefExoticComponent<
sharedStyles.endIconWrapperTertiary,
(focused || hovered) &&
kind !== "primary" &&
sharedStyles.iconWrapperSecondaryHovered,
buttonStyles.iconWrapperHovered,
Copy link
Member Author

Choose a reason for hiding this comment

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

I've moved this to the function where we generate style colors dynamically to be able to support default (progressive) and destructive colors.

Comment on lines 354 to 361
const colorToAction = light
? buttonColor === "destructive"
? "destructiveLight"
: "progressiveLight"
: buttonColor === "destructive"
? "destructive"
: "progressive";
const themeColorAction = theme.color[colorToAction];
Copy link
Member Author

Choose a reason for hiding this comment

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

note: Now that the theme tries to cover all the different aspects of the button states (hover/focus, press, disabled, light), I'm simplifying the logic a bit here to avoid using ternary operators when defining the CSS properties below.

e.g.
Instead of: bg: light ? theme.color.light.background : somethingActionEtc.background

We now use bg: themeColorAction.default.background.

paddingRight: padding,
background: themeColorAction.default.background,
color: themeColorAction.default.foreground,
paddingInline: padding,
Copy link
Member Author

Choose a reason for hiding this comment

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

note: Switching to Inline here for better RTL support (it shouldn't affect the current styles at all).

Comment on lines +434 to +431
background: themeColorAction.default.background,
color: themeColorAction.default.foreground,
borderColor: themeColorAction.default.border,
borderStyle: "solid",
borderWidth: theme.border.width.secondary,
paddingLeft: padding,
paddingRight: padding,
paddingInline: padding,
Copy link
Member Author

Choose a reason for hiding this comment

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

note: These are the main changes of the secondary button (resting state). I don't understand why Chromatic is detecting so many changes with the secondary variant.

Comment on lines 512 to 500
":focus-visible": {
outlineColor: themeColorAction.disabled.foreground,
outlineStyle: "solid",
outlineWidth: theme.border.width.disabled,
},
Copy link
Member Author

Choose a reason for hiding this comment

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

note: Adding CSS pseudo-state here.

Base automatically changed from wb-semantic-color-refactor to main January 21, 2025 23:23
@jandrade jandrade marked this pull request as ready for review January 22, 2025 14:59
@khan-actions-bot khan-actions-bot requested a review from a team January 22, 2025 14:59
@khan-actions-bot
Copy link
Contributor

Gerald

Required Reviewers
  • @Khan/wonder-blocks for changes to .changeset/many-apes-play.md, __docs__/wonder-blocks-button/button-variants.stories.tsx, __docs__/wonder-blocks-button/button.stories.tsx, packages/wonder-blocks-button/src/components/button-core.tsx, packages/wonder-blocks-button/src/themes/default.ts, packages/wonder-blocks-button/src/themes/khanmigo.ts, packages/wonder-blocks-button/src/components/__tests__/button-with-icon.test.tsx

Don't want to be involved in this pull request? Comment #removeme and we won't notify you of further changes.

Copy link
Contributor

github-actions bot commented Jan 22, 2025

npm Snapshot: Published

🎉 Good news!! We've packaged up the latest commit from this PR (2bfd6c9) and published all packages with changesets to npm.

You can install the packages in webapp by running:

./services/static/dev/tools/deploy_wonder_blocks.js --tag="PR2431"

Packages can also be installed manually by running:

yarn add @khanacademy/wonder-blocks-<package-name>@PR2431

Copy link
Member Author

Choose a reason for hiding this comment

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

note: I've changed the color tokens structure here to follow a similar approach to the semanticColor actions. Also added a disabled category to every variant b/c Button has particular disabled states (I hope we could really simplify this in the future).

Copy link
Member

@beaesguerra beaesguerra left a comment

Choose a reason for hiding this comment

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

I left some questions around the semantic token usage! I'm still wrapping my head around it + themes so I'll reach out as well!

I also left a comment in Chromatic around border color changes in light mode, wanted to confirm it was intended!

const focusStyling = {
outlineColor: light ? theme.color.bg.primary.default : color,
outlineColor: themeColorAction.default.background,
Copy link
Member

Choose a reason for hiding this comment

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

Since this is the focus outline, should this be themeColorAction.default.border instead of background?

background: light
? theme.color.bg.secondary.inverse
: theme.color.bg.secondary.focus,
background: themeColorAction.hover.background,
borderColor: "transparent",
Copy link
Member

Choose a reason for hiding this comment

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

Should the border color use a token as well? This is an example where there is a outline and border are different colors!

Copy link
Member Author

Choose a reason for hiding this comment

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

I've removed that token as I noticed it was not needed for this case.

Comment on lines 447 to 448
borderColor: themeColorAction.disabled.foreground,
outlineColor: themeColorAction.disabled.foreground,
Copy link
Member

Choose a reason for hiding this comment

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

Should these use the border token instead of foreground?

cursor: "default",
":focus-visible": {
outlineColor: themeColorAction.disabled.foreground,
Copy link
Member

Choose a reason for hiding this comment

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

Should this be border instead of foreground?

@@ -510,13 +490,18 @@ export const _generateStyles = (
focused: focusStyling,
pressed: activePressedStyling,
disabled: {
color: light ? fadedColor : theme.color.text.disabled,
// NOTE: This is an special case to handle the light color
color: themeColorAction.disabled.border,
Copy link
Member

Choose a reason for hiding this comment

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

Is the special case that we use the border token for the text color? For special cases, I wonder if we could avoid one-offs like this by using the primitive token instead? That way, it's relying on the color instead of the semantic meaning. What do you think!

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice catch! It was a mistake from my side. I'm now using the correct token.

progressive: {
// filled
...semanticColor.action.filled.progressive,
disabled: {
Copy link
Member

Choose a reason for hiding this comment

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

Would it be helpful to have the disabled object as part of the semantic color tokens? (I might have asked this when we were pairing but I can't remember 😅)

Copy link
Member Author

Choose a reason for hiding this comment

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

This disabled version is very particular to buttons only, so that's the reason why I kept it separately. Once we migrate to the new theme and it becomes default, then we should be able to move this tokens to the global semantic ones.

@khan-actions-bot khan-actions-bot requested a review from a team January 27, 2025 15:19
@jandrade jandrade requested a review from beaesguerra January 27, 2025 15:39
Copy link
Member

@beaesguerra beaesguerra left a comment

Choose a reason for hiding this comment

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

Code changes look good! I left some comments in Chromatic to confirm some of the update styling for states!

@khan-actions-bot khan-actions-bot requested a review from a team January 29, 2025 15:27
Copy link
Member

@beaesguerra beaesguerra left a comment

Choose a reason for hiding this comment

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

Looks good! 🎉

@jandrade jandrade merged commit c089d35 into main Jan 29, 2025
14 checks passed
@jandrade jandrade deleted the wb-button-semantic branch January 29, 2025 21:03
Copy link

codecov bot commented Jan 29, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (a00747f) to head (1b0bc80).
Report is 7 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

@@     Coverage Diff      @@
##   main   #2431   +/-   ##
============================
============================

Continue to review full report in Codecov by Sentry.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a00747f...1b0bc80. Read the comment docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants