Theme Toggle
A round light/dark button. The icon going away turns and shrinks out as the other springs in.
Default
One button that flips between light and dark. Dark shows the sun, the way to light; light shows the moon. On a press the icon going away turns 100° and shrinks to 0.3 as it fades, and the other springs in from the opposite turn. The toggle is controlled: it asks for the other tone and the host decides where the tone lives.
lightOn a Photo
Glass is off by default: the toggle is a solid button on the theme's surface, border and ink. Inside data-glass="on" (see Glass), where a photo sits behind, it reads the glass layer's control values, the same set Button's glass variant reads: the control fill and its hover, the ring, the ink and the blur. The theme sets those values, so no app passes colours or a backdrop-filter. The toggle stays round where a theme draws squircle corners.
In Five Looks
The toggle as the theme draws it, in each look on the theme picked in the docs, at Backstage's top-bar size (42px, 38px on phones). The Photo looks turn glass on over a photo.
Honcho Backstage
The Backstage top-bar toggle from the theme alone. Today is the honcho theme; the other four looks are honcho-backstage. Dark and Light draw it in the theme's ink on its secondary surface; over a photo it takes the theme's glass control values.
Installation
npx visor add theme-toggleThis copies two files into your project:
components/ui/theme-toggle/theme-toggle.tsx— the componentcomponents/ui/theme-toggle/theme-toggle.module.css— the styles
Usage
import { ThemeToggle } from '@/components/ui/theme-toggle/theme-toggle';
const [tone, setTone] = useState<'light' | 'dark'>('light');
<ThemeToggle
value={tone}
onValueChange={(next) => {
setTone(next);
document.documentElement.dataset.tone = next;
}}
/>Theming
| Variable | Default | Sets |
|---|---|---|
--theme-toggle-size | --spacing-10 (40px) | Width and height |
--theme-toggle-icon-size | --spacing-4 (16px) | Sun and moon size |
--theme-toggle-bg | --surface-subtle; under glass, --glass-on-control-fill | Background |
--theme-toggle-bg-hover | --surface-muted; under glass, --glass-on-control-hover-fill | Background on hover |
--theme-toggle-ring | --border-default; under glass, --glass-on-control-ring | The 1px inset ring's colour |
--theme-toggle-color | --text-primary; under glass, --glass-on-control-ink | Icon colour |
--theme-toggle-ease | --motion-easing-enter | The turn's and the hover's easing |
Under glass the toggle also blurs the photo with --glass-on-control-blur. Leave the colour variables unset to follow the theme; set them only to depart from it.
The motion itself is fixed: the turn takes 600ms, the scale 500ms on --motion-easing-spring, the fade 300ms, and a press dips the button to 0.94.
API Reference
ThemeToggleProps
| Prop | Type | Default | Description |
|---|---|---|---|
value | 'light' | 'dark' | — | The tone in force now. Dark shows the sun, light shows the moon. Required. |
onValueChange | (value: 'light' | 'dark') => void | — | Called with the other tone on a press. |
aria-label | string | "Switch light or dark" | The button's accessible name. |
...props | React.ButtonHTMLAttributes<HTMLButtonElement> | — | Standard button attributes are forwarded. onClick runs first; call preventDefault() in it to skip the change. |
Accessibility
- A native
<button type="button">named "Switch light or dark" by default; passaria-labelto change it (4.1.2) - Both icons are
aria-hidden; the name carries the meaning - With
prefers-reduced-motion: reduce, the icons cross-fade in place and the press does not dip (2.3.3) - Focus draws a visible ring at the focus-ring tokens (2.4.7)
ThemeSwitcher
Fixed-position pill switcher for theme and dark/light mode, with an extras slot for hosting devtools chrome. Built with CSS Modules — copy it into your project and own it completely.
Box
Universal layout wrapper for padding, margin, background, border, and border-radius via Visor design tokens.