|
1 | 1 | --- |
2 | 2 | name: css-light-dark |
3 | | -description: Light and dark theme in native CSS — color tokens as `light-dark()` pairs on `:root` with `color-scheme`, so the site follows the system; a forced `color-scheme` for a theme switch or a preview toolbar; keeping `light-dark()` native through the build; and a theme class with a palette of its own for widgets on someone else's page. Apply when defining color tokens, adding a dark theme, or wiring a theme switch. |
| 3 | +description: Light and dark theme in native CSS — color tokens as `light-dark()` pairs on `:root` with `color-scheme`, so the site follows the system; keeping `light-dark()` native through the build; a theme switch that forces `color-scheme` on the root, keeps the choice in `localStorage` and sets it from an inline script in the head, so a server-rendered page opens in the chosen theme; pictures drawn for one theme; and a theme class with a palette of its own for widgets on someone else's page. Apply when defining color tokens, adding a dark theme, or wiring a theme switch. |
4 | 4 | license: MIT |
5 | 5 | compatibility: |
6 | 6 | - Claude Code |
@@ -60,25 +60,141 @@ metadata: |
60 | 60 | } |
61 | 61 | ``` |
62 | 62 |
|
63 | | -- A site's own theme switch forces the scheme on the root the same way, so the tokens stay `light-dark()` pairs. |
64 | | -- A theme class with a palette of its own is for exceptional cases, such as DevTools that sit on someone else's page and switch their own theme: the root is not theirs to set. Their tokens live on the class: the default palette on `.root`, the other one on a modifier that redefines the colors. Each sets its `color-scheme`, so native controls and scrollbars follow: |
| 63 | +## A theme switch |
65 | 64 |
|
66 | | - ```css |
67 | | - /* uikit/Theme/Theme.module.css */ |
68 | | - .root { |
69 | | - --colorBackground: #1d1d1f; |
70 | | - --colorText: #ececee; |
71 | | - /* ... */ |
| 65 | +A site's own theme switch forces the scheme on the root the same way, so the tokens stay `light-dark()` pairs. The choice is the browser's, kept in `localStorage`, never in a cookie or the account: the server renders one page for everyone, and a visitor without an account can choose too. The page's head sets it before the stylesheets arrive, so a server-rendered page opens in the chosen theme, never in the system's first: |
| 66 | + |
| 67 | +```html |
| 68 | +<head> |
| 69 | + <!-- The browser paints its canvas and controls in the system's theme before the stylesheets come --> |
| 70 | + <meta name="color-scheme" content="light dark"> |
| 71 | + <!-- The theme chosen; a browser that refuses storage throws, and keeps the system's --> |
| 72 | + <script> |
| 73 | + try { |
| 74 | + const theme = localStorage.getItem('theme') |
| 75 | +
|
| 76 | + if (theme === 'light' || theme === 'dark') { |
| 77 | + document.documentElement.dataset.theme = theme |
| 78 | + document.documentElement.style.colorScheme = theme |
| 79 | + } |
| 80 | + } catch {} |
| 81 | + </script> |
| 82 | + <link rel="stylesheet" href="/styles.css"> |
| 83 | +</head> |
| 84 | +``` |
| 85 | + |
| 86 | +The switch keeps the choice and sets it the same way. System sets nothing, and the root's `color-scheme: light dark` follows the device again: |
| 87 | + |
| 88 | +```ts |
| 89 | +// theme.ts |
| 90 | +export type Theme = 'system' | 'light' | 'dark' |
| 91 | + |
| 92 | +// The key the head's script reads too |
| 93 | +const THEME_KEY = 'theme' |
72 | 94 |
|
73 | | - color-scheme: dark; |
74 | | - color: var(--colorText); |
| 95 | +/** |
| 96 | + * Sets a theme on the root: `color-scheme` picks each token's half, and `data-theme` serves what |
| 97 | + * a token can't switch, such as a picture drawn for one theme. |
| 98 | + * @param theme - The theme; anything but `light` and `dark` is the system's. |
| 99 | + */ |
| 100 | +export function applyTheme(theme: string | null) { |
| 101 | + const root = document.documentElement |
| 102 | + |
| 103 | + if (theme === 'light' || theme === 'dark') { |
| 104 | + root.dataset.theme = theme |
| 105 | + root.style.colorScheme = theme |
| 106 | + } else { |
| 107 | + root.removeAttribute('data-theme') |
| 108 | + root.style.removeProperty('color-scheme') |
75 | 109 | } |
| 110 | +} |
76 | 111 |
|
77 | | - .light { |
78 | | - --colorBackground: #f2f2f4; |
79 | | - --colorText: #1d1d1f; |
80 | | - /* ... */ |
| 112 | +/** |
| 113 | + * Keeps the theme chosen and sets it. |
| 114 | + * @param theme - The theme chosen. |
| 115 | + */ |
| 116 | +export function setTheme(theme: Theme) { |
| 117 | + try { |
| 118 | + if (theme === 'system') { |
| 119 | + localStorage.removeItem(THEME_KEY) |
| 120 | + } else { |
| 121 | + localStorage.setItem(THEME_KEY, theme) |
| 122 | + } |
| 123 | + } catch { |
| 124 | + // A browser that refuses storage keeps the choice for this page alone |
| 125 | + } |
81 | 126 |
|
82 | | - color-scheme: light; |
| 127 | + applyTheme(theme) |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Follows a theme chosen in another tab. |
| 132 | + * @returns Stops following. |
| 133 | + */ |
| 134 | +export function syncTheme() { |
| 135 | + function onStorage(event: StorageEvent) { |
| 136 | + if (event.key === THEME_KEY) { |
| 137 | + applyTheme(event.newValue) |
| 138 | + } |
83 | 139 | } |
84 | | - ``` |
| 140 | + |
| 141 | + window.addEventListener('storage', onStorage) |
| 142 | + |
| 143 | + return () => window.removeEventListener('storage', onStorage) |
| 144 | +} |
| 145 | +``` |
| 146 | + |
| 147 | +A picture drawn in one theme's inks, such as a logo, has no token to switch: it comes twice, and the theme hides one of the two. With no theme chosen, the system's decides: |
| 148 | + |
| 149 | +```css |
| 150 | +/* mixins.module.css */ |
| 151 | + |
| 152 | +/** |
| 153 | + * `hideDark` on a picture drawn for the light theme, `hideLight` on its twin for the dark one. |
| 154 | + */ |
| 155 | + |
| 156 | +:root[data-theme='dark'] .hideDark, |
| 157 | +:root[data-theme='light'] .hideLight { |
| 158 | + display: none; |
| 159 | +} |
| 160 | + |
| 161 | +@media (prefers-color-scheme: dark) { |
| 162 | + :root:not([data-theme]) .hideDark { |
| 163 | + display: none; |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +@media (prefers-color-scheme: light) { |
| 168 | + :root:not([data-theme]) .hideLight { |
| 169 | + display: none; |
| 170 | + } |
| 171 | +} |
| 172 | +``` |
| 173 | + |
| 174 | +- Nothing the server renders shows the choice: the server doesn't know it, and the browser's first render would differ from the HTML. A control that shows it, such as a menu's checked item, reads it in the browser, once the menu opens. |
| 175 | +- A framework that hydrates `<html>` finds two attributes there it didn't render: let it keep them, as React's `suppressHydrationWarning` on `<html>` does. |
| 176 | +- A color reads its token, never `data-theme`. The attribute is for what a token can't switch, a picture: a `<source media>` reads the system's theme alone, and an SVG picture's own `prefers-color-scheme` doesn't follow the page in Safari 18, which draws its light inks whatever the theme. |
| 177 | + |
| 178 | +## A theme class |
| 179 | + |
| 180 | +A theme class with a palette of its own is for exceptional cases, such as DevTools that sit on someone else's page and switch their own theme: the root is not theirs to set. Their tokens live on the class: the default palette on `.root`, the other one on a modifier that redefines the colors. Each sets its `color-scheme`, so native controls and scrollbars follow: |
| 181 | + |
| 182 | +```css |
| 183 | +/* uikit/Theme/Theme.module.css */ |
| 184 | +.root { |
| 185 | + --colorBackground: #1d1d1f; |
| 186 | + --colorText: #ececee; |
| 187 | + /* ... */ |
| 188 | + |
| 189 | + color-scheme: dark; |
| 190 | + color: var(--colorText); |
| 191 | +} |
| 192 | + |
| 193 | +.light { |
| 194 | + --colorBackground: #f2f2f4; |
| 195 | + --colorText: #1d1d1f; |
| 196 | + /* ... */ |
| 197 | + |
| 198 | + color-scheme: light; |
| 199 | +} |
| 200 | +``` |
0 commit comments