Skip to main content

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. An input will also render when used within a form to ensure events propagate correctly.
boolean
The controlled checked state of the switch.
boolean
The checked state when initially rendered (uncontrolled).
(checked: boolean) => void
Event handler called when the checked state changes.
boolean
When true, prevents the user from interacting with the switch.
boolean
When true, indicates that the user must check the switch before the form can be submitted.
string
The name of the switch. Submitted with its owning form as part of a name/value pair.
string
default:"'on'"
The value given as data when submitted with a name.
boolean
default:"false"
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.
boolean
default:"false"
Change the default rendered element for the one passed as a child.

Example

Accessibility

Keyboard Interactions

  • Space - Toggles the switch.

Data Attributes

Root
  • [data-state] - “checked” or “unchecked”
  • [data-disabled] - Present when disabled
Thumb
  • [data-state] - “checked” or “unchecked”
  • [data-disabled] - Present when disabled