# Switch

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

_Diagram: 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

```html
<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

```html
<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 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](https://lucide.dev) 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](https://domestic-ui.odonckers.dev/principles/opinions-we-hold.md#icons).

### 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, <kbd>Space</kbd> to toggle, form submission and `:checked`. `role="switch"` makes assistive technology announce on and off ([APG Switch](https://www.w3.org/WAI/ARIA/apg/patterns/switch/), [ARIA in HTML](https://www.w3.org/TR/html-aria/#el-input-checkbox)).
- State is not color alone: the thumb moves and changes color, and the track fills ([WCAG 1.4.1](https://www.w3.org/WAI/WCAG22/Understanding/use-of-color.html)).
- Track border and thumb have 3:1 contrast with the surface ([WCAG 1.4.11](https://www.w3.org/WAI/WCAG22/Understanding/non-text-contrast.html)).
- In [forced colors](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/forced-colors), system colors keep both states visible.

## Specifications

- [APG Switch](https://www.w3.org/WAI/ARIA/apg/patterns/switch/)
- [ARIA in HTML](https://www.w3.org/TR/html-aria/#el-input-checkbox)
- [MDN `switch` role](https://developer.mozilla.org/en-US/docs/Web/Accessibility/ARIA/Reference/Roles/switch_role)
- [ui.shadcn.com switch](https://ui.shadcn.com/docs/components/switch)
- [MDN `forced-colors`](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/forced-colors)
- [WCAG Use of color](https://www.w3.org/WAI/WCAG22/Understanding/use-of-color.html)
- [WCAG Non text contrast](https://www.w3.org/WAI/WCAG22/Understanding/non-text-contrast.html)

## See also

- [Atoms](https://domestic-ui.odonckers.dev/atoms/index.md)
- [Component API convention](https://domestic-ui.odonckers.dev/index.md#component-api)
- [Accessibility baseline](https://domestic-ui.odonckers.dev/principles/accessibility-baseline-wcag-22-aa.md)
- Source: [`css/components/atoms/switch.css`](https://github.com/odonckers/domestic-ui/blob/main/css/components/atoms/switch.css), [`examples/atoms/switch.html`](https://github.com/odonckers/domestic-ui/blob/main/examples/atoms/switch.html)

## How other systems do it

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

- [Apple Human Interface Guidelines: Toggles](https://developer.apple.com/design/human-interface-guidelines/toggles)
- [Material Design 3: Switch](https://m3.material.io/components/switch/overview)
- [Vercel Geist: Switch](https://vercel.com/geist/switch)
