# Dark mode

Apply Zenos' semantic theme in class-based or data-attribute applications.

Zenos dark mode is a semantic theme, not an inverted color filter. Surfaces, text, borders, elevation, feedback colors, and interaction states receive deliberate values for each mode.

Zenos recognizes either a `.dark` ancestor or `data-theme="dark"` on the document root.

<div className="zenos-theme-pair not-content">
  <div className="is-light">
    <span>Light</span>
    <strong>Security overview</strong>
    <p>12 assets require review.</p>
    <button type="button">Review assets</button>
  </div>
  <div className="is-dark">
    <span>Dark</span>
    <strong>Security overview</strong>
    <p>12 assets require review.</p>
    <button type="button">Review assets</button>
  </div>
</div>

## Apply a theme

Keep the class and data attribute synchronized when the application supports both conventions:

```tsx
document.documentElement.classList.toggle("dark", theme === "dark");
document.documentElement.dataset.theme = theme;
```

Components respond through semantic variables. Application code should not branch between light and dark hex values when one semantic role can describe the intent.

## Global theme hook

The public `useTheme` hook reads the preference in this order:

1. A valid persisted value
2. The document's `data-theme` value
3. The document's `.dark` class
4. The operating-system preference
5. The configured fallback

```tsx
import { useTheme } from "@zentara-co/zenos-ui";

function ThemeControl() {
  const { theme, setTheme, toggleTheme } = useTheme();

  return (
    <Button onClick={toggleTheme} aria-label={`Use ${theme === "dark" ? "light" : "dark"} theme`}>
      {theme === "dark" ? "Light" : "Dark"}
    </Button>
  );
}
```

By default, the hook stores the value under `theme`, updates both root selectors, reacts to changes from another browser tab, and follows system changes until a preference is stored.

See the [`useTheme` hook reference](/hooks/use-theme/) for every option, return value, synchronization rule, and SSR consideration.

Use a product-specific `storageKey` when multiple applications share a domain. Set it to `null` when persistence is managed elsewhere.

## Theme toggle button

`ThemeToggleButton` composes the global hook with an accessible button and an optional View Transition reveal.

```tsx
<ThemeToggleButton
  icon={({ theme }) => (theme === "dark" ? <MoonIcon /> : <SunIcon />)}
  shape="circle"
/>
```

The button sets `aria-pressed`, supplies a default accessible name, forwards native button props, and skips the visual transition when the user prefers reduced motion or the browser does not support View Transitions.

Use the controlled `theme` and `onThemeChange` props when another state manager owns the preference.

## Prevent a theme flash

Apply a stored or system theme before the first paint when server-rendering or generating a static application. Place a small inline script in the document head before styles render:

```html
<script is:inline>
  const stored = localStorage.getItem("theme");
  const theme =
    stored === "light" || stored === "dark"
      ? stored
      : matchMedia("(prefers-color-scheme: dark)").matches
        ? "dark"
        : "light";

  document.documentElement.dataset.theme = theme;
  document.documentElement.classList.toggle("dark", theme === "dark");
</script>
```

If storage access can be restricted, wrap it in `try/catch` and fall back to the system preference.

## Customize both modes

Override roles rather than component selectors:

```css
:root {
  --background-default: #ffffff;
  --text-default: #0f0f0f;
  --border-default: rgb(15 15 15 / 8%);
}

[data-theme="dark"] {
  --background-default: #0f0f0f;
  --text-default: #f8f9fa;
  --border-default: rgb(255 255 255 / 12%);
}
```

Check every interactive state in both modes: default, hover, pressed, selected, focused, disabled, invalid, and loading.

## Surfaces and elevation

Dark interfaces need visible surface hierarchy without bright borders around every region. Use semantic elevation surfaces and shadows so cards, raised controls, and overlays remain distinct from the canvas.

Avoid pure black for every layer. Small differences between canvas, card, raised, and overlay surfaces make dense interfaces easier to scan.

## Images, charts, and code

- Verify logos and illustrations remain legible on both surfaces.
- Use semantic chart tokens rather than light-only series colors.
- Keep grid lines and axes quieter than the plotted data.
- Test code blocks, selection, syntax colors, and scrollbars independently.
- Do not reduce image opacity globally as a substitute for a dark-mode treatment.

## Testing

Test the complete interface in light, dark, and system-selected modes. Verify persistence across reloads, synchronization across tabs, initial render without a flash, forced-colors behavior, reduced motion, and readable focus indicators.

The documentation theme control follows the same browser preference and lets Starlight manage persistence, avoiding a second competing theme state.

Source: https://zenos-ui.pages.dev/foundations/dark-mode/