Skip to content

[WB-1644.2] Convert border and sizing tokens to CSS variables #2561

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

Merged
merged 8 commits into from
Apr 16, 2025

Conversation

jandrade
Copy link
Member

@jandrade jandrade commented Apr 16, 2025

Summary:

This PR migrates the border and sizing tokens to internally use CSS
variables. It also updates the documentation to reflect these changes.

Also, refactored the logic for generating CSS variables by moving all those
tokens to a theme/default.ts file, then converting all the tokens in there to
CSS variables.

Issue: https://khanacademy.atlassian.net/browse/WB-1644

Test plan:

Navigate to the border and sizing docs and verify that the CSS variables are now included:

  • /?path=/docs/packages-tokens-border--docs
Screenshot 2025-04-16 at 11 42 37 AM
  • /?path=/docs/packages-tokens-sizing--docs
Screenshot 2025-04-16 at 11 42 45 AM

Also inspect the styles in the browser and check that the CSS variables are
being used in places where sizing are border tokens are used.
Screenshot 2025-04-16 at 11 42 08 AM

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

changeset-bot bot commented Apr 16, 2025

🦋 Changeset detected

Latest commit: c8c88fd

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

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 Apr 16, 2025

Size Change: +42 B (+0.04%)

Total Size: 103 kB

Filename Size Change
packages/wonder-blocks-dropdown/dist/es/index.js 19.7 kB +7 B (+0.04%)
packages/wonder-blocks-tabs/dist/es/index.js 1.93 kB +7 B (+0.36%)
packages/wonder-blocks-tokens/dist/es/index.js 3.06 kB +28 B (+0.92%)
ℹ️ View Unchanged
Filename Size
packages/wonder-blocks-accordion/dist/es/index.js 3.56 kB
packages/wonder-blocks-announcer/dist/es/index.js 2.04 kB
packages/wonder-blocks-banner/dist/es/index.js 1.58 kB
packages/wonder-blocks-birthday-picker/dist/es/index.js 1.88 kB
packages/wonder-blocks-breadcrumbs/dist/es/index.js 886 B
packages/wonder-blocks-button/dist/es/index.js 4.25 kB
packages/wonder-blocks-cell/dist/es/index.js 2.36 kB
packages/wonder-blocks-clickable/dist/es/index.js 3.12 kB
packages/wonder-blocks-core/dist/es/index.js 2.85 kB
packages/wonder-blocks-data/dist/es/index.js 6.25 kB
packages/wonder-blocks-form/dist/es/index.js 6.03 kB
packages/wonder-blocks-grid/dist/es/index.js 1.36 kB
packages/wonder-blocks-icon-button/dist/es/index.js 2.27 kB
packages/wonder-blocks-icon/dist/es/index.js 873 B
packages/wonder-blocks-labeled-field/dist/es/index.js 1.26 kB
packages/wonder-blocks-layout/dist/es/index.js 1.82 kB
packages/wonder-blocks-link/dist/es/index.js 1.98 kB
packages/wonder-blocks-modal/dist/es/index.js 5.48 kB
packages/wonder-blocks-pill/dist/es/index.js 1.54 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-styles/dist/es/index.js 468 B
packages/wonder-blocks-switch/dist/es/index.js 1.9 kB
packages/wonder-blocks-testing-core/dist/es/index.js 3.96 kB
packages/wonder-blocks-testing/dist/es/index.js 1.07 kB
packages/wonder-blocks-theming/dist/es/index.js 779 B
packages/wonder-blocks-timing/dist/es/index.js 1.79 kB
packages/wonder-blocks-toolbar/dist/es/index.js 923 B
packages/wonder-blocks-tooltip/dist/es/index.js 7.02 kB
packages/wonder-blocks-typography/dist/es/index.js 1.23 kB

compressed-size-action

Copy link
Contributor

github-actions bot commented Apr 16, 2025

A new build was pushed to Chromatic! 🚀

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

Chromatic results:

Metric Total
Captured snapshots 5
Tests with visual changes 0
Total stories 600
Inherited (not captured) snapshots [TurboSnap] 369
Tests on the build 374

@jandrade jandrade marked this pull request as ready for review April 16, 2025 15:43
@khan-actions-bot khan-actions-bot requested a review from a team April 16, 2025 15:43
@khan-actions-bot
Copy link
Contributor

khan-actions-bot commented Apr 16, 2025

Gerald

Required Reviewers
  • @Khan/wonder-blocks for changes to .changeset/clean-ravens-happen.md, __docs__/wonder-blocks-tabs/navigation-tabs.stories.tsx, __docs__/wonder-blocks-tokens/tokens-border.mdx, __docs__/wonder-blocks-tokens/tokens-sizing.mdx, packages/wonder-blocks-tokens/src/index.ts, packages/wonder-blocks-dropdown/src/components/select-opener.tsx, packages/wonder-blocks-tabs/src/components/navigation-tab-item.tsx, packages/wonder-blocks-tokens/src/build/generate-css-variables.ts, packages/wonder-blocks-tokens/src/internal/generate-tokens.ts, packages/wonder-blocks-tokens/src/theme/default.ts, packages/wonder-blocks-tokens/src/tokens/theme.ts, packages/wonder-blocks-tokens/src/util/constants.ts, packages/wonder-blocks-tokens/src/internal/__test__/generate-tokens.test.ts, packages/wonder-blocks-tokens/src/theme/primitive/border.ts, packages/wonder-blocks-tokens/src/theme/primitive/sizing.ts, packages/wonder-blocks-tokens/src/theme/semantic/semantic-color.ts

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 Apr 16, 2025

npm Snapshot: Published

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

Packages can also be installed manually by running:

pnpm add @khanacademy/wonder-blocks-<package-name>@PR2561

@@ -299,7 +299,7 @@ export const HeaderWithNavigationTabsExample: StoryComponentType = {
navigationTabsRoot: {
// set margin to negative value of header vertical spacing so
// that selected indicator lines up with header border
margin: `-${headerVerticalSpacing} 0`,
margin: `var(${headerVerticalSpacing}, *-1) 0`,
Copy link
Member Author

Choose a reason for hiding this comment

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

Comment on lines +13 to +15
import theme from "./tokens/theme";

const {border, semanticColor, sizing} = theme;
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: Made this change to make it backwards compatible with the existing JS tokens

Comment on lines +5 to +9
export default {
border,
semanticColor,
sizing,
};
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: All the tokens included in this theme file will be automatically mapped to CSS vars and included in @khanacademy/wonder-blocks-tokens/styles.css. (I'll add this note in the code as a comment).

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.

Noticed a typo in one of the stories that might be causing the Chromatic diff. Other than that, looks good!

Would it be helpful to document somewhere how to negate WB token values? And how the token values are css variables now instead of specific values? I wonder if a lint rule could be helpful to make sure tokens are used properly as css variables now!

@@ -299,7 +299,7 @@ export const HeaderWithNavigationTabsExample: StoryComponentType = {
navigationTabsRoot: {
// set margin to negative value of header vertical spacing so
// that selected indicator lines up with header border
margin: `-${headerVerticalSpacing} 0`,
margin: `calc(${headerVerticalSpacing}, *-1) 0`,
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 comma after the token intended? I think this might be why there's an unexpected Chromatic diff for this story!

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 why, thanks for noticing that :)

@jandrade
Copy link
Member Author

Would it be helpful to document somewhere how to negate WB token values? And how the token values are css variables now instead of specific values? I wonder if a lint rule could be helpful to make sure tokens are used properly as css variables now!

@beaesguerra These are great points! I'm going to iterate on these ideas in separate PRs.

https://khanacademy.atlassian.net/browse/WB-1813

@jandrade jandrade merged commit 2656fd4 into feature/css-vars Apr 16, 2025
14 checks passed
@jandrade jandrade deleted the css-vars-theme branch April 16, 2025 21:26
Copy link

codecov bot commented Apr 16, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 0.00%. Comparing base (aa776bd) to head (c92a5b6).
Report is 1 commits behind head on feature/css-vars.

Additional details and impacted files

Impacted file tree graph

@@           Coverage Diff            @@
##   feature/css-vars   #2561   +/-   ##
========================================
========================================

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 aa776bd...c92a5b6. Read the comment docs.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

jandrade added a commit that referenced this pull request Apr 22, 2025
…n` (#2541)

## Summary:

- [WB-1816] Rework `semanticColor` tokens to be exported as CSS variables (#2481)
- [WB-1644.1] Modify ThemeSwitcher to use a data-wb-theme attribute for theming (#2557)
- [WB-1644.2] Convert border and sizing tokens to CSS variables (#2561)
- [🔥AUDIT🔥] Tokens: Export TS types correctly in package.json so consumers can use them correctly (#2564)

[WB-1816]: https://khanacademy.atlassian.net/browse/WB-1816?atlOrigin=eyJpIjoiNWRkNTljNzYxNjVmNDY3MDlhMDU5Y2ZhYzA5YTRkZjUiLCJwIjoiZ2l0aHViLWNvbS1KU1cifQ

Issue: "none"

## Test plan:

Verify that all the components are now using CSS variables when `sizing`, `border` and `semanticColor` tokens are used.

<img width="2910" alt="Screenshot 2025-04-17 at 3 56 39 PM" src="https://github.com/user-attachments/assets/65602a0e-073c-4109-9b28-cfd7d11f4752" />

Author: jandrade

Reviewers: beaesguerra

Required Reviewers:

Approved By: beaesguerra

Checks: ✅ 34 checks were successful, ⏭️  8 checks have been skipped, ⏹️  15 checks were cancelled

Pull Request URL: #2541
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants