Skip to main content
React hook to access and control the application theme. Supports light, dark, and system-based theme preferences with localStorage persistence.

Signature

Returns

'dark' | 'light' | 'system'
Current theme setting. Can be “dark”, “light”, or “system” (auto-detect from OS preferences)
'dark' | 'light'
Actual theme after system resolution. When theme is “system”, this reflects the detected OS preference (“dark” or “light”)
(theme: Theme) => void
Function to update the theme. Accepts “dark”, “light”, or “system”. Automatically persists to localStorage.

Usage

Basic Theme Toggle

Theme Selector with System Option

Using Resolved Theme

Complete Theme Menu

How It Works

Theme Resolution

When theme is set to “system”, the hook uses window.matchMedia("(prefers-color-scheme: dark)") to detect the OS preference:

DOM Updates

The hook automatically applies the resolved theme to the document root:

localStorage Persistence

Theme preference is automatically saved to localStorage when changed:

Prerequisites

This hook must be used within a ThemeProvider component. The provider is typically included automatically when using AppShell’s SidebarLayout:
If you use useTheme outside of a ThemeProvider, it will throw an error: “useTheme must be used within a ThemeProvider”

Best Practices

  1. Respect User Preference: Default to “system” theme to respect OS-level preferences
  2. Provide All Options: Offer light, dark, and system options for maximum flexibility
  3. Visual Feedback: Show the current theme setting clearly in your UI
  4. Accessibility: Ensure both themes have sufficient contrast and are accessible

Example: Settings Page