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/react

Usage

import { Field, Switch } from "@harmo-ui/react";

<Field orientation="horizontal" label="Play a sound for new messages">
  <Switch defaultChecked />
</Field>

Examples

This site is using your theme

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>
</>

Disabled

disabled dims the switch, and its label turns gray.

<>
  <Field orientation="horizontal" label="Translate incoming messages">
    <Switch disabled />
  </Field>
  <Field
    orientation="horizontal"
    label="Two-factor sign-in, required by your administrator"
  >
    <Switch defaultChecked disabled />
  </Field>
</>

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>
</>

Custom size

--harmo-switch-height, --hswitch-width, and --hswitch-thumb resize the track and thumb; here 36×20 with a 16px thumb.

<Field orientation="horizontal" label="Compact conversation list">
  <Switch
    style={
      {
        "--harmo-switch-height": "20px",
        "--hswitch-width": "36px",
        "--hswitch-thumb": "16px",
      } as CSSProperties
    }
    defaultChecked
  />
</Field>

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.

HookEffect
checkedOn
disabledDimmed; the Field label turns gray
--hswitch-width, --hswitch-thumb, --hswitch-inset on the inputTrack width (44px), thumb size (20px), and the thumb's inset (2px)
--harmo-switch-heightTrack 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.