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
Section titled “Import”import { useTheme } from "@zentara-co/zenos-ui";Basic usage
Section titled “Basic usage”"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
Section titled “Resolution order”When the hook initializes in the browser, it resolves the theme in this order:
- A valid persisted value for
storageKey data-theme="light"ordata-theme="dark"on the document root- A
.darkclass on the document root - A dark operating-system preference
defaultTheme
Only light and dark are accepted. Missing or unrelated stored values are ignored.
Options
Section titled “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 |
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
Section titled “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
Section titled “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
Section titled “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 for the complete pattern.
Use ThemeToggleButton when you need the standard accessible control rather than a custom theme interface.