---
name: light-dark-mode
description: >
Implement, fix or audit light/dark mode on any website or web app, on any stack (static
HTML, Astro, Vite/React, Next.js, SvelteKit, Vue): a data-theme attribute on the html
element, semantic CSS variables with a dark override block, a blocking boot script so
there is no flash of the wrong theme, a small controller for localStorage, OS
preference, browser chrome color and cross-tab sync, and an accessible toggle or
Light/Dark/System control. Use whenever the user asks for dark mode, a theme toggle or
switcher, prefers-color-scheme, data-theme, color-scheme, theme-color, a flash of the
wrong theme on load, dark mode for code blocks, canvas or images, or says the site
"should follow the system theme", even if they only say "add a dark version". Not for
Quantum Design token files (`system-light.json`, `system-dark.json`), which belong to
the `quantum-design-tokens` skill.
---
# Light/Dark Mode
One mechanism for every site: **the resolved theme lives in `data-theme` on ``,
colors live in CSS variables, and an inline script sets the attribute before first
paint.** Components never know which theme is active; they read variables, and the
variables change under them.
If the project uses Quantum Design tokens (`system-light.json` / `system-dark.json`,
`--components-tokens--*` variables), `quantum-design-tokens` owns the token files and the
boot script (its Phase 3). Skip Phases 3 and 4 here and keep that boot script, upgrade its
toggle in Phase 6 instead of adding a second one, read `theme-color` from a component
token instead of a literal, and do everything else.
## Hard rules (never violate)
1. **One attribute.** `` is the only switch. Never a
`.dark` class somewhere else, a theme prop threaded through components, or two
mechanisms side by side.
2. **Three preferences, two themes.** The stored preference is `light`, `dark` or
`system`. What lands on `` is always resolved to `light` or `dark`.
Resolution order: stored choice → the default (normally `system`, which means the OS
setting) → light.
3. **Colors are variables.** Light values on `:root, [data-theme="light"]`, dark
overrides on `[data-theme="dark"]`, each block with its own `color-scheme`. Any
literal color outside the token block is a bug.
4. **No flash.** A blocking inline script in `
`, ahead of every stylesheet, sets
the attribute and the `theme-color` meta. Hydration only takes over from it.
5. **Validate what you read.** Storage can hold anything or throw. Accept only the
three values; wrap every storage access in try/catch.
6. **Follow the OS live only while the preference is `system`.** An explicit choice
wins until the user changes it.
7. **One set of constants.** Storage key, default preference and `theme-color` values
are identical in the boot script and the controller. In SSR frameworks, build the
boot script string from the same constants.
8. **Everything follows.** Native controls and scrollbars (`color-scheme`), the mobile
browser bar (`theme-color`), code blocks, canvas, images and Storybook switch too.
## Phase 1 — Inspect
- Stack and where the document `` is authored: `index.html`, `BaseHead.astro`,
`_document.tsx`, `app/layout.tsx`, `app.html`, a template partial.
- Existing mechanisms: grep for `prefers-color-scheme`, `data-theme`, `.dark`,
`color-scheme`, `theme-color`, `next-themes`, `darkMode`, `localStorage` + `theme`.
- Hardcoded colors in component CSS/JS: `#hex`, `rgb(`, `hsl(`, Tailwind palette
classes (`bg-white`, `text-gray-900`).
- Things that won't follow CSS variables: syntax highlighting, `