Settings

Sidebar
Display
Profile Panel
👤

John Doe

Administrator
  • 📊 Dashboard
  • 📝 Forms
  • 📋 Tables
Commands
🚀
Go to Page
Navigate to a page
/go
🌓
Toggle Theme
Switch between light and dark mode
/theme
📐
Toggle Sidebar
Show or hide the sidebar
/sidebar
⚙️
Open Settings
Open the settings panel
/settings
Search In
📄
Pages
Search pages
:p
🧩
Components
Search component pages
:c
Auto Theme Switching allows your application to automatically follow the user's operating system theme preference. When set to "Auto", the theme switches between light and dark mode based on the OS setting and reacts to changes in real-time.

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
Current State:
  • themeMode: auto
  • osPreference: light
  • resolvedTheme: 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
Try it! Change your OS theme setting and watch the osPreference and resolvedTheme values update in real-time (when mode is "Auto").
Preview
☀️
Resolved Theme: light

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

ValueBehaviorUse Case
'light'Always applies light themeUser prefers light regardless of OS
'dark'Always applies dark themeUser prefers dark regardless of OS
'auto'Follows OS preference, updates in real-timeUser 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.

KeyPossible ValuesDefault
theme-mode'light', 'dark', 'auto''light'
Note: When '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 theme

Edge 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:

BrowserVersionSupport
Chrome76+ Full
Firefox67+ Full
Safari12.1+ Full
Edge79+ Full
IE 11- Fallback to light
For browsers that don't support prefers-color-scheme, the auto mode will gracefully fall back to the light theme.

Keyboard Shortcuts

No shortcuts registered.