domestic-ui GitHub

Switch

An on/off switch built on a native checkbox with role switch, following the APG Switch pattern.

Where switches are used Switches sit at the start of each row in a settings fieldset, each turning one option on or off, with its label beside it.

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 valueWhereEffect
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 with appearance: none and 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, currentColor stroke and inline SVG needs no build and follows forced colors. The hook is data-icon on the <svg>, the same one-attribute rule as every atom, rather than svg[aria-hidden], which would also style meaningful role="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

See also

How other systems do it

For comparison only; these are not the standard this system follows.

Search