Day Night Switch
betaA theme switch whose thumb squashes and stretches across the track while its sun morphs into a moon.
- Forms
- 1 file
- 2 npm packages
- Accessibility notes
Loading the example…
pnpm dlx @dowel-ui/cli add day-night-switchnpm packages installed: class-variance-authority, radix-ui.
import { DayNightSwitch } from "@/components/ui/day-night-switch";Default
Demo
Sizes
<DayNightSwitch>
| Prop | Type | Default |
|---|---|---|
checkedControlled state: true is night. | boolean | — |
defaultCheckedInitial state when uncontrolled. | boolean | false |
onCheckedChange | (checked: boolean) => void | — |
size | "sm" | "md" | "lg" | "md" |
Every prop of Radix UI’s Switch.Root, plus className.
Built on the Radix switch primitive: role="switch" with aria-checked, toggled by click, Space or Enter, with a hidden input inside forms and a visible focus ring. The sun and moon are aria-hidden and there is no visible label, so name it with aria-label ("Dark mode") or a <label>. The thumb's position carries the state as well as the glyph and the track colour. It travels toward the inline end, mirrored in right-to-left layouts. Under reduced motion the state changes without the squash or the morph.
10/10
checks passed
- Tested — passes
- axe assertion — passes
- Keyboard tested — passes
- Storybook examples — passes
- Accessibility documented — passes
- Semantic tokens only — passes
- Motion from tokens — passes
- className merged — passes
- Visible focus — passes
- No fixed widths — passes