Toggle Switch
FreeAn accessible on/off switch built on a native button.
npx shadcn@latest add @ui-shrushank/toggle-switchThe install writes a single file to components/ui/ToggleSwitch.tsx, which exports ToggleSwitch.
import { ToggleSwitch } from "@/components/ui/ToggleSwitch";<ToggleSwitch label="Label" />Needs framer-motion installed. The shadcn CLI adds it for you.
The on/off control people most often fake with a styled checkbox or a plain div.
Built on a native <button> with role="switch" and aria-checked; the knob slides, the track colour changes.
Every line below was read out of the file you are about to copy, so you can check each one against the source at the bottom of this page.
- Uses the native
role="switch"semantics rather than a styleddiv, so assistive technology announces what the control is and how it behaves. - Carries
aria-checked, so its state is exposed and not left to the visual treatment alone. - Has an explicit
prefers-reduced-motionbranch: the animation is dropped for an instant state change, and the component stays fully usable without it. - Draws a
focus-visiblering that is separate from its hover treatment, so keyboard users get an affordance mouse users do not take away. - Pairs every input with a real
<label>, so the field keeps its name after the placeholder disappears.
role="switch" tells a screen reader "this takes effect immediately" (vs. a checkbox that's submitted later). The semantic difference matters for the user's mental model.
1 required, 0 optional.
| Prop | Type | Default |
|---|---|---|
labelrequired | string | none |
labelrequiredstring
One file, no runtime package. Copy it, or install it with the command at the top of this page.