Skip to content

Theme Switcher

A component that provides a convenient way to toggle between light and dark themes in your Metro UI application.

None

<!-- Basic usage -->
<input type="checkbox" data-role="theme-switcher">
<!-- With custom options -->
<input type="checkbox" data-role="theme-switcher"
data-mode="button"
data-dark-symbol="🌚"
data-light-symbol="🌝"
data-state="dark">
<!-- Disabled state -->
<input type="checkbox" data-role="theme-switcher" disabled>
// Initialize with Metro.makePlugin
const themeSwitcher = Metro.makePlugin("#myThemeSwitcher", "theme-switcher", {
mode: "button",
darkSymbol: "🌚",
lightSymbol: "🌝"
});
ParameterTypeDefaultDescription
stateStringMetro.theme.LIGHTInitial state of the theme switcher
targetString"html"Selector for the element to apply theme classes to
saveStateBooleantrueWhether to save the selected theme in localStorage
saveStateKeyString"THEME:SWITCHER"Key used for storing theme preference in localStorage
clsDarkString""Additional class to add to the target when dark theme is active
darkSymbolString"🌙"Symbol displayed when dark theme is active
lightSymbolString"🌞"Symbol displayed when light theme is active
modeString"switch"Display mode: “switch” (toggle switch) or “button” (circular button)
  • val([value]) - Gets or sets the current theme value. Returns Metro.theme.DARK or Metro.theme.LIGHT if called without arguments.
  • destroy() - Destroys the component and removes it from the DOM.
// Get current theme
const themeSwitcher = Metro.getPlugin('#myThemeSwitcher', 'theme-switcher');
const currentTheme = themeSwitcher.val(); // Returns "dark" or "light"
// Set theme
themeSwitcher.val(Metro.theme.DARK); // Switch to dark theme
// or
themeSwitcher.val(true); // Same as above
EventDescription
onThemeSwitcherCreateTriggered after theme switcher component creation
onChangeThemeTriggered when theme is changed (receives an object with state property: true for dark, false for light)
// Initialize with event handlers
Metro.makePlugin("#myThemeSwitcher", "theme-switcher", {
onThemeSwitcherCreate: function() {
console.log("Theme switcher created");
},
onChangeTheme: function(e) {
console.log("Theme changed to:", e.state ? "dark" : "light");
}
});

You can globally configure the theme-switcher component using the Metro.themeSwitcherSetup method:

Metro.themeSwitcherSetup({
saveState: false,
darkSymbol: "🌚",
lightSymbol: "🌝",
mode: "button"
});
VariableDefault (Light)Dark ModeDescription
--theme-switcher-background#e9e9e9#232527Background color of the switcher track
--theme-switcher-background-button#ffffff#232527Background color in button mode
--theme-switcher-background-checked#191919#474748Background color when checked (dark mode)
--theme-switcher-background-disabled#e8e8e8#e8e8e8Background color when disabled
--theme-switcher-toggle-color#fff#232527Toggle button color
--theme-switcher-toggle-disabled#ccc#3e4145Toggle color when disabled
--theme-switcher-text-color#000#efefefText color
--theme-switcher-text-color-checked#fff#fffText color when checked (dark mode)
--theme-switcher-focus-color#e8e8e8#191919Focus outline color
--theme-switcher-border-color#e8e8e8#474748Border color
/* Custom styling example */
#myThemeSwitcher {
--theme-switcher-background: #f0f0f0;
--theme-switcher-background-checked: #333;
--theme-switcher-toggle-color: #ffd700;
}
  • .theme-switcher - Base class for the component (automatically added)
  • .mode-switch - Applied when mode is set to “switch” (default)
  • .mode-button - Applied when mode is set to “button”

When activated, the component adds the class dark-side to the target element (default is html). You can use this class to define your dark theme styles:

:root {
--background-color: #ffffff;
--text-color: #000000;
}
.dark-side {
--background-color: #232527;
--text-color: #ffffff;
}

This allows you to define different CSS variable values for light and dark themes, which your components can then use for styling.