Theme modes
WebEditor supports three theme modes:Light mode
Fixed light color scheme
Dark mode
Fixed dark color scheme
Auto mode
Follows system preferences
useTheme hook
TheuseTheme hook provides theme management functionality:
Hook interface
Return values
- theme
- resolvedTheme
- setTheme
- toggleTheme
The current theme setting (
'light', 'dark', or 'auto')Hook implementation
The hook is implemented in~/workspace/source/packages/webeditor/src/hooks/use-theme.ts:
Initial theme detection
~/workspace/source/packages/webeditor/src/hooks/use-theme.ts:11-25
The theme preference is persisted to
localStorage under the key webeditor-theme, so it survives page reloads.Resolved theme calculation
~/workspace/source/packages/webeditor/src/hooks/use-theme.ts:27-37
System preference listener
~/workspace/source/packages/webeditor/src/hooks/use-theme.ts:58-92
The hook automatically listens for changes to the system’s color scheme preference and updates the resolved theme accordingly when in auto mode.
Applying theme to DOM
~/workspace/source/packages/webeditor/src/hooks/use-theme.ts:95-105
Usage in WebEditor
The WebEditor component initializes theme detection automatically:~/workspace/source/packages/webeditor/src/editor/index.tsx:70-72
You don’t need to do anything to enable theme support - it’s automatically initialized when you use the WebEditor component.
Customizing theme behavior
You can create a theme toggle button:Example theme toggle
CSS variables and styling
WebEditor uses CSS custom properties for theming. These are typically defined in your CSS:Example theme variables
Supported CSS variables
Supported CSS variables
WebEditor components use these CSS variables:
--background- Main background color--foreground- Main text color--card- Card background--card-foreground- Card text--popover- Popover/menu background--popover-foreground- Popover text--primary- Primary accent color--primary-foreground- Primary accent text--secondary- Secondary accent color--secondary-foreground- Secondary accent text--muted- Muted background--muted-foreground- Muted text--accent- Accent background (hover states)--accent-foreground- Accent text--border- Border color--ring- Focus ring color
Server-side rendering
The theme hook is SSR-safe:During server-side rendering, the hook returns sensible defaults and skips browser-specific operations.
Theme persistence
Theme preferences are saved to localStorage:~/workspace/source/packages/webeditor/src/hooks/use-theme.ts:39-45
Theme toggle function
The toggle function cycles through all three modes:~/workspace/source/packages/webeditor/src/hooks/use-theme.ts:47-56
The toggle cycle is: auto → light → dark → auto
Best practices
Use auto mode by default
Auto mode respects user preferences and adapts to system settings
Provide manual override
Give users a way to override auto mode if they prefer
Persist preferences
The hook automatically saves to localStorage, ensuring consistency
Test both themes
Ensure your custom components work in both light and dark modes
Next steps
Overview
Return to the architecture overview
API Reference
Explore the complete useTheme API