Switch

A toggle control for boolean settings. Uses role='switch' for accessibility and supports both controlled and uncontrolled patterns.

Preview

Alerts for new messages and mentions.

Sizes

SizeTrackThumb
sm28 × 16px12 × 12px
md36 × 20px14 × 14px
lg44 × 24px18 × 18px

Motion

PropertyValueEasing
thumb translate-x±(track width - thumb - 2×offset)200ms ease
thumb backgroundtertiary → white200ms ease
track backgroundsurface-raised → accent200ms ease

Implementation

Switch.tsx
tsx
import { Switch } from "@/components/ui/Switch";
import { useState } from "react";

// Uncontrolled
<Switch label="Dark mode" defaultChecked />

// Controlled
const [enabled, setEnabled] = useState(false);
<Switch
  label="Enable notifications"
  helperText="Push alerts for new messages and mentions."
  checked={enabled}
  onChange={(e) => setEnabled(e.target.checked)}
/>

// Disabled
<Switch label="Beta features" disabled defaultChecked />
<Switch label="Legacy mode" disabled />

Props

All native <input type="checkbox"> attributes are forwarded. The switch renders as role="switch".

PropTypeDefaultDescription
labelstringVisible label rendered beside the switch track.
helperTextstringSupplemental hint rendered below the label.
size"sm" | "md" | "lg""md"Controls the track and thumb dimensions.
checkedbooleanControlled on/off state.
defaultCheckedbooleanfalseInitial state for uncontrolled usage.
disabledbooleanfalsePrevents interaction and dims the control.
onChange(e: React.ChangeEvent<HTMLInputElement>) => voidCalled when the switch changes state.

ARIA roles

ElementRoleKey attributes
Inputswitcharia-checked (true/false), aria-labelledby, aria-describedby

Keyboard

KeyAction
SpaceToggle the switch on or off
TabMove focus to the next focusable element
Shift+TabMove focus to the previous focusable element

Accessibility

  • Uses role="switch" — screen readers announce the state as "on" or "off", not "checked" or "unchecked".
  • The underlying input is sr-only; the visual track and thumb are purely presentational.
  • Keyboard: Space toggles the switch when focused.
  • Focus ring appears via focus-visible — keyboard only.
  • Disabled state sets disabled on the input and visually dims the control.
  • Prefer Switch over Checkbox for settings that take immediate effect (dark mode, airplane mode). Use Checkbox for form fields that require explicit submission.