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-1814.5] Refactor IconButton to use semantic colors #2449

Merged
merged 5 commits into from
Jan 31, 2025

Conversation

jandrade
Copy link
Member

@jandrade jandrade commented Jan 30, 2025

Summary:

Next step is to refactor the IconButton component to use semantic colors.

Besides the migration, this PR also includes the following changes:

  • Reworked the theme structure to make it closer to the semanticColor structure.
  • Moved all the color-related CSS properties to a separate function (getStylesByKind).
  • Added border to all the states to prepare the work for Polaris as these
    icon buttons will look similar to the Button ones.

Implementation plan:

  1. [WB-1814.1] Refactor Checkbox and Radio to use semantic colors #2439
  2. [WB-1814.2] Refactor TextField and TextArea to use semantic colors #2440
  3. [WB-1814.3] Refactor Switch to use semantic colors #2441
  4. [WB-1814.4] Refactor Accordion, Banner, BirthdayPicker to use semantic colors #2446
  5. IconButton (current PR)
  6. Clickable, Link
  7. Modal
  8. Popover, Tooltip
  9. Pill, Toolbar

Issue: WB-1814

Test plan:

Verify that the Chromatic snapshots are unchanged.

URL: /?path=/story/packages-iconbutton-all-variants--default

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

changeset-bot bot commented Jan 30, 2025

🦋 Changeset detected

Latest commit: e7b02f3

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

This PR includes changesets to release 8 packages
Name Type
@khanacademy/wonder-blocks-icon-button Patch
@khanacademy/wonder-blocks-banner Patch
@khanacademy/wonder-blocks-modal Patch
@khanacademy/wonder-blocks-popover Patch
@khanacademy/wonder-blocks-search-field Patch
@khanacademy/wonder-blocks-dropdown Patch
@khanacademy/wonder-blocks-tooltip Patch
@khanacademy/wonder-blocks-birthday-picker 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

Copy link
Contributor

github-actions bot commented Jan 30, 2025

Size Change: +169 B (+0.17%)

Total Size: 98.5 kB

Filename Size Change
packages/wonder-blocks-icon-button/dist/es/index.js 3.12 kB +169 B (+5.73%) 🔍
ℹ️ View Unchanged
Filename Size
packages/wonder-blocks-accordion/dist/es/index.js 3.75 kB
packages/wonder-blocks-banner/dist/es/index.js 1.55 kB
packages/wonder-blocks-birthday-picker/dist/es/index.js 1.8 kB
packages/wonder-blocks-breadcrumbs/dist/es/index.js 887 B
packages/wonder-blocks-button/dist/es/index.js 4.31 kB
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 5.99 kB
packages/wonder-blocks-grid/dist/es/index.js 1.36 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.85 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 2 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.54 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

Copy link
Contributor

github-actions bot commented Jan 30, 2025

A new build was pushed to Chromatic! 🚀

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

Chromatic results:

Metric Total
Captured snapshots 54
Tests with visual changes 0
Total stories 535
Inherited (not captured) snapshots [TurboSnap] 0
Tests on the build 380

Comment on lines +220 to +224
buttonColor: ButtonColor,
disabled: boolean,
kind: Kind,
light: boolean,
buttonColor: string,
theme: IconButtonThemeContract,
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: Just reordered the arguments alphabetically.

) {
switch (kind) {
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: Changed from switch to if as constants cannot be redefined within switch statements. I did this as I wanted to be able to reuse const themeVariant etc for better readability.

}

const _generateStyles = (
buttonColor = "default",
kind: "primary" | "secondary" | "tertiary",
buttonColor: ButtonColor = "default",
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: Same here related to reordering args alphabetically.

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: Modified the structure to make it really closer to semanticColor.action. Note that this time used primary, secondary, etc instead of filled, outlinedto be able to map the keys to the props defined inIconButton(e.g.kind="primary"`). Still undecided with which approach to take. Any thoughts are completely welcome!

@jandrade jandrade marked this pull request as ready for review January 30, 2025 21:54
@khan-actions-bot
Copy link
Contributor

khan-actions-bot commented Jan 30, 2025

Gerald

Required Reviewers
  • @Khan/wonder-blocks for changes to .changeset/sixty-jars-guess.md, __docs__/wonder-blocks-icon-button/icon-button-variants.stories.tsx, __docs__/wonder-blocks-icon-button/icon-button.stories.tsx, packages/wonder-blocks-icon-button/src/components/icon-button-core.tsx, packages/wonder-blocks-icon-button/src/themes/default.ts, packages/wonder-blocks-icon-button/src/themes/khanmigo.ts

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

@khan-actions-bot khan-actions-bot requested a review from a team January 30, 2025 21:55
Copy link
Contributor

github-actions bot commented Jan 30, 2025

npm Snapshot: Published

🎉 Good news!! We've packaged up the latest commit from this PR (7ed03e7) 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="PR2449"

Packages can also be installed manually by running:

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

Copy link
Member

@marcysutton marcysutton left a comment

Choose a reason for hiding this comment

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

This looks like a really robust overhaul! I left some questions on things I wasn't understanding 100%. But I think it's going to be way easier to theme with this in place. Nice work!

autodocs: false,
},
},
tags: ["!autodocs"],
Copy link
Member

@marcysutton marcysutton Jan 31, 2025

Choose a reason for hiding this comment

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

praise: I didn't know you could do this! Nice!

@@ -101,6 +104,7 @@ const IconButtonCore: React.ForwardRefExoticComponent<
const renderInner = (router: any): React.ReactNode => {
const buttonStyles = _generateStyles(
color,
!!disabled,
Copy link
Member

Choose a reason for hiding this comment

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

question: Is the boolean coercion of disabled meant to handle an undefined case?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's correct, this is mostly required b/c that prop is optional.

*/
function getActionType(buttonColor: ButtonColor, disabled: boolean) {
const actionType =
buttonColor === "destructive" ? "destructive" : "progressive";
Copy link
Member

Choose a reason for hiding this comment

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

question: could you use nullish coalescing here? (out of curiosity)

const actionType = buttonColor === "destructive" ?? "progressive";

Copy link
Member Author

Choose a reason for hiding this comment

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

In this case it wouldn't work because the left-hand operand would return true if the condition is fulfilled.

const actionType = buttonColor === "destructive" ?? "progressive";
// when `buttonColor=destructive` => actionType=true
// when `buttonColor=default` => actionType="progressive"

Apologies if I'm being too literal 😅, but maybe I'm missing something in the expression (this could still use nullish coalescing here, which would be cool).

* will apply the same styles as Button.
*/
const baseColorStates = {
...semanticColor.action.outlined,
Copy link
Member

Choose a reason for hiding this comment

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

question: what case does this spread token cover? Could you add a comment for it?

Copy link
Member Author

Choose a reason for hiding this comment

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

Good point! I'll add it.

@@ -18,6 +18,9 @@ import {
IconButtonThemeContract,
} from "../themes/themed-icon-button";

type Kind = "primary" | "secondary" | "tertiary";
type ButtonColor = "default" | "destructive";
Copy link
Member

Choose a reason for hiding this comment

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

question: Where does progressive come in? Should that be included here among the options?

Copy link
Member Author

Choose a reason for hiding this comment

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

ahh great question..... so the thing is that historically we have called the "blue" color version as default, but this is changing with Polaris. This is a change that it is being proposed in Figma (which makes sense IMO). So we have to make that mapping (default -> progressive), but the goal is to align on the naming later. I'll add a note in the code to clarify that.

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 created a ticket to address this later. I'll add the TODO comment to point to it. Thanks again for raising this question.

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! Left a question around naming!

: theme.color.stroke.action.default;

const buttonType = `${color}-${kind}-${light}-${size}-${themeName}`;
const buttonType = `${buttonColor}-d_${disabled}-${kind}-l_${light}-${size}-${themeName}`;
Copy link
Member

Choose a reason for hiding this comment

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

question: What does the -d_ mean in the buttonType name?

Copy link
Member Author

Choose a reason for hiding this comment

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

Disabled, but that's just to be able to cache this string properly.

This is mostly for debugging purposes if needed.

@jandrade jandrade merged commit c1f420a into main Jan 31, 2025
13 of 14 checks passed
@jandrade jandrade deleted the semantic-icon-button branch January 31, 2025 22:53
Copy link

codecov bot commented Jan 31, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (cbf9c34) to head (e7b02f3).
Report is 4 commits behind head on main.

Additional details and impacted files

Impacted file tree graph

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

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 cbf9c34...e7b02f3. 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.

4 participants