Signature
Theme switch
Smooth light and dark transitions.
In a header
Three icons — light, dark, system — with one pill that glides between them. In browsers with View Transitions, the new theme is revealed as a circle growing from the option you clicked.
Northwind
When to use
- App and docs headers where the user should choose between light, dark and following the system.
- If you only need a two-state toggle, call
useTheme().toggle()from your own button.
Sizes and instant swap
size="sm" makes the options 24px; reveal={false} swaps instantly with no transition.
API
ThemeSwitch
| Prop | Type | Default | Description |
|---|---|---|---|
| reveal | boolean | true | Circular reveal from the clicked option via the View Transitions API. Falls back to an instant swap. |
| size | 'sm' | 'md' | 'md' | Options are 24 / 28px (36px on touch screens). |
| label | string | 'Theme' | Accessible name of the group. |
| ...props | HTMLAttributes<HTMLDivElement> | — | Forwarded to the group, including ref and className. |
It reads and writes the same state as useTheme() (stored in localStorage and applied by the theme script), so it stays in sync with any other control.
Keyboard
| Key | Action |
|---|---|
| Tab | Moves into and out of the group (one stop: the selected option). |
| ← → ↑ ↓ | Selects the previous / next option. |
| Home / End | Selects the first / last option. |
| Space / Enter | Selects the focused option. |
Accessibility
- A radio group: role="radiogroup" with three role="radio" options, aria-checked and a roving tabindex. Selection follows focus.
- Each icon-only option has an accessible name (Light, Dark, System) and a tooltip title.
- The pill is decorative (aria-hidden). Keyboard focus shows the 3px ring.
Motion
- Pill: one element measured by
useIndicatorand moved withtransformon--ui-spring-morph(550ms). The first placement is never animated. - Reveal: document.startViewTransition() swaps the theme; the new snapshot is clipped by circle(0 → farthest corner) centred on the click point, 520ms, cubic-bezier(0.22, 1, 0.36, 1). While it runs, <html data-ui-reveal> turns off the browser’s default cross-fade.
- Keyboard activation reveals from the centre of the focused option.
- Reduced motion: no transition at all — the theme swaps instantly and the pill jumps. Browsers without View Transitions behave the same.