shadcn.io is not affiliated with official shadcn/ui
Switch examples · Icons
Theme Toggle with Icons Switch for React
A switch between a sun and a moon icon for light and dark mode.
A Switch with a sun before it and a moon after it, the familiar shape of a light and dark toggle. The icons are decoration; the switch keeps its own state, so connect it to your theme code to change the page.
Preview
Installation
FAQ
Do the sun and moon change when the switch toggles?
No. SunIcon is size-4, and MoonIcon adds text-muted-foreground; those classes stay as written. Both have aria-hidden, in a flex items-center gap-2 row.
What do screen readers call this switch?
The icons are hidden from screen readers, so the switch has aria-label (Dark mode). Keep a text name whenever the icons are the only cue.
How do I control this switch in React?
Nothing in this file sets the on state, so the switch starts off and keeps its own state. To control it, keep the on or off state in your own state and pass it back. To start it on, use the default prop.
The prop is defaultChecked.
Pass checked; onCheckedChange receives the new boolean.
How do I add a visible label to this switch?
Put the Switch in a horizontal Field next to a FieldLabel. Clicking the label toggles the switch, and screen readers read the label with it.
Give the Switch an id and set the same value as htmlFor on the FieldLabel.
Related
- Icon with Label SwitchA switch with a bell icon and a
- Volume Toggle SwitchA switch between a muted and a volume
- Default SwitchA switch in its default off state, with
- Feature Card SwitchA bordered card with an icon, a label, a
- Switch in ItemAn outlined item with a title, a
- Settings Card SwitchA bordered settings card with a bell, a