# useTheme

Persist and synchronize the application's light or dark theme.

`useTheme` manages a global `light` or `dark` preference. It can persist the value, synchronize hook consumers and browser tabs, follow the operating-system preference when no value is stored, and update both theme selectors on the document root.

## Import

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

## Basic usage

```tsx
"use client";

import { Button, useTheme } from "@zentara-co/zenos-ui";

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

  return (
    <div>
      <p>Current theme: {theme}</p>
      <Button onClick={toggleTheme}>Use {theme === "dark" ? "light" : "dark"} theme</Button>
      <Button variant="primary" onClick={() => setTheme("light")}>
        Reset to light
      </Button>
    </div>
  );
}
```

The default configuration stores the preference under `theme` and synchronizes both `data-theme` and the `.dark` class on `document.documentElement`.

## Resolution order

When the hook initializes in the browser, it resolves the theme in this order:

1. A valid persisted value for `storageKey`
2. `data-theme="light"` or `data-theme="dark"` on the document root
3. A `.dark` class on the document root
4. A dark operating-system preference
5. `defaultTheme`

Only `light` and `dark` are accepted. Missing or unrelated stored values are ignored.

## Options

| Option            | Type                | Default   | Description                                                       |
| ----------------- | ------------------- | --------- | ----------------------------------------------------------------- |
| `defaultTheme`    | `"light" \| "dark"` | `"light"` | Server value and fallback when no browser preference resolves     |
| `storageKey`      | `string \| null`    | `"theme"` | `localStorage` key; use `null` to disable persistence             |
| `applyToDocument` | `boolean`           | `true`    | Synchronize `data-theme` and `.dark` on the root document element |

```tsx
const theme = useTheme({
  defaultTheme: "dark",
  storageKey: "acme-theme",
  applyToDocument: true,
});
```

Use a product-specific key when multiple applications share one origin. Set `applyToDocument={false}` when a separate theme provider owns the root selectors; persistence and hook state can remain enabled independently.

## Return value

| Value         | Type                              | Description                                      |
| ------------- | --------------------------------- | ------------------------------------------------ |
| `theme`       | `"light" \| "dark"`               | Current resolved theme                           |
| `setTheme`    | `Dispatch<SetStateAction<Theme>>` | Set a value directly or with a functional update |
| `toggleTheme` | `() => void`                      | Toggle between light and dark                    |

The package also exports `UseThemeOptions` and `UseThemeResult` for application APIs that need the public types.

## Synchronization

Calling `setTheme` updates every `useTheme` consumer using the same `storageKey` in the current document. The browser `storage` event synchronizes changes from other tabs. If no valid value is stored, changes to `prefers-color-scheme` update the theme automatically.

Storage access is guarded. Theme state and document updates continue to work when privacy settings or browser policies make `localStorage` unavailable.

## Server rendering and theme flash

The server cannot inspect browser storage or media preferences, so the initial server value is `defaultTheme`. After mounting, the hook resolves browser state and synchronizes the result.

Apply a small initialization script in the document head when the correct theme must render before the first paint. See [Prevent a theme flash](/foundations/dark-mode/#prevent-a-theme-flash) for the complete pattern.

Use [ThemeToggleButton](/components/theme-toggle-button/) when you need the standard accessible control rather than a custom theme interface.

Source: https://zenos-ui.pages.dev/hooks/use-theme/