Switch

Binary toggle styled as a slider.

Also known as:
  • Slide toggle
  • Toggle switch
  • ToggleSwitch
  • Toggle Button
  • Binary Switch
  • On/Off Switch
  • State Toggle

Overview

Purpose

The Switch component binary toggle styled as a slider.within our design system. It follows our primitive layer principles, ensuring consistency and reusability across applications.

When to Use

  • Use when you need collecting user input
  • Appropriate for inputs contexts
  • Follows primitive component patterns

When Not to Use

  • Avoid when simpler alternatives exist
  • Don't use for display-only content
  • Consider alternatives for edge cases

Live Example

import React from 'react';
import Switch from './components/Switch';
import props from '/props.json';

export default function App() {
  return (
    <div style={{ padding: '2rem', display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: '100vh' }}>
      <Switch
        checked={props.checked || false}
        size={props.size || 'medium'}
        disabled={props.disabled || false}
      >
        {props.children || 'Toggle Switch'}
      </Switch>
    </div>
  );
}

Anatomy

Understanding the structure of the Switch component helps ensure proper implementation and customization.

Anatomy data not yet defined for this component. Define anatomy in the component contract file (Switch.contract.json).

Variants & States

import React from 'react';
import Switch from './components/Switch';
import props from '/props.json';

export default function App() {
  return (
    <div style={{ padding: '2rem', display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: '100vh' }}>
      <Switch
        checked={props.checked || false}
        size={props.size || 'medium'}
        disabled={props.disabled || false}
      >
        {props.children || 'Toggle Switch'}
      </Switch>
    </div>
  );
}

API Reference

Props

NameTypeRequiredDefaultDescription
sizeSwitchSizeNo'md'
checkedbooleanNo{isControlled ? checked : undefined
defaultCheckedbooleanNo{!isControlled ? defaultChecked : undefined
disabledbooleanNofalse
onChange(event: React.ChangeEvent<HTMLInputElement>) => voidNo{onChange
classNamestringNo''
idstringNoprovidedId || generatedId

Accessibility

Standards Compliance

This component follows WCAG 2.1 AA guidelines and includes proper ARIA attributes, keyboard navigation, and screen reader support.

Common Pitfalls

  • Misused as yes/no
  • unclear semantics.

Accessibility Checklist

  • Keyboard navigation support
  • Screen reader compatibility
  • Color contrast compliance
  • Focus management
  • ARIA attributes
  • Reduced motion support

Usage Guidelines

✓ Do

  • Use consistent spacing and sizing
  • Follow established patterns
  • Provide clear labels and descriptions
  • Test with assistive technologies

✗ Don't

  • Override core functionality
  • Use without proper context
  • Ignore accessibility requirements
  • Modify without design system approval

Examples

Basic Usage

Simple implementation with default props and common configurations.

import React from 'react';
import Switch from './components/Switch';
import props from '/props.json';

export default function App() {
  return (
    <div style={{ padding: '2rem', display: 'flex', alignItems: 'center', justifyContent: 'center', minHeight: '100vh' }}>
      <Switch
        checked={props.checked || false}
        size={props.size || 'medium'}
        disabled={props.disabled || false}
      >
        {props.children || 'Toggle Switch'}
      </Switch>
    </div>
  );
}

Advanced Usage

Complex patterns including composition, state management, and real-world scenarios.

import React from 'react';

export default function App() {
  return (
    <div style={{ 
      padding: '2rem', 
      display: 'flex',
      flexDirection: 'column',
      gap: '1.5rem'
    }}>
      <h3>Switch - Advanced Patterns</h3>
      <p>Advanced usage patterns for Switch include:</p>
      <ul>
        <li>Composition with other components</li>
        <li>Custom styling and theming</li>
        <li>Controlled vs uncontrolled patterns</li>
        <li>Form integration</li>
        <li>Accessibility enhancements</li>
      </ul>
      <p style={{ color: '#6b7280', fontStyle: 'italic' }}>
        Full advanced examples coming soon.
      </p>
    </div>
  );
}

Development Tools

Use these tools to analyze the component's performance, design tokens, and accessibility during development.

Contribute

Help us improve the Switch component documentation. Found an issue or have suggestions?

Related Components