Switch
An on/off switch for a setting that applies at once. On settings screens, put it in a Settings list; in a form, pair it with a label in a horizontal Field. For a choice that applies on submit, use a Checkbox.
Installation
bun add @harmo-ui/css @harmo-ui/reactnpm install @harmo-ui/css @harmo-ui/reactpnpm add @harmo-ui/css @harmo-ui/reactyarn add @harmo-ui/css @harmo-ui/reactbunx harmo-ui add switchnpx harmo-ui add switchpnpm dlx harmo-ui add switchyarn dlx harmo-ui add switchUsage
import { Field, Switch } from "@harmo-ui/react";
<Field orientation="horizontal" label="Play a sound for new messages">
<Switch defaultChecked />
</Field>Examples
Off and on
A checkbox with role="switch", for a setting that applies at once. Screen readers announce it as a switch, and Space toggles it.
<>
<Field orientation="horizontal" label="Set me as away">
<Switch />
</Field>
<Field orientation="horizontal" label="Play a sound for new messages">
<Switch defaultChecked />
</Field>
</><div class="hfield" data-orientation="horizontal">
<input class="hswitch" type="checkbox" role="switch" id="away" />
<label class="hlabel" for="away">Set me as away</label>
</div>
<div class="hfield" data-orientation="horizontal">
<input class="hswitch" type="checkbox" role="switch" id="message-sound" checked />
<label class="hlabel" for="message-sound">Play a sound for new messages</label>
</div>Disabled
disabled dims the switch, and its label turns gray.
<div class="hfield" data-orientation="horizontal">
<input class="hswitch" type="checkbox" role="switch" id="auto-translate" disabled />
<label class="hlabel" for="auto-translate">Translate incoming messages</label>
</div>
<div class="hfield" data-orientation="horizontal">
<input class="hswitch" type="checkbox" role="switch" id="two-factor" checked disabled />
<label class="hlabel" for="two-factor">Two-factor sign-in, required by your administrator</label>
</div>With a description
.hfield-content holds the label and a description; the switch stays centered on the label's first line.
Customers see when you've read their messages.
<Field
orientation="horizontal"
label="Send read receipts"
description="Customers see when you've read their messages."
>
<Switch defaultChecked />
</Field>Right to left
The same switches in Arabic: the thumb travels toward the end of the line, so on is at the left.
<>
<Field orientation="horizontal" label="تعيين حالتي كغائب">
<Switch />
</Field>
<Field orientation="horizontal" label="تشغيل صوت للرسائل الجديدة">
<Switch defaultChecked />
</Field>
</><div class="hfield" data-orientation="horizontal">
<input class="hswitch" type="checkbox" role="switch" id="away-ar" />
<label class="hlabel" for="away-ar">تعيين حالتي كغائب</label>
</div>
<div class="hfield" data-orientation="horizontal">
<input class="hswitch" type="checkbox" role="switch" id="message-sound-ar" checked />
<label class="hlabel" for="message-sound-ar">تشغيل صوت للرسائل الجديدة</label>
</div>Custom size
--harmo-switch-height, --hswitch-width, and --hswitch-thumb resize the track and thumb; here 36×20 with a 16px thumb.
API reference
React
<Switch> renders the <input type="checkbox" role="switch">. Put it in a horizontal Field for its label and description, or in a Setting. Its props are the input's attributes, such as checked, defaultChecked, and onChange.
HTML
An <input class="hswitch" type="checkbox" role="switch">, named by a <label for> or aria-labelledby. The input is the track and its ::before the thumb. Load switch.css; field.css for its label. It needs no script.
| Hook | Effect |
|---|---|
checked | On |
disabled | Dimmed; the Field label turns gray |
--hswitch-width, --hswitch-thumb, --hswitch-inset on the input | Track width (44px), thumb size (20px), and the thumb's inset (2px) |
--harmo-switch-height | Track height (24px); a horizontal Field centers the switch by it |
See Switch's full contract.
Accessibility
A native checkbox with role="switch": screen readers announce "switch, on" or "off", and Space toggles it. Its <label> names it, or in a Settings list the setting's title through aria-labelledby.