Interactive Demo
See how the auto theme feature works. Select a theme mode and observe how it resolves to an actual theme based on your OS preference.
Theme Mode Selector
themeMode: autoosPreference: lightresolvedTheme: light
How It Works
- Light: Always uses light theme
- Dark: Always uses dark theme
- Auto: Follows your OS preference
- OS set to dark → Dark theme
- OS set to light → Light theme
- OS changes → Theme updates automatically
osPreference and resolvedTheme values update in real-time (when mode is "Auto").Preview
Following OS preference (light)
Overview
The auto theme feature provides a 3-way toggle for theme selection: Light, Dark, and Auto. When set to "Auto", the application automatically
follows the operating system's color scheme preference using the CSS media query prefers-color-scheme.
- Three theme modes - Light, Dark, and Auto (System)
- Real-time updates - Theme changes instantly when OS preference changes
- Persistent setting - Theme mode saved to localStorage
- No FOUC - Flash of unstyled content prevented with isInline script
- SSR compatible - Graceful fallback on server-side rendering
Theme Mode Values
| Value | Behavior | Use Case |
|---|---|---|
'light' | Always applies light theme | User prefers light regardless of OS |
'dark' | Always applies dark theme | User prefers dark regardless of OS |
'auto' | Follows OS preference, updates in real-time | User wants app to match system theme |
Configuration
The theme type in config.ts now includes the 'auto' option.
Settings Panel
The SettingsPanel component provides a dropdown with all three theme mode options.
Implementation Details
1. Resolving Auto Mode
When the theme mode is 'auto', we use the browser's matchMedia API
to detect the OS preference.
2. Real-time OS Theme Detection
A MediaQueryList listener is set up to detect when the user changes their OS theme preference.
3. Applying the Theme
The resolved theme is applied by adding the appropriate CSS class to the body element.
4. FOUC Prevention
To prevent a flash of unstyled content on page load, an isInline script in app.html resolves and applies the theme before the page renders.
LocalStorage
The theme mode setting is persisted in localStorage under the key theme-mode.
| Key | Possible Values | Default |
|---|---|---|
theme-mode | 'light', 'dark', 'auto' | 'light' |
'auto' is stored, the FOUC prevention script and
SettingsPanel both resolve it to the actual theme based on the current OS preference.Usage
The auto theme feature is built into the SettingsPanel component. Simply include
the component in your layout and users can select their preferred theme mode.
Testing Auto Theme
To test the auto theme feature, you can change your OS theme preference or use browser DevTools.
Data Flow
Here's how the auto theme system processes theme changes:
User selects "Auto" in SettingsPanel
↓
localStorage['theme-mode'] = 'auto'
↓
getResolvedThemeMode('auto') checks prefers-color-scheme
↓
Returns 'light' or 'dark' based on OS
↓
Applies pa-mode-light or pa-mode-dark to body
↓
MediaQueryList listener watches for OS changes
↓
On change → reapply resolved themeEdge Cases
Server-Side Rendering (SSR)
On the server, window.matchMedia is not available. The implementation defaults
to 'light' on the server, and the client-side hydration corrects it if needed.
FOUC Prevention
The isInline script in app.html runs synchronously before the page renders,
ensuring the correct theme class is applied immediately. This prevents any flash of the
wrong theme.
Listener Cleanup
The MediaQueryList listener is properly cleaned up when:
- The user switches away from "Auto" mode
- The SettingsPanel component is destroyed
Browser Support
The prefers-color-scheme media query is supported in all modern browsers:
| Browser | Version | Support |
|---|---|---|
| Chrome | 76+ | Full |
| Firefox | 67+ | Full |
| Safari | 12.1+ | Full |
| Edge | 79+ | Full |
| IE 11 | - | Fallback to light |
prefers-color-scheme, the auto mode will
gracefully fall back to the light theme.