VisorVisor
ComponentsGeneral

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.

light

On 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.

Today
Dark
Dark + Photo
Light
Light + 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.

Today
Dark
Dark + Photo
Light
Light + Photo

Installation

npx visor add theme-toggle

This copies two files into your project:

  • components/ui/theme-toggle/theme-toggle.tsx — the component
  • components/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

VariableDefaultSets
--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-fillBackground
--theme-toggle-bg-hover--surface-muted; under glass, --glass-on-control-hover-fillBackground on hover
--theme-toggle-ring--border-default; under glass, --glass-on-control-ringThe 1px inset ring's colour
--theme-toggle-color--text-primary; under glass, --glass-on-control-inkIcon colour
--theme-toggle-ease--motion-easing-enterThe 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

PropTypeDefaultDescription
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-labelstring"Switch light or dark"The button's accessible name.
...propsReact.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; pass aria-label to 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)