Skip to main content

Switch

StorybookView 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​

Preferences

Receive notifications about updates

Use dark theme interface

Automatically save your work

Help improve our service

Receive product updates and offers

Feature Toggles​

Feature Flags

Access experimental features

Show advanced options and controls

Enable debugging information

Try our new interface design

Disabled State​

These switches are disabled due to insufficient permissions:

Form Integration​

Notification Preferences

Properties​

Switch​

PropertyTypeDefaultDescription
checkedboolean-The controlled checked state of the switch.
defaultCheckedbooleanfalseThe default checked state for uncontrolled usage.
onCheckedChange(checked: boolean) => void-Callback when the checked state changes.
disabledbooleanfalseWhether the switch is disabled.
requiredbooleanfalseWhether the switch is required for form submission.
namestring-The name attribute for form submission.
valuestring"on"The value attribute for form submission.
idstring-The HTML id attribute.
classNamestring-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​

  1. Clear Labels - Use descriptive labels that clearly explain what the switch controls
  2. Immediate Feedback - Provide immediate visual feedback when toggled
  3. Consistent Behavior - Ensure switches behave consistently across your application
  4. Appropriate Defaults - Choose sensible default states for switches
  5. Group Related Switches - Organize related switches together in settings panels
  6. Accessibility - Always associate switches with proper labels using htmlFor/id
  7. Form Integration - Include switches in form validation and submission logic
  8. Visual Hierarchy - Use proper spacing and typography to create clear hierarchy