Checkbox

A native checkbox with Harmo's appearance. Pair it with a label in a horizontal Field. For a setting that applies at once, use a Switch.

Installation

bun add @harmo-ui/css @harmo-ui/react

Usage

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

<CheckboxGroup label="Notify me about" name="notify" defaultValue={["messages", "mentions"]}>
  <Field orientation="horizontal" label="New messages">
    <Checkbox value="messages" />
  </Field>
  <Field orientation="horizontal" label="Mentions">
    <Checkbox value="mentions" />
  </Field>
</CheckboxGroup>

Examples

This site is using your theme

States

Unchecked, checked, and mixed, each in a horizontal Field with its label. Mixed is the indeterminate DOM property, which has no HTML attribute: a script sets it, so it doesn't show in the markup.

<>
  <Field orientation="horizontal" label="Email me updates">
    <Checkbox name="email-updates" />
  </Field>
  <Field orientation="horizontal" label="Send a weekly summary">
    <Checkbox name="weekly-summary" defaultChecked />
  </Field>
  <Field orientation="horizontal" label="Select all reports">
    <Checkbox name="all-reports" indeterminate />
  </Field>
</>

Disabled

disabled dims the checkbox in any state, and its label turns gray.

<>
  <Field orientation="horizontal" label="Updates unavailable">
    <Checkbox disabled />
  </Field>
  <Field
    orientation="horizontal"
    label="Summary enabled by your administrator"
  >
    <Checkbox defaultChecked disabled />
  </Field>
  <Field
    orientation="horizontal"
    label="Some reports shared by your administrator"
  >
    <Checkbox disabled indeterminate />
  </Field>
</>

Custom size

--harmo-checkbox-size resizes the box, and the marks scale with it; here 24px instead of 20px.

<>
  <Field orientation="horizontal" label="Send a weekly summary">
    <Checkbox name="large-summary" style={{ "--harmo-checkbox-size": "24px" } as CSSProperties} defaultChecked />
  </Field>
  <Field orientation="horizontal" label="Select all reports">
    <Checkbox name="large-reports" style={{ "--harmo-checkbox-size": "24px" } as CSSProperties} indeterminate />
  </Field>
</>

With a description

.hfield-content holds the label and a description, connected to the checkbox with aria-describedby.

A weekly summary. You can change this at any time.

<Field
  orientation="horizontal"
  label="Email me about activity across my teams"
  description="A weekly summary. You can change this at any time."
>
  <Checkbox name="activity-updates" />
</Field>

Group

Related checkboxes go in a Field with role="group", named by its label.

Notify me about
<CheckboxGroup
  label="Notify me about"
  name="notify"
  defaultValue={["messages", "mentions"]}
>
  <Field orientation="horizontal" label="New messages">
    <Checkbox value="messages" />
  </Field>
  <Field orientation="horizontal" label="Mentions">
    <Checkbox value="mentions" />
  </Field>
  <Field orientation="horizontal" label="Failed campaigns">
    <Checkbox value="failures" />
  </Field>
</CheckboxGroup>

Long label

In a narrow container, drawn dashed here, the label wraps and the checkbox stays beside its first line.

<Field
  orientation="horizontal"
  label="Send me detailed updates about changes to reports and activity across every team in my account"
>
  <Checkbox name="detailed-updates" />
</Field>

API reference

React

<Checkbox> renders the <input type="checkbox">. Put it in a horizontal Field for its label and description. Other props are the input's attributes.

PropEffect
indeterminateShows the mixed state

<CheckboxGroup> renders a Field that is the group, named by its label, and gives its checkboxes one name. Each checkbox in it takes a value.

PropEffect
label, optional, description, errorAs for a Field
nameThe name its checkboxes submit under; generated when left out
value or defaultValueThe checked checkboxes' values, as an array
onValueChangeCalled with the checked values, in the checkboxes' order, when one changes
disabledDisables every checkbox in it

HTML

An <input class="hcheckbox" type="checkbox"> with an id that its <label for> points to, or aria-labelledby. Load checkbox.css; field.css for its label.

HookEffect
checkedChecked
indeterminate (DOM property)Mixed, shown as a dash; wins over checked
disabledDimmed, not-allowed cursor; its Field label turns gray
--harmo-checkbox-size on the elementSize (default 20px); the marks scale with it

A script is only needed for the mixed state, since indeterminate has no HTML attribute. See Checkbox's full contract.

Accessibility

A native checkbox: Space toggles it, and its <label> names it. A group of checkboxes goes in a Field with role="group" labelled by its .hlabel.