Skip to content

ThemeProvider

Context provider for theme management. Enables programmatic theme control, system preference detection, localStorage persistence, and cross-tab synchronization.

import { ThemeProvider, useTheme } from 'asterui'

Wrap your app with ThemeProvider to enable theme control:

main.tsx
import { ThemeProvider } from 'asterui'
createRoot(document.getElementById('root')!).render(
<ThemeProvider defaultTheme="system">
<App />
</ThemeProvider>
)

Use the useTheme hook to control the theme:

function ThemeSwitcher() {
const { theme, setTheme, resolvedTheme, isDark } = useTheme()
return (
<div>
<p>Selected: {theme}</p>
<p>Applied: {resolvedTheme}</p>
<p>Mode: {isDark ? 'Dark' : 'Light'}</p>
<button onClick={() => setTheme('light')}>Light</button>
<button onClick={() => setTheme('dark')}>Dark</button>
<button onClick={() => setTheme('system')}>System</button>
</div>
)
}

Use any DaisyUI theme by setting it directly:

function ThemeSelector() {
const { setTheme, resolvedTheme } = useTheme()
const themes = ['light', 'dark', 'cupcake', 'cyberpunk', 'retro', 'valentine']
return (
<select value={resolvedTheme} onChange={(e) => setTheme(e.target.value)}>
{themes.map(t => <option key={t} value={t}>{t}</option>)}
</select>
)
}

Set storageKey to false to disable localStorage persistence:

<ThemeProvider defaultTheme="dark" storageKey={false}>
<App />
</ThemeProvider>

For custom themes, provide an isDarkTheme function:

<ThemeProvider
defaultTheme="system"
isDarkTheme={(theme) => ['dark', 'night', 'dracula', 'synthwave'].includes(theme)}
>
<App />
</ThemeProvider>
PropertyTypeDefaultDescription
defaultThemestring'system'Initial theme. Use 'system' to follow browser preference.
storageKeystring | false'asterui-theme'localStorage key for persistence. Set to false to disable.
lightThemestring'light'Theme to use when system preference is light.
darkThemestring'dark'Theme to use when system preference is dark.
isDarkTheme(theme: string) => boolean-Custom function to determine if a theme is dark.
childrenReact.ReactNode-Child components.
PropertyTypeDescription
themestring | undefinedThe selected theme (e.g., 'system', 'light', 'dark').
resolvedThemestring | undefinedThe actual applied theme after resolving 'system'.
isDarkbooleantrue if dark mode is active.
setTheme(theme: string) => voidFunction to change theme.
colorsThemeColorsComputed theme colors as hex values.
systemTheme'light' | 'dark' | undefinedThe detected system preference.

See useTheme for complete hook documentation including standalone usage and ThemeColors.

  • System Preference Detection: Automatically detects prefers-color-scheme: dark
  • Persistence: Saves user preference to localStorage (configurable)
  • Cross-Tab Sync: Theme changes sync across browser tabs via storage events
  • Any DaisyUI Theme: Works with all 30+ built-in DaisyUI themes
  • Reactive Updates: Automatically updates when theme or system preference changes
  • Canvas Support: Provides hex colors for canvas/WebGL contexts via colors property
  • Respects user’s system preference when defaultTheme="system"
  • Theme preference persists across sessions
  • Works with reduced-motion preferences (no theme transition animations by default)