default.tsx
1'use client';23import { Switch } from '@/components/ui/switch';45export function Default() {6 return <Switch aria-label="Toggle switch" />;7}
Installation
pnpm dlx nachui add switchAnatomy
1import { Switch } from '@/components/ui/switch';
1<Switch id="airplane-mode" />
Variants
Default
default.tsx
1'use client';23import { Switch } from '@/components/ui/switch';45export function Default() {6 return <Switch aria-label="Toggle switch" />;7}
With Label
Switch component alongside a label for context.
with-label.tsx
1'use client';23import { Label } from '@/components/ui/label';4import { Switch } from '@/components/ui/switch';56const switches = [{ id: 'airplane-mode', label: 'Airplane Mode' }];78export function WithLabel() {9 return (10 <div className="flex flex-col gap-3">11 {switches.map((item) => (12 <div key={item.id} className="flex items-center gap-2">13 <Switch id={item.id} />14 <Label htmlFor={item.id}>{item.label}</Label>15 </div>16 ))}17 </div>18 );19}
Disabled
A disabled state stops interactions.
disabled.tsx
1'use client';23import { Label } from '@/components/ui/label';4import { Switch } from '@/components/ui/switch';56const switches = [{ id: 'disabled-switch', label: 'Airplane Mode' }];78export function Disabled() {9 return (10 <div className="flex flex-col gap-3">11 {switches.map((item) => (12 <div key={item.id} className="flex items-center gap-2">13 <Switch id={item.id} disabled />14 <Label htmlFor={item.id} className="opacity-50">15 {item.label}16 </Label>17 </div>18 ))}19 </div>20 );21}
API Reference
Switch
| Prop | Type | Default | Description |
|---|---|---|---|
onCheckedChange | (checked: boolean) => void | - | Callback when checked state changes |
disabled | boolean | - | Disables the switch |
Found something to improve?
Notice a bug, typo, or missing detail on this page? Help us make the documentation better by opening a GitHub issue.