components / form
Switch
Toggle an option with immediate effect.
switch-default.tsx
import { Switch } from "@/components/ui/switch";
export default function SwitchDefault() {
return (
<div className="flex flex-col gap-4 text-sm">
<label className="flex items-center justify-between gap-6">
email notifications <Switch defaultChecked />
</label>
<label className="flex items-center justify-between gap-6">
compact mode <Switch />
</label>
</div>
);
}Installation
$ npx shadcn@latest add https://cd-ui.vercel.app/r/switch.jsonfirst time? set up components.json and the theme first: see installation.
Usage
import { Switch, SwitchRow } from "@/components/ui/switch"
<Switch defaultChecked />
<SwitchRow title="Weekly digest" description="A summary, once a week." />Examples
Setting row
SwitchRow puts title, description, and switch in a <label>: the whole row is clickable and the title names the switch.
switch-row.tsx
import { SwitchRow } from "@/components/ui/switch";
export default function SwitchRowExample() {
return (
<div className="flex w-full max-w-sm flex-col divide-y">
<SwitchRow className="py-3" defaultChecked description="A summary of activity, once a week." title="Weekly digest" />
<SwitchRow className="py-3" description="Only for sign-ins from a new device." title="Security alerts" />
<SwitchRow className="py-3" description="Unavailable on your plan." disabled title="Priority support" />
</div>
);
}API
SwitchRow
Row with title, description, and a switch. className goes on the row; every other prop goes on the Switch. Has a focus ring on the row and a disabled state.
| prop | type | default |
|---|---|---|
titleMain text of the row. | ReactNode | - |
descriptionSecondary text, below the title. | ReactNode | - |
classNameExtra classes, merged with | string | - |
Switch
Base UI · Switch.RootToggle. Use it for immediate effect; for "apply later", prefer Checkbox.
| prop | type | default |
|---|---|---|
checked · defaultCheckedControlled or initial state. | boolean | - |
onCheckedChangeCalled when toggled. | (checked: boolean) => void | - |
classNameExtra classes, merged with | string | - |
Keyboard
| key | action |
|---|---|
| SpaceEnter | Toggles. |