Switch
A switch on Base UI Switch with a 24px hit area.
For a setting that takes effect at once. In a form that is submitted, use @khaeli/checkbox.
With a label
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"
export default function SwitchWithALabel() {
return (
<div className="flex items-center gap-2">
<Switch id="switch-reminders" defaultChecked />
<Label htmlFor="switch-reminders">Payment reminders</Label>
</div>
)
}
Sizes
import { Label } from "@/components/ui/label"
import { Switch } from "@/components/ui/switch"
export default function SwitchSizes() {
return (
<div className="flex flex-wrap items-center gap-6">
<div className="flex items-center gap-2">
<Switch id="switch-size-small" size="sm" defaultChecked />
<Label htmlFor="switch-size-small">Small</Label>
</div>
<div className="flex items-center gap-2">
<Switch id="switch-size-default" defaultChecked />
<Label htmlFor="switch-size-default">Default</Label>
</div>
</div>
)
}
With a description
Drafts are saved every minute and when you leave the page.
import { Field, FieldContent, FieldDescription, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
export default function SwitchWithADescription() {
return (
<Field orientation="horizontal" className="max-w-sm">
<FieldContent>
<FieldLabel htmlFor="switch-autosave">Save drafts automatically</FieldLabel>
<FieldDescription id="switch-autosave-description">
Drafts are saved every minute and when you leave the page.
</FieldDescription>
</FieldContent>
<Switch id="switch-autosave" defaultChecked aria-describedby="switch-autosave-description" />
</Field>
)
}
Disabled
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
export default function SwitchDisabled() {
return (
<FieldGroup className="max-w-xs gap-3">
<Field orientation="horizontal" data-disabled="true">
<Switch id="switch-sso-locked" disabled />
<FieldLabel htmlFor="switch-sso-locked">Single sign-on</FieldLabel>
</Field>
<Field orientation="horizontal" data-disabled="true">
<Switch id="switch-audit-locked" defaultChecked disabled />
<FieldLabel htmlFor="switch-audit-locked">Audit log</FieldLabel>
</Field>
</FieldGroup>
)
}
Invalid
Workspace owners must keep two-step sign-in on.
import { Field, FieldContent, FieldError, FieldLabel } from "@/components/ui/field"
import { Switch } from "@/components/ui/switch"
export default function SwitchInvalid() {
return (
<Field orientation="horizontal" data-invalid="true" className="max-w-sm">
<FieldContent>
<FieldLabel htmlFor="switch-two-step">Two-step sign-in</FieldLabel>
<FieldError id="switch-two-step-error">Workspace owners must keep two-step sign-in on.</FieldError>
</FieldContent>
<Switch id="switch-two-step" aria-invalid aria-describedby="switch-two-step-error" />
</Field>
)
}
Installation
npx shadcn@latest add @khaeli/switchbunx --bun shadcn@latest add @khaeli/switchInstall @khaeli/foundation first: this item's styling lives in its tokens and [data-slot] rules.
Installs with
- Registry items
- npm packages
- @base-ui/react ^1.7.0
Keyboard
SpaceEnter- Turn the switch on and off.
Tab- Moves focus to the switch. A
disabledswitch leaves the tab order.
Accessible name
- It renders an element with
role="switch"andaria-checked. Name it with a @khaeli/label whosehtmlForis the switch'sid. - The name says what is on when the switch is on, and stays the same in both states: Payment reminders, not Turn reminders off.
- A description or an error is tied to the switch with
aria-describedby.
Registry JSON
The item as the shadcn CLI reads it, with every file it ships: https://ui.khaeli.com/r/switch.json