ThemeManager is a small global object exposed once themeManager.js loads. Most projects only need the default setup from Installation - the methods below are for advanced or programmatic use.
Initializes the theme manager: loads the saved theme, attaches button listeners, and starts watching for system theme changes. Called automatically on script load - you shouldn't normally need to call this yourself.
Applies a theme and persists it to localStorage.
ThemeManager.applyTheme('light');
ThemeManager.applyTheme('dark');
ThemeManager.applyTheme('auto'); // follow the OS settingLoads the previously saved theme from localStorage and applies it.
Returns the currently selected theme setting.
ThemeManager.getCurrentTheme(); // 'light' | 'dark' | 'auto'Returns whether dark mode is currently active (accounting for auto mode resolving against the OS preference).
if (ThemeManager.isDarkMode()) {
console.log('dark mode is active');
}Returns whether a given theme setting should resolve to dark mode.
ThemeManager.shouldBeDark('light'); // false
ThemeManager.shouldBeDark('dark'); // true
ThemeManager.shouldBeDark('auto'); // depends on the OS preferenceAttaches click listeners to all elements matching .theme-btn. Called automatically during init().
Updates the active/inactive visual state of the toggle buttons.
ThemeManager.updateButtonStates('dark');Starts listening for OS-level theme changes, so auto mode stays in sync in real time.
| Property | Default | Description |
|---|---|---|
STORAGE_KEY |
'theme' |
The localStorage key used to persist the theme. |
DEFAULT_THEME |
'auto' |
The theme used when nothing is saved yet. |
BUTTON_SELECTOR |
'.theme-btn' |
CSS selector used to find toggle buttons. |
ACTIVE_CLASSES |
n/a | Classes applied to the active button. |
HOVER_CLASSES |
n/a | Classes applied on hover. |
ROOT_DARK_CLASS |
'dark' |
Class added to <html> when dark mode is active. |
// Read the saved theme
console.log(localStorage.getItem('theme'));
// Change it directly
localStorage.setItem('theme', 'dark');
// Reset to the default
localStorage.removeItem('theme');