Switch Overview

The switch component is a toggle control that allows users to quickly turn an option on or off. Unlike checkboxes which are for selecting multiple options, switches are specifically designed for immediate state changes and binary choices. The component provides clear visual feedback for the current state and supports various sizes, validation states, and accessibility features.

When to Use

Use switches when:

  • Controlling system settings or preferences (enable/disable features)
  • Toggling between two distinct states with immediate effect
  • Turning services, notifications, or modes on/off
  • Managing visibility or availability of features
  • Creating compact toggle controls in mobile interfaces
  • Implementing preference panels and configuration screens

Don't use switches for:

  • Selecting multiple items from a list (use checkboxes instead)
  • Single-choice selection from multiple options (use radio buttons)
  • Form submissions where immediate action isn't intended
  • Complex state changes requiring confirmation
  • Navigation or triggering actions (use buttons instead)
  • When the "off" state isn't meaningful or clear
Design system docs Examples Basic Usage

Simple switches for immediate state toggles with clear labels:

Sizes & Scaling

Choose switch sizes based on your interface density and importance:

Size variations
States & Feedback

Switches support various states to communicate different conditions:

Interactive states
Validation states
Label Positioning & Descriptions

Flexible label and description placement for different layout requirements:

Label positioning
Description placement
Switch Groups

Use input groups to organize related switches with shared context:

Privacy settings
System configuration
Background & Border Variants

Special styling options for enhanced visibility and emphasis:

Background variant
Border emphasis
Real-World Patterns Application Settings Panel
Application Settings Save Settings Reset to Defaults
Notification Preferences
Notification Preferences Update Preferences Test Notifications
Feature Toggle Dashboard
Feature Management Enable or disable features for your account. Changes take effect immediately.
Features enabled: 3 of 5 View Billing Upgrade Plan
Accessibility & Keyboard Navigation

The switch component is built with accessibility as a core principle:

Keyboard Support
  • Space: Toggle switch state
  • Enter: Alternative toggle (custom enhancement)
  • Tab: Navigate between switches
  • Shift + Tab: Navigate backwards
Screen Reader Support
  • Proper ARIA role (switch) for clear identification
  • State announcements (on, off) with proper labels
  • Form association and validation messages
  • Group labeling for related switches
Focus Management
  • Clear focus indicators that respect user preferences
  • Logical tab order following visual layout
  • Programmatic focus control for dynamic interfaces
Accessibility Examples
Best Practices When to Use Switches vs. Checkboxes
  • Use switches for immediate state changes and system preferences
  • Use checkboxes for form selections and multi-item choices
  • Consider the context: switches imply immediate action, checkboxes imply selection
Visual Design
  • Maintain consistent sizing within related switch groups
  • Use background variants on colored surfaces for better contrast
  • Consider border emphasis for critical or high-impact settings
  • Test with different themes and accessibility settings
  • Ensure sufficient color contrast for all states
User Experience
  • Provide clear, descriptive labels that explain the effect
  • Use descriptions to clarify what happens when toggled
  • Group related switches with input groups
  • Consider the cognitive load of too many switches
  • Make the consequences of each switch state clear
  • Test with real users and assistive technologies
Technical Implementation
  • Always provide meaningful value attributes for form handling
  • Implement proper validation for required switches
  • Use appropriate feedback types for different validation states
  • Consider the immediate vs. deferred action patterns
  • Ensure switches work properly in forms and with validation
Importing
import { DapDSSwitch } from 'dap-design-system'
Importing React
import { DapDSSwitchReact } from 'dap-design-system/react'
Tree-Shakeable Imports

For optimal bundle sizes, use the tree-shakeable import syntax:

import { DapDSSwitch } from 'dap-design-system/components'
Attributes
PropertyTypeDefaultDescription
borderbooleanfalseThis sets up border around the switch, when true.
type'normal', 'background''normal'The type of the switch
feedbackIdfeedback-${DapDSSwitch.nextId}-${uniqueSuffix}
namestringThe name of the switch.
valuestringThe value of the switch.
labelstringThe label of the switch.
descriptionstringThe description of the switch.
checkedbooleanThe checked state of the switch.
size'xs', 'sm' , 'lg'The size of the switch.
disabledbooleanThe disabled state of the switch.
readonlybooleanWhether the switch is readonly (cannot be changed but value is submitted with form).
requiredbooleanThe required state of the switch.
labelPlacement'left', 'right'The placement of the label.
descriptionPlacement'top', 'bottom'The placement of the description.
subtlebooleanThe weight of the label.
feedbackstringThe feedback of the switch.
feedbackType'negative', 'positive' , 'warning'The feedback type of the switch.
optionalbooleanThe optional state of the switch.
optionalLabelstringThe optional label of the switch.
Slots

No slots available.

Events
Event NameDescriptionType
dds-changeFired when the input value changes.{value: string }
dds-blurEmitted when the input loses focus.{void }
dds-focusEmitted when the input gains focus.{void }
dds-inputEmitted when the input receives input.{value: string }
CSS Parts
Part NameDescription
baseThe main switch container.
baselabelThe main label container
labelThe label of the switch.
inputThe native input of the switch.
controlThe control of the switch.
descriptionThe description of the switch.
How to Use CSS Parts

You can style CSS parts using the ::part() pseudo-element selector:

/* Target a specific part */
.my-custom-dap-ds-switch::part(base) {
  /* Your custom styles */
}

/* Target multiple parts */
.my-custom-dap-ds-switch::part(base),
.my-custom-dap-ds-switch::part(baselabel) {
  /* Shared styles */
}

Example usage:

<dap-ds-switch class="my-custom-dap-ds-switch">
  Switch
</dap-ds-switch>
.my-custom-dap-ds-switch::part(base) {
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

CSS parts allow you to style internal elements of the component while maintaining encapsulation. Learn more in our styling guide.

CSS Custom Properties

No CSS custom properties available.