# Dark mode

Dark mode is a colour theme with light text on a dark background, chosen in the operating system or in the app, which a web page reads through `prefers-color-scheme`. Done well it is a second set of values for the same colour roles, not an inverted page. Meadowstack's apps follow the system setting, may let people override it, and test contrast in both themes.

Checked 2 December 2026. By Bohuslav Poláček. https://meadowstack.com/glossary/dark-mode/

*The kit's first-paint script: a stored choice wins, otherwise the system's setting, applied before the page paints.*

From `packages/kit/src/lib/theme-script.ts` in the Meadowstack repository:

```ts
 * The first-paint theme script, kept free of React so a Vite config (or any
 * HTML template) can inline it into <head>. It runs before the page paints,
 * so a dark-mode user never sees a white flash.
 *
 * Logic, mirrored by resolveTheme() in theme.tsx: a stored "light" or "dark"
 * wins; anything else follows prefers-color-scheme. Storage access is wrapped
 * in try/catch because private windows and blocked site data throw.
 */
export const THEME_STORAGE_KEY = "kit-theme";

export const themeScript = `(function(){var p;try{p=localStorage.getItem("${THEME_STORAGE_KEY}")}catch(e){}var d=p==="dark"||(p!=="light"&&window.matchMedia("(prefers-color-scheme: dark)").matches);var r=document.documentElement;r.classList.toggle("dark",d);r.style.colorScheme=d?"dark":"light"})();`;
```

## Why it matters in a business app

People who run a dispatch board into the evening or sit in a dim warehouse office set their system to dark and expect their tools to follow. An app that stays white, or flashes white for a moment on every load, stands out in the worst way.

Dark mode is also where colour mistakes surface: a grey that reads on white sinks into a dark page, a status colour tuned for light turns muddy. If only the light theme is checked, the dark one ships broken.

## How Meadowstack handles it

- **Follow the system.** AGENTS.md: the theme follows the system everywhere; apps may offer an override (`ThemeProvider`). Never default to dark, never read the clock.
- **No flash.** `themeScript` runs in the page's head before it paints: a stored "light" or "dark" wins, anything else follows `prefers-color-scheme`. Storage access is wrapped, because private windows can refuse it.
- **Same roles, other values.** The palette generator writes the dark set from the same rules as the light one, from a dark page of `oklch(0.155 0 0)`, so components ask for [semantic tokens](https://meadowstack.com/glossary/semantic-color-tokens/) and never for a theme.
- **Tested in both.** `npm run check:contrast` checks every text and surface pair in light and dark, the end-to-end tests run axe in both, and the definition of done asks for screenshots of changed screens in light and dark.

## Common mistakes

- **Inverting colours.** Inverted images, avatars and charts look wrong; dark mode is a palette, not a filter.
- **Pure black with pure white text.** The kit's dark page is a very dark grey, and its text levels are generated to hold contrast without glaring.
- **Shadows that disappear.** On a dark page a light shadow does nothing; the kit's dark elevation adds a faint top highlight and deeper shadows.
- **Forgetting the first paint.** Applying the theme after the app loads gives a white flash on every visit.

## Questions

### Should a business app default to dark mode?

No. Follow the person's system setting, which is light unless they chose otherwise, and let them override it in the app if they want.

### How do you avoid the white flash when a dark page loads?

Decide the theme in a small script in the page's head, before the first paint, and set the dark class and the colour scheme there. Meadowstack's kit ships that script as themeScript.

### Does dark mode need its own contrast check?

Yes. Every text and background pair has different values in dark mode, so it has to be measured again; Meadowstack's contrast guard and accessibility tests run in both themes.

## Sources

- [MDN, prefers-color-scheme](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-color-scheme), checked 2 December 2026

## Related

- [APCA](https://meadowstack.com/glossary/apca/) (Glossary)
- [Contrast ratio](https://meadowstack.com/glossary/contrast-ratio/) (Glossary)
- [Contrast checker: APCA and WCAG 2](https://meadowstack.com/tools/apca-contrast-checker/) (Tools)
- [Contrast in business apps: what an audit of our demo found, and how we fixed it](https://meadowstack.com/blog/contrast-in-business-apps/) (Blog)
- [Notice: messages by importance, in one frame](https://meadowstack.com/components/notice/) (Components)
- [StatusLabel: a status as a glyph and a word](https://meadowstack.com/components/status-label/) (Components)
