|
2 | 2 | 'styled-components': minor |
3 | 3 | --- |
4 | 4 |
|
5 | | -Add `createTheme` utility for CSS variable theming that works across RSC and client components. |
| 5 | +Add `createTheme(defaultTheme, options?)` for CSS variable theming that works across RSC and client components. |
6 | 6 |
|
7 | | -`createTheme` takes a default theme object and returns an object with the same shape where every leaf value is replaced with a `var(--prefix-path, fallback)` CSS string. This lets you use theme values in RSC styled components (which can't access React context) while keeping the same tokens as client components. |
| 7 | +Returns an object with the same shape where every leaf is `var(--prefix-path, fallback)`. Pass it to `ThemeProvider` for stable class name hashes across themes (no hydration mismatch on light/dark switch). |
8 | 8 |
|
9 | 9 | ```ts |
10 | | -import { createTheme } from 'styled-components'; |
11 | | - |
12 | | -const theme = createTheme({ |
13 | | - colors: { primary: '#0070f3', text: '#111' } |
14 | | -}); |
15 | | - |
| 10 | +const theme = createTheme({ colors: { primary: '#0070f3' } }); |
16 | 11 | // theme.colors.primary → "var(--sc-colors-primary, #0070f3)" |
17 | | -// theme.colors.text → "var(--sc-colors-text, #111)" |
18 | | -// theme.raw → { colors: { primary: '#0070f3', text: '#111' } } |
19 | | -``` |
20 | | - |
21 | | -Pass the returned object to `ThemeProvider` so that `p.theme.colors.*` interpolations produce CSS variable references instead of literal values. This gives you stable class name hashing across themes (no hydration mismatches when switching light/dark) and lets CSS variables handle the actual theming. |
22 | | - |
23 | | -**`theme.resolve(el?)`** — read the current computed CSS variable values from the DOM, returning an object with the same shape as the original theme but with resolved values. Useful for JS logic that needs the actual current color (e.g., canvas rendering, conditional branching). Client-only. |
24 | | - |
25 | | -```ts |
26 | | -const current = theme.resolve(); |
27 | | -// current.colors.primary → "#3b82f6" (dark mode value) |
28 | | -// current.colors.text → "#f9fafb" |
29 | | - |
30 | | -// Scoped to a specific element: |
31 | | -const scoped = theme.resolve(myElement); |
| 12 | +// theme.raw → original object |
| 13 | +// theme.resolve(el?) → computed values from DOM (client-only) |
| 14 | +// theme.GlobalStyle → component that emits CSS var declarations |
32 | 15 | ``` |
33 | 16 |
|
34 | | -Options: |
35 | | -- `prefix` (default: `"sc"`) — namespace for CSS variable names |
36 | | -- `selector` (default: `":root"`) — CSS selector for variable declarations, use `":host"` for Shadow DOM |
| 17 | +Options: `prefix` (default `"sc"`), `selector` (default `":root"`, use `":host"` for Shadow DOM). |
0 commit comments