Switch
View in Storybook
Explore interactive examples and all component variations in our Storybook.
The Switch component provides a toggle control for binary choices. Built on Radix UI, it offers accessibility features, keyboard navigation, and smooth animations. Perfect for settings, preferences, feature toggles, and any on/off controls in your application.
Basic Usage
With Label
Controlled Switch
Current state: ON
Settings Panel
Feature Toggles
Disabled State
These switches are disabled due to insufficient permissions:
Form Integration
Properties
Switch
| Property | Type | Default | Description |
|---|---|---|---|
checked | boolean | - | The controlled checked state of the switch. |
defaultChecked | boolean | false | The default checked state for uncontrolled usage. |
onCheckedChange | (checked: boolean) => void | - | Callback when the checked state changes. |
disabled | boolean | false | Whether the switch is disabled. |
required | boolean | false | Whether the switch is required for form submission. |
name | string | - | The name attribute for form submission. |
value | string | "on" | The value attribute for form submission. |
id | string | - | The HTML id attribute. |
className | string | - | Additional CSS classes for custom styling. |
Accessibility
The Switch component includes comprehensive accessibility features:
- Keyboard Navigation - Space and Enter keys to toggle the switch
- Screen Reader Support - Proper ARIA attributes and role announcements
- Focus Management - Clear focus indicators and logical tab order
- Form Integration - Works seamlessly with form validation and submission
- Label Association - Proper label association with switch elements
- State Announcement - Screen readers announce checked/unchecked states
Common Use Cases
The Switch component is perfect for:
- Settings & Preferences - Theme toggles, notification settings, privacy controls
- Feature Toggles - Enable/disable features, beta access, experimental options
- User Permissions - Access controls, role-based permissions, visibility settings
- Application State - Online/offline mode, auto-save, sync settings
- Content Filters - Show/hide content, filter options, display preferences
- System Configuration - Debug mode, maintenance mode, feature flags
- Accessibility Options - High contrast, reduced motion, screen reader mode
Best Practices
- Clear Labels - Use descriptive labels that clearly explain what the switch controls
- Immediate Feedback - Provide immediate visual feedback when toggled
- Consistent Behavior - Ensure switches behave consistently across your application
- Appropriate Defaults - Choose sensible default states for switches
- Group Related Switches - Organize related switches together in settings panels
- Accessibility - Always associate switches with proper labels using htmlFor/id
- Form Integration - Include switches in form validation and submission logic
- Visual Hierarchy - Use proper spacing and typography to create clear hierarchy