Skip to content
Playground

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.

TSX
import { useTheme } from "@zentara-co/zenos-ui";
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.

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.

OptionTypeDefaultDescription
defaultTheme"light" | "dark""light"Server value and fallback when no browser preference resolves
storageKeystring | null"theme"localStorage key; use null to disable persistence
applyToDocumentbooleantrueSynchronize 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.

ValueTypeDescription
theme"light" | "dark"Current resolved theme
setThemeDispatch<SetStateAction<Theme>>Set a value directly or with a functional update
toggleTheme() => voidToggle between light and dark

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

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.

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 for the complete pattern.

Use ThemeToggleButton when you need the standard accessible control rather than a custom theme interface.