Switch
An on/off switch built on a native checkbox with role switch, following the APG Switch pattern.
Live example
Off, on and disabled
<div class="stack" style="--stack-gap: 0">
<label><input type="checkbox" role="switch" name="alerts"> Email alerts</label>
<label><input type="checkbox" role="switch" name="digest" checked> Weekly digest</label>
<label><input type="checkbox" role="switch" name="beta" disabled> Beta features</label>
</div>
In a fieldset
<fieldset>
<legend>Privacy</legend>
<label><input type="checkbox" role="switch" name="profile-public" checked> Public profile</label>
<label><input type="checkbox" role="switch" name="show-email"> Show email address</label>
</fieldset>
Attributes
role="switch" on <input type="checkbox">
| Attribute or value | Where | Effect |
|---|---|---|
role="switch" | <input type="checkbox"> | no class needed. |
- Keep the label the same in both states; the state is on or off.
- Wrap the input in its
<label>so the whole label is the click target.
- Our choice: the switch is the native checkbox with
role="switch", drawn withappearance: noneand one pseudo-element. The thumb moves and changes color, and forced-colors mode gets explicit system colors. - Our choice: icons are Lucide SVGs pasted inline, with no icon files, sprite, font or package in the repository, because Lucide already draws in our 24px, 2px,
currentColorstroke and inline SVG needs no build and follows forced colors. The hook isdata-iconon the<svg>, the same one-attribute rule as every atom, rather thansvg[aria-hidden], which would also style meaningfulrole="img"graphics and tie the look to an accessibility attribute. The rules for when to use an icon are in PRINCIPLES.md.
Motion
The track color fades over --dui-motion-duration-fast; the thumb slides for that duration times --dui-motion-movement, so with reduced motion it jumps and the color change stays. The state itself never waits on the transition.
Accessibility
- The native checkbox brings focus, Space to toggle, form submission and
:checked.role="switch"makes assistive technology announce on and off (APG Switch, ARIA in HTML). - State is not color alone: the thumb moves and changes color, and the track fills (WCAG 1.4.1).
- Track border and thumb have 3:1 contrast with the surface (WCAG 1.4.11).
- In forced colors, system colors keep both states visible.
Specifications
- APG Switch
- ARIA in HTML
- MDN
switchrole - ui.shadcn.com switch
- MDN
forced-colors - WCAG Use of color
- WCAG Non text contrast
See also
- Atoms
- Component API convention
- Accessibility baseline
- Source:
css/components/atoms/switch.css,examples/atoms/switch.html
How other systems do it
For comparison only; these are not the standard this system follows.