-
Notifications
You must be signed in to change notification settings - Fork 460
feat(ui): migrate Mosaic Button to StyleX #9208
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
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
d70595f
feat(ui): migrate Mosaic Button to StyleX
alexcarpenter 722577d
ci(repo): dedupe lockfile and ignore dist-mosaic in prettier
alexcarpenter d9b55a4
refactor(ui): use shadcn color token names in Mosaic
alexcarpenter 7b64fd0
fix(ui): build styles.css as part of build so its export resolves
alexcarpenter a8c6010
fix(ui): correct Button story source path and assert default type
alexcarpenter d406b5d
fix(swingset): enable useCSSLayers to match published Mosaic build
alexcarpenter 96dd1dd
refactor(ui): single --cl-spacing base with defineConsts scale
alexcarpenter 97a0b23
fix(ui): guard button hover behind (hover: hover) media query
alexcarpenter f940a20
docs(repo): add StyleX reference to mosaic skill
alexcarpenter 14b1b40
fix(ui): drop no-op src/**/*.stylex.ts sideEffects entry
alexcarpenter ddcb7d6
refactor(swingset): drop no-op StyleX config (lightningcss targets, p…
alexcarpenter 09be4be
Merge remote-tracking branch 'origin/main' into migrate-mosaic-to-stylex
alexcarpenter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| --- | ||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| # Styling a component with StyleX | ||
|
|
||
| Mosaic styling is migrating off the Emotion slot-recipe engine (`styling.md`) | ||
| onto **StyleX** (`@stylexjs/stylex` 0.19). StyleX is compile-time atomic CSS: the | ||
| style objects become hashed atom classes plus one static stylesheet, with zero | ||
| runtime. This file is the authoring model for the StyleX layer; read it against | ||
| the pilot, `packages/ui/src/mosaic/components/button/`. | ||
|
|
||
| The public contract is unchanged from the recipe era — consumers still target | ||
| `--cl-*` vars, the `.cl-<slot>` class, and `data-<axis>` attrs, never StyleX's | ||
| hashed `x…` atoms. What changes is how a component is authored internally. | ||
|
|
||
| ## File layout & the `.stylex.ts` rule | ||
|
|
||
| | File | Holds | | ||
| | ------------------------- | ---------------------------------------------------------- | | ||
| | `tokens.stylex.ts` | `defineVars` / `defineConsts` only (the tokens) | | ||
| | `<comp>/<comp>.styles.ts` | `stylex.create({...})` — the atoms | | ||
| | `<comp>/<comp>.tsx` | component; spreads `stylex.props(...)` via `mergeProps` | | ||
| | `props.ts` | `themeProps` (`.cl-<slot>` + `data-<axis>`) + `mergeProps` | | ||
| | `styles/index.ts` | isolated-build barrel; derives `*VarName` types | | ||
|
|
||
| All 9 `@stylexjs` eslint rules run on `src/mosaic/**`. The `enforce-extension` | ||
| rule reserves the `.stylex.ts` extension for token files: **a `.stylex.ts` file | ||
| may export nothing but `defineVars`/`defineConsts` results.** So: | ||
|
|
||
| - `stylex.create(...)` lives in a plain `.styles.ts` file, never `.stylex.ts`. | ||
| - Derived types (`type ColorVarName = keyof typeof colorVars`) live in | ||
| `styles/index.ts`, not in `tokens.stylex.ts` (they'd be a disallowed export). | ||
|
|
||
| ## Tokens (`tokens.stylex.ts`) | ||
|
|
||
| **`defineVars` keys that start with `--` emit verbatim** as real custom | ||
| properties; other keys get hashed. That verbatim behavior is the whole point — | ||
| it gives consumers stable `--cl-*` vars to override in plain CSS: | ||
|
|
||
| ```ts | ||
| const colorDefaults = { | ||
| // one value carries light + dark; resolves against the in-scope `color-scheme`, | ||
| // so dark mode lives in the token, no `@media (prefers-color-scheme)` copy. | ||
| '--cl-color-primary': 'light-dark(oklch(0.205 0 0), oklch(0.922 0 0))', | ||
| } as const; | ||
| export const colorVars = stylex.defineVars(colorDefaults); | ||
| ``` | ||
|
|
||
| **Spacing is one exposed var plus a `defineConsts` scale.** Only `--cl-spacing` | ||
| is a custom property; every step is inlined at build as `calc(var(--cl-spacing) * | ||
| n)` and carries no var of its own: | ||
|
|
||
| ```ts | ||
| export const spacingVars = stylex.defineVars({ '--cl-spacing': '0.25rem' }); | ||
|
|
||
| const step = (multiple: number): string => `calc(var(--cl-spacing) * ${multiple})`; | ||
| export const space = stylex.defineConsts({ '0': '0px', '2': step(2), '8': step(8) /* … */ }); | ||
| // usage: gap: space['2'] | ||
| ``` | ||
|
|
||
| Why `defineConsts` and not a `space(n)` helper: StyleX evaluates | ||
| `stylex.create` **statically at build time** and cannot inline a helper imported | ||
| from a non-`.stylex` module (it errors "Could not resolve the path to the | ||
| imported file"). `defineConsts` is StyleX's shareable inlined-value primitive, so | ||
| it's the only way to get a scale that is both shared across components and free | ||
| of per-step vars. | ||
|
|
||
| ## Atoms (`<comp>.styles.ts`) | ||
|
|
||
| `stylex.create` values must be statically resolvable. Allowed: literals, | ||
| **same-file** locals, and references to `.stylex` tokens (`colorVars[...]`, | ||
| `space['2']`). Not allowed: a value from a helper imported across modules. For | ||
| arithmetic, inline a `calc()` template literal with a token: | ||
| `` `calc(-1 * ${space['2']})` ``. | ||
|
|
||
| **Conditions.** Use StyleX's conditional-value objects (a `default` plus | ||
| pseudo / at-rule keys), and nest to guard hover so it never sticks on touch — | ||
| this is the StyleX form of the old `_hover` condition: | ||
|
|
||
| ```ts | ||
| backgroundColor: { | ||
| default: colorVars['--cl-color-primary'], | ||
| ':active': `color-mix(in oklab, ${colorVars['--cl-color-primary']}, ${colorVars['--cl-color-primary-foreground']} 24%)`, | ||
| '@media (hover: hover)': { | ||
| // only the top-level value needs `default`; the nested block just adds `:hover` | ||
| ':hover': `color-mix(in oklab, ${colorVars['--cl-color-primary']}, ${colorVars['--cl-color-primary-foreground']} 12%)`, | ||
| }, | ||
| }, | ||
| ``` | ||
|
|
||
| Guard `:hover` behind `@media (hover: hover)`; leave `:active`, `:focus-visible`, | ||
| `:disabled` unguarded. Runtime conditions the component owns (disabled, etc.) are | ||
| also reflected as `data-<axis>` attrs via `themeProps` so they stay overridable. | ||
|
|
||
| Write the guard **raw**, as above — there is no `hover()` helper to import, | ||
| because StyleX can't inline a cross-module helper into `create` (the Emotion | ||
| engine's `hover()`/`motionSafe()` utils don't translate). `*.styles.ts` files are | ||
| therefore exempted from the repo's `no-restricted-syntax` media-query rule | ||
| (`eslint.config.mjs`); the `@stylexjs/*` rules still apply. | ||
|
|
||
| ## Public contract (`props.ts`) | ||
|
|
||
| The element carries three things, and nothing else is a contract: | ||
|
|
||
| 1. `--cl-*` vars (from tokens), 2. `.cl-<slot>` class, 3. `data-<axis>` attrs. | ||
|
|
||
| `mergeProps` fuses them in precedence order — **theme props → StyleX atoms → | ||
| consumer `className`/`style`** — so the consumer always wins: | ||
|
|
||
| ```tsx | ||
| <button {...mergeProps(themeProps('button', { intent, variant }), stylex.props(styles.base, ...), className, style)} /> | ||
| ``` | ||
|
|
||
| ## Build & CSS delivery (two contexts, same babel) | ||
|
|
||
| - **Published** (`build:mosaic` → `@stylexjs/rollup-plugin`): compiles the | ||
| `styles/index.ts` barrel into `dist-mosaic/styles.css`, exported as | ||
| `@clerk/ui/styles.css`. Consumers choose the cascade layer at import: | ||
| `@import '@clerk/ui/styles.css' layer(components)`. | ||
| - **Swingset** (source-consumed): `@stylexjs/unplugin/webpack` in `next.config` | ||
| transforms StyleX **JS only** (calls → static atoms; SWC/Emotion untouched); | ||
| `@stylexjs/postcss-plugin` extracts the **CSS** by replacing `@stylex;` in | ||
| `globals.css`. Both must share the same StyleX babel version + options so atom | ||
| hashes line up. | ||
|
|
||
| Both set **`useCSSLayers: true`** (StyleX emits `@layer priorityN` for its own | ||
| precedence; the consumer's `@import … layer()` picks the outer layer). Both pin | ||
| **lightningcss targets** to browsers with native `light-dark()`/`oklch()` so the | ||
| token colors aren't down-leveled into an invalid polyfill. | ||
|
|
||
| ## CSS features we lean on / caveats | ||
|
|
||
| - YES: `var()`, `calc()`, `color-mix()`, `light-dark()`, nested `@media`+pseudo, | ||
| `:hover`/`:active`/`:focus-visible`/`:disabled`, `::before`/`::after`. | ||
| - Prefer CSS-native solutions over JS workarounds for anything StyleX supports. | ||
| - Avoid manual `@layer` / `@property` inside `create` (StyleX owns layering; | ||
| `@property` compiles but emits invalid output). | ||
| - We do not use functions-in-`create` (dynamic runtime values); Mosaic styling is | ||
| fully static. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| # compiled output | ||
| dist | ||
| dist-mosaic | ||
| tmp | ||
| out-tsc | ||
| out | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,6 +13,7 @@ | |
| build | ||
| coverage | ||
| dist | ||
| dist-mosaic | ||
| /packages/nextjs/examples | ||
| node_modules | ||
| package-lock.json | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,38 @@ | ||
| import { createRequire } from 'module'; | ||
| import { dirname, resolve } from 'path'; | ||
| import { fileURLToPath } from 'url'; | ||
|
|
||
| const require = createRequire(import.meta.url); | ||
| const __dirname = dirname(fileURLToPath(import.meta.url)); | ||
|
|
||
| // StyleX CSS extraction. The `@stylexjs/postcss-plugin` scans the Mosaic source, runs the | ||
| // StyleX babel transform itself, and replaces the `@stylex;` directive in `globals.css` with | ||
| // the generated CSS (token `:root` defaults + atoms). This is the CSS half of the setup; the | ||
| // JS half is the unplugin in `next.config.mjs`. Both must use the SAME StyleX babel version | ||
| // and options (`dev`, `rootDir`) so the atom class hashes line up. | ||
| const uiRoot = resolve(__dirname, '../ui'); | ||
|
|
||
| export default { | ||
| plugins: { | ||
| '@stylexjs/postcss-plugin': { | ||
| useCSSLayers: true, | ||
| babelConfig: { | ||
| babelrc: false, | ||
| configFile: false, | ||
| presets: [require('@babel/preset-typescript')], | ||
| plugins: [ | ||
| require('@babel/plugin-syntax-jsx'), | ||
| [ | ||
| require('@stylexjs/babel-plugin'), | ||
| { | ||
| dev: process.env.NODE_ENV !== 'production', | ||
| runtimeInjection: false, | ||
| unstable_moduleResolution: { type: 'commonJS', rootDir: uiRoot }, | ||
| }, | ||
| ], | ||
| ], | ||
| }, | ||
| }, | ||
| '@tailwindcss/postcss': {}, | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.