Switch
Terminal-styled binary toggle switch. Uses role="switch" with checked/defaultChecked/onChange(boolean) pattern and a hidden native checkbox for form submission.
Installation
dgadd ships in @diffgazer/add on npm: pnpm add -D @diffgazer/add puts it on pnpm exec, and npx @diffgazer/add runs it without the install.
UI components require Tailwind CSS v4. Local copy mode imports src/styles/styles.css; package mode uses @diffgazer/ui CSS from the installed package.
Usage
Examples
Sizes and States
Controlled
API Reference
Switch
Data attributes
Accessibility
Keyboard Navigation
Switch renders a native button with role=switch, so keyboard activation follows button semantics.
| Key | Action |
|---|---|
| Space | Toggles the checked state when the switch is focused. |
| Enter | Toggles the checked state when the switch is focused. |
Controlled toggle
Notes
State Glyph
The thumb renders a binary glyph — "1" when checked, "0" when unchecked — so the on/off state stays readable without relying on track inversion or thumb position. The glyph is aria-hidden; role="switch" plus aria-checked carries the state for assistive technology.
Labelled Row
Pass label (and optionally description) to render the same row grammar Checkbox and Radio use: track on the left, label on the right, one shared gap and one shared 44px coarse-pointer height. The label is wired as the accessible name through aria-labelledby, the description through aria-describedby, and clicking either forwards to the control. An explicit aria-label still wins. With neither prop the render is unchanged, so hand-rolled rows keep working.
Disabled Treatment
A disabled switch dashes its track border and resolves the thumb to the recessed surface with a muted digit — the same disabled grammar Input and Textarea use. It is not an opacity fade: forced-colors mode drops opacity but keeps border-style, so the state stays visible in high-contrast themes.
Form Submission
Switch renders a hidden native checkbox when name or required is set. The value prop controls the form-submission string (default "on"). Pass form to associate both the switch and its form mirror with a remote form.
Source
Install via CLI: pnpm exec dgadd add ui/switch. Keyboard hooks are included as standalone copies. For the full experience, use --integration keys.
Highlighted source loads after this disclosure opens. Browse the source repository.