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/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 checkboxnpx harmo-ui add checkboxpnpm dlx harmo-ui add checkboxyarn dlx harmo-ui add checkboxUsage
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
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.
Disabled
disabled dims the checkbox in any state, and its label turns gray.
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.
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><div class="hfield" data-orientation="horizontal">
<input class="hcheckbox" type="checkbox" id="detailed-updates" name="detailed-updates" />
<label class="hlabel" for="detailed-updates">
Send me detailed updates about changes to reports and activity across every team in my account
</label>
</div>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.
| Prop | Effect |
|---|---|
indeterminate | Shows 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.
| Prop | Effect |
|---|---|
label, optional, description, error | As for a Field |
name | The name its checkboxes submit under; generated when left out |
value or defaultValue | The checked checkboxes' values, as an array |
onValueChange | Called with the checked values, in the checkboxes' order, when one changes |
disabled | Disables 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.
| Hook | Effect |
|---|---|
checked | Checked |
indeterminate (DOM property) | Mixed, shown as a dash; wins over checked |
disabled | Dimmed, not-allowed cursor; its Field label turns gray |
--harmo-checkbox-size on the element | Size (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.