Skip to Content
ComponentsSwitch

Switch

A control that allows the user to toggle between checked and not checked.

import { Switch } from "@/components/ui/switch" import { Label } from "@/components/ui/label"
<div className="flex items-center gap-2"> <Switch id="my-switch" /> <Label htmlFor="my-switch">Enable feature</Label> </div>

Examples

Checked by default

<div className="flex items-center gap-2"> <Switch id="notifications" defaultChecked /> <Label htmlFor="notifications">Notifications</Label> </div>

Disabled

<Switch disabled /> <Switch disabled defaultChecked />

With description

Receive alerts for new messages and updates.

<Field orientation="horizontal"> <Switch id="notifications" /> <FieldContent> <FieldLabel htmlFor="notifications">Push notifications</FieldLabel> <FieldDescription>Receive alerts for new messages and updates.</FieldDescription> </FieldContent> </Field>

Invalid

<Field orientation="horizontal" data-invalid> <Switch id="terms" aria-invalid /> <FieldContent> <FieldLabel htmlFor="terms">Accept terms and conditions</FieldLabel> <FieldError>You must accept the terms to continue.</FieldError> </FieldContent> </Field>

Card

<div className="grid gap-3"> <FieldLabel> <Field orientation="horizontal"> <Switch id="email" defaultChecked /> <FieldContent> <FieldLabel htmlFor="email">Email notifications</FieldLabel> <FieldDescription>Get notified when someone mentions you.</FieldDescription> </FieldContent> </Field> </FieldLabel> <FieldLabel> <Field orientation="horizontal"> <Switch id="marketing" /> <FieldContent> <FieldLabel htmlFor="marketing">Marketing emails</FieldLabel> <FieldDescription>Receive emails about new products and features.</FieldDescription> </FieldContent> </Field> </FieldLabel> </div>

Size

<Switch size="compact" /> <Switch size="default" />
Last updated on