Theme toggle
Lets people choose a light or dark theme, or follow their system, and remembers the choice.
Example
This panel follows the toggle. On a site, leave out target so the toggle sets the theme on <html>.
<focsd-theme-toggle target="#theme-toggle-demo" storage-key-prefix="docs-demo-"></focsd-theme-toggle><focsd-card id="theme-toggle-demo"> <p> This panel follows the toggle. On a site, leave out <code>target</code> so the toggle sets the theme on <code><html></code>. </p> <focsd-button variant="action">Save</focsd-button></focsd-card>Usage
Import the module once, then use the tag like any HTML element. Every framework below renders the same element; see Frameworks for setup.
<script type="module"> import '@focsd-dev/components/focsd-theme-toggle.js';</script>
<focsd-theme-toggle target="#theme-toggle-demo" storage-key-prefix="docs-demo-"></focsd-theme-toggle>import { ThemeToggle } from '@focsd-dev/react';
export function Example() { return <ThemeToggle onChange={(e) => console.log(e.target)} target="#theme-toggle-demo" storage-key-prefix="docs-demo-"></ThemeToggle>;}<script setup>import '@focsd-dev/components/focsd-theme-toggle.js';</script>
<template> <focsd-theme-toggle @change="onChange" target="#theme-toggle-demo" storage-key-prefix="docs-demo-"></focsd-theme-toggle></template>// Add CUSTOM_ELEMENTS_SCHEMA to the component's schemas, then:import '@focsd-dev/components/focsd-theme-toggle.js';
// template<focsd-theme-toggle (change)="onChange($event)" target="#theme-toggle-demo" storage-key-prefix="docs-demo-"></focsd-theme-toggle><script> import '@focsd-dev/components/focsd-theme-toggle.js';</script>
<focsd-theme-toggle onchange={(e) => console.log(e.target)} target="#theme-toggle-demo" storage-key-prefix="docs-demo-"></focsd-theme-toggle>API
Attributes
| Attribute | Property | Type | Default |
|---|---|---|---|
value | value | ThemeChoice | system |
label-display | labelDisplay | 'shown' | 'hidden' | shown |
target | target | string | — |
storage-key-prefix | storageKeyPrefix | string | focsd- |
label | label | string | — |
Methods
| Method | Description |
|---|---|
options() | The options, in order. |
readTarget(target) | The value a target shows now. |
applyTo(value, target) | Apply a value to the target. |
storedValue(value) | The value to store for a choice (null removes the key). |
Events
| Event | Description |
|---|---|
change | Fired when the user picks a theme; read value. |
CSS parts
Style the inside of the component with ::part(), for example focsd-theme-toggle::part(group).
| Part | Description |
|---|---|
group | The radio group. |
option | Each option. |
option-checked | The checked option. |
icon | The icon in each option. |
label | The text of each option. |
Accessibility
- It is a radio group named "Color theme" (or
label). Each option has visible text (Light, Dark, System, High contrast) beside its icon, so the choice never depends on the icon alone. - The group is one Tab stop. The arrow keys move between the options and choose them, as in any radio group; Home and End go to the first and last. In right-to-left text, Left moves forward.
- The chosen option shows a border and a bar under it as well as a color change, and a Highlight border in forced-colors mode.
- System removes
data-focsd-theme, so the page followsprefers-color-scheme(andprefers-contrast). The choice is stored asfocsd-themeinlocalStorage; storage that is blocked is ignored. - Put the tokens'
noFlashScriptin the page head so a returning visitor's theme applies before the first paint.
Keyboard
| Key | Action |
|---|---|
| Tab | Move to the chosen option |
| Arrow keys | Move to the next or previous option and choose it |
| Home / End | Choose the first or last option |
Ask the docs
Hi, what are you building?
Ask about any component, theming, forms or frameworks. I answer with the right component, a snippet and a link to its docs.