uni-themes

API Reference

Options, hooks and subpath exports

Options

ThemeProvider and ThemeScript take the same config — pass it to both.

OptionDefaultDescription
themes['light', 'dark']Selectable themes, excluding 'system'
defaultTheme'system'Falls back to 'light' when enableSystem: false
enableSystemtrueTrack prefers-color-scheme, offer 'system'
attribute'class'Or any data-* attribute, e.g. 'data-theme'
storageKey'theme'localStorage key and cookie name
storage'localStorage''cookie' enables server reading (localStorage still mirrored for cross-tab sync); 'none' disables persistence
enableColorSchemetrueKeep <html style="color-scheme"> in sync
disableTransitionOnChangefalseSuppress CSS transitions for the frame a change lands on
forcedThemePin the rendered theme; setTheme still persists

ThemeScript additionally accepts nonce for CSP.

useTheme()

const {
  theme, // selected theme, e.g. 'system'
  resolvedTheme, // 'system' resolved to 'light' | 'dark'
  systemTheme, // OS preference; undefined on the server
  themes,
  forcedTheme,
  setTheme,
} = useTheme()

On the server (and hydration's first pass) theme is defaultTheme and resolvedTheme guesses 'light' for 'system' — gate theme-dependent text on mount if you need exactness. The DOM attribute itself is always correct before paint.

Subpath exports

EntryContents
uni-themesThemeProvider, useTheme (client)
uni-themes/scriptThemeScript, buildThemeScript (server-safe)
uni-themes/coreFramework-agnostic store + script builder + cookie helpers
uni-themes/servergetThemeFromRequest, parseThemeCookie, serializeThemeCookie
uni-themes/nextgetTheme() via next/headers

On this page