Overview
Switch provides a binary control that users can toggle on or off. It’s similar to a checkbox but styled to look like a physical toggle switch.Features
- Can be controlled or uncontrolled
- Full keyboard navigation
- Works inside forms
- Accessible by default with proper ARIA attributes
Installation
Anatomy
API Reference
Root
Contains all the switch component parts. Aninput will also render when used within a form to ensure events propagate correctly.
The controlled checked state of the switch.
The checked state when initially rendered (uncontrolled).
Event handler called when the checked state changes.
When true, prevents the user from interacting with the switch.
When true, indicates that the user must check the switch before the form can be submitted.
The name of the switch. Submitted with its owning form as part of a name/value pair.
The value given as data when submitted with a name.
Change the default rendered element for the one passed as a child.
Thumb
The thumb that is used to visually indicate whether the switch is on or off.Change the default rendered element for the one passed as a child.
Example
Accessibility
Adheres to the Switch WAI-ARIA design pattern.
Keyboard Interactions
- Space - Toggles the switch.
Data Attributes
Root[data-state]- “checked” or “unchecked”[data-disabled]- Present when disabled
[data-state]- “checked” or “unchecked”[data-disabled]- Present when disabled