Switch
Forms
Toggle
Toggle switches for binary on/off states, built on Radix UI primitives. Perfect for settings and preferences.
Preview
Simple on/off toggle
Controlled State
Manage switch state in your component
Receive push notifications on your device
Receive emails about new products and features
Help us improve by sharing usage data
Disabled State
Switches can be disabled
Settings Panel Example
Common use case for switches
Privacy
Notifications
Props
Switch component API reference
| Prop | Type | Default | Description |
|---|---|---|---|
checked | boolean | — | Controlled checked state |
defaultChecked | boolean | false | Default checked state (uncontrolled) |
onCheckedChange | (checked: boolean) => void | — | Callback when state changes |
disabled | boolean | false | Disable the switch |
required | boolean | false | Mark as required in forms |
name | string | — | Name for form submission |
value | string | 'on' | Value for form submission |
Usage
Import and implementation example
import { Switch } from '@/components/ui/switch'
import { Label } from '@/components/ui/label'
export default function Example() {
const [enabled, setEnabled] = useState(false)
return (
<>
{/* Basic switch */}
<div className="flex items-center space-x-2">
<Switch id="airplane-mode" />
<Label htmlFor="airplane-mode">Airplane Mode</Label>
</div>
{/* Controlled switch */}
<Switch
checked={enabled}
onCheckedChange={setEnabled}
/>
{/* With description */}
<div className="flex items-center justify-between">
<div className="space-y-0.5">
<Label htmlFor="notifications">Notifications</Label>
<p className="text-sm text-slate-500">
Receive push notifications
</p>
</div>
<Switch id="notifications" />
</div>
{/* Disabled state */}
<Switch disabled defaultChecked />
</>
)
}Features
Built-in functionality
- Keyboard accessible: Space/Enter to toggle
- Controlled & uncontrolled: Flexible state management
- Smooth animation: 200ms slide transition
- Focus ring: Clear focus indicator
- Disabled state: Visual and functional disabled support
- Full border radius: Circular track design
- Form compatible: Works with form submission
- Dark mode: Full dark mode support
Accessibility
Accessibility considerations
ARIA Attributes
Uses native switch role via Radixaria-checked reflects current stateProperly labeled via htmlFor associationKeyboard Navigation
| Key | Action |
|---|---|
| Tab | Focus the switch |
| Space | Toggle state |
| Enter | Toggle state |
Notes
- Always pair with Label for accessibility
- Disabled state prevents interaction
- Focus ring visible on keyboard focus
- State change announced to screen readers