Components / Inputs
Toggle Switch
An accessible on/off switch with a springy knob.
Notifications
Dark mode
Beta features
Installation
Requires React 18+ and Tailwind CSS.
- 1
Install dependencies
$ npm install motion - 2
Add the component
Copy the code from the Code tab above into
components/ui/toggle-switch.tsx. - 3
Use it
example.tsximport { ToggleSwitch } from "@/components/ui/toggle-switch"; export default function ToggleSwitchDemo() { return ( <div className="w-64 space-y-4"> <div className="flex items-center justify-between"> <span className="text-sm text-zinc-700 dark:text-zinc-300">Notifications</span> <ToggleSwitch label="Notifications" defaultChecked /> </div> <div className="flex items-center justify-between"> <span className="text-sm text-zinc-700 dark:text-zinc-300">Dark mode</span> <ToggleSwitch label="Dark mode" /> </div> <div className="flex items-center justify-between"> <span className="text-sm text-zinc-500">Beta features</span> <ToggleSwitch label="Beta features" disabled /> </div> </div> ); }
Props
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | boolean | — | Controlled value. Omit to let the switch manage itself. |
| defaultChecked | boolean | false | Starting value when uncontrolled. |
| onChange | (checked: boolean) => void | — | Called when toggled. |
| label | string | — | Accessible name for screen readers. |
| disabled | boolean | false | Disable the switch. |