Switch
Switch lets users choose between two opposing states, applying the change immediately with no submit action needed.
Build switches from native checkbox inputs or buttons with role="switch"; button switches must manage aria-checked.
Show code
<label class="flex-row gap-x-16 items-center justify-between"> Enable notifications <input type="checkbox" role="switch" /></label>Elements
Section titled “Elements”A switch can be built with either an <input type="checkbox"> or a <button>. Both require role="switch".
Checkbox input
Section titled “Checkbox input”The simplest approach: the browser manages checked state natively. No JavaScript needed for basic usage.
Button
Section titled “Button”Use a <button type="button"> when you need full control over toggling. You must manage aria-checked yourself.
Grouping and layout
Section titled “Grouping and layout”Wrap related switches in a flex-col gap-16 container. Place the label first and the switch after, using justify-between to push them apart.
Target size
Section titled “Target size”The switch has a minimum target size of 24px.
Accessibility
Section titled “Accessibility”Key responsibilities from the WCAG audit:
- Always set
role="switch": required for screen readers to announce the element as a switch instead of a checkbox. - Always provide a label: wrap the switch in a
<label>, or usearia-label/aria-labelledby. - Manage
aria-checkedon buttons: for<button>switches, togglearia-checkedbetween"true"and"false". Checkbox inputs handle this natively. - No unexpected side effects: toggling a switch should not cause disorienting changes elsewhere on the page without notifying the user (WCAG 3.2.2, flagged for attention).
- Keyboard: focusable via Tab, toggled with Space (checkbox) or Space/Enter (button).
- Avoid disabled switches: disabled switches are hidden from screen readers. Prefer explaining why a setting is unavailable instead.