Components

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

Do
  • 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 CheckboxGroup with a parent for 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”.
Don’t
  • 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.

PropTypeDefault
checked
Whether it’s checked. Pass it to control the checkbox.
boolean | 'indeterminate'—
defaultChecked
Whether it starts checked, when uncontrolled.
boolean | 'indeterminate'false
name
The name it submits under.
string—
value
What it submits while checked, and its value in a group.
string'on'
parent
Checks or clears every checkbox in its group.
booleanfalse
disabled
Can’t be focused or changed, and isn’t submitted.
booleanfalse
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

CheckboxLabel

A <label> that lays a Checkbox or Switch beside its text, 8px apart.

PropTypeDefault
sx
StyleX styles merged after the built-in ones.
StyleXStyles—

CheckboxGroup

Wraps ui-sdk’s CheckboxGroup in a column and takes its props.

PropTypeDefault
value
The checked values. Pass it to control the group.
string[]—
defaultValue
The values checked at first.
string[]—
onValueChange
Runs when a checkbox in the group changes.
(value: string[], details) => void—
allValues
Every value the parent checks, for its first render.
string[]—
name
The name every checked value submits under.
string—
sx
StyleX styles merged after the built-in ones.
StyleXStyles—