Checkbox
A box that checks and unchecks, alone or in a group with a select-all parent.
Usage
import { Checkbox, CheckboxLabel } from 'nyctoui/checkbox'
<CheckboxLabel>
<Checkbox name="git" defaultChecked />
Collect git metadata
</CheckboxLabel>The box is 16px with a 4px corner and fills with the brand ink when checked. CheckboxLabel is a <label> that lays the box beside its text, so a click on the text toggles it. Without visible text, give the checkbox an aria-label.
The checkbox renders a button with the checkbox role, and a hidden native checkbox, so name, value, required and form reset work as they do on an <input type="checkbox">.
Good for
- Accepting terms or opting into a newsletter on a sign-up form.
- Choosing which columns to export from a report.
- Selecting rows in an email inbox or a file list, with a select-all at the top.
- Picking dietary options or toppings in a food ordering app.
Examples
States
defaultChecked="indeterminate" shows a dash for a partly checked set. A press checks it; only your code puts it back into the mixed state.
With a description
In a Field, the label and description are wired to the checkbox. Lay the field out as a row and nudge the box down 2px to center it on the label’s first line.
Group with select all
CheckboxGroup keeps one list of checked values. A checkbox with parent checks or clears them all, and shows the mixed state in between. Pass allValues so the parent is right on the first render.
Keyboard
Space toggles the focused checkbox. Enter submits its form and never toggles it, as on a native checkbox.
Best practices
- Give every checkbox a visible label with
CheckboxLabel, written as what happens when it’s checked. - Add a description under a label that needs explaining, like what “Share usage data” actually shares.
- Use a
CheckboxGroupwith aparentfor select all; it shows the mixed state when only some are checked. - Name the group for what the choices are: “Export formats” says more than “Options”.
- Use checkboxes when only one option can be picked. Use a segmented control or a select.
- Use a checkbox for a setting that applies at once, with no Save. That’s a switch.
- List more than about seven choices as checkboxes. A longer set needs search, such as a tag input with suggestions.
API reference
Checkbox
Wraps ui-sdk’s Checkbox and its indicator, and takes its props, including onCheckedChange, readOnly, required and controller.
| Prop | Type | Default |
|---|---|---|
checkedWhether it’s checked. Pass it to control the checkbox. | boolean | 'indeterminate' | — |
defaultCheckedWhether it starts checked, when uncontrolled. | boolean | 'indeterminate' | false |
nameThe name it submits under. | string | — |
valueWhat it submits while checked, and its value in a group. | string | 'on' |
parentChecks or clears every checkbox in its group. | boolean | false |
disabledCan’t be focused or changed, and isn’t submitted. | boolean | false |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CheckboxLabel
A <label> that lays a Checkbox or Switch beside its text, 8px apart.
| Prop | Type | Default |
|---|---|---|
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |
CheckboxGroup
Wraps ui-sdk’s CheckboxGroup in a column and takes its props.
| Prop | Type | Default |
|---|---|---|
valueThe checked values. Pass it to control the group. | string[] | — |
defaultValueThe values checked at first. | string[] | — |
onValueChangeRuns when a checkbox in the group changes. | (value: string[], details) => void | — |
allValuesEvery value the parent checks, for its first render. | string[] | — |
nameThe name every checked value submits under. | string | — |
sxStyleX styles merged after the built-in ones. | StyleXStyles | — |