Documentation Index
Fetch the complete documentation index at: https://mintlify.com/devscribe-team/webeditor/llms.txt
Use this file to discover all available pages before exploring further.
Overview
TheuseTheme hook provides comprehensive theme management for your WebEditor application. It handles theme persistence, system preference detection, and automatic theme application to the document root.
Import
Usage
Basic usage
Setting a specific theme
Cycling through themes
Using resolved theme for conditional rendering
Return value
The hook returns an object with the following properties:The current theme setting. Can be
'light', 'dark', or 'auto'.When set to 'auto', the theme follows the system preference using prefers-color-scheme media query.The actual theme being applied to the UI. This is always either
'light' or 'dark', never 'auto'.When theme is 'auto', resolvedTheme reflects the system’s color scheme preference.Function to update the theme. Accepts
'light', 'dark', or 'auto' as the argument.The selected theme is automatically persisted to localStorage under the key 'webeditor-theme'.Function to cycle through themes in the order:
auto → light → dark → auto.This provides a convenient way to implement a theme toggle button.Type definitions
Behavior
Initialization
On initial load, the hook determines the theme in the following order:- localStorage: Checks for a previously saved theme in
localStorageunder the key'webeditor-theme' - Default: Falls back to
'auto'mode if no saved preference exists
Theme persistence
When you callsetTheme(), the new theme is automatically saved to localStorage, ensuring the user’s preference persists across sessions.
System preference detection
Whentheme is set to 'auto', the hook:
- Detects the system’s color scheme preference using the
prefers-color-schememedia query - Automatically updates
resolvedThemewhen the system preference changes - Listens for system theme changes in real-time
DOM updates
The hook automatically applies the resolved theme to the document root by:- Adding a class name (
'light'or'dark') todocument.documentElement - Removing the previous theme class when the theme changes
- Updating the class whenever
resolvedThemechanges
The hook applies theme classes to
document.documentElement (the <html> tag), allowing you to style your entire application based on the theme using CSS selectors like .dark and .light.Server-side rendering
The hook is safe to use in SSR environments:- Returns
'auto'as the default theme whenwindowis undefined - Returns
'light'as the default resolved theme on the server - Skips DOM manipulation and localStorage access when running on the server
Example integration
Here’s a complete example showing how theuseTheme hook is used within the WebEditor component:
The WebEditor component automatically uses this hook internally, so themes are applied automatically when you use the editor. You typically only need to use this hook directly when building custom theme controls.
Best practices
Use resolvedTheme for UI decisions
When you need to know which theme is actually being displayed, useresolvedTheme instead of theme:
Provide theme controls
Give users control over their theme preference:Respect system preferences
Consider defaulting to'auto' mode to respect user system preferences: