Checkbox

Slant / Forms

A checkbox with an indeterminate state and a card variant.

Demo

Usage

@tuttn/slant
import { Checkbox, Field } from '@tuttn/slant'

// Controlled: checked and onCheckedChange.
const [autosave, setAutosave] = useState(true)
<Checkbox checked={autosave} onCheckedChange={setAutosave}>Auto-save drafts</Checkbox>

// 'indeterminate' draws the dash, for a parent box over a partly checked list.
// A click on it reports true.
const all = picked.length === files.length ? true : picked.length > 0 ? 'indeterminate' : false
<Checkbox checked={all} onCheckedChange={next => setPicked(next ? files : [])}>Attach all files</Checkbox>

// Uncontrolled: it keeps its own state from defaultChecked. A description sits
// under the label, and the card variant makes the whole row the target.
<Checkbox defaultChecked variant="card" description="Kept for 30 days">Daily snapshots</Checkbox>

// In a form it submits its value ("on" unless you set one) under name while checked.
<Checkbox name="sync" value="yes" defaultChecked>Sync</Checkbox>

// In a Field, the box takes the field's description, error and states; its text still names it.
<Field invalid={!agreed} required>
  <Checkbox checked={agreed} onCheckedChange={setAgreed} name="terms">I agree to the terms</Checkbox>
  <Field.Error>Agree to the terms to continue.</Field.Error>
</Field>

Props

PropTypeDefaultDescription
checkedboolean | 'indeterminate'—Whether the box is checked, or `'indeterminate'` for a parent box whose children are partly checked. Pass it to control the box; leave it out to let the box keep its own state. Inside a CheckboxGroup the group's value decides.
defaultCheckedboolean | 'indeterminate'falseThe state an uncontrolled box starts in.
onCheckedChange(checked: boolean) => void—Called with the next state when a click or Space toggles the box. An indeterminate box reports `true`.
childrenReactNode—The visible label beside the box, which names it.
descriptionReactNode—A line of hint text under the label, read as the box's description.
variant'default' | 'card''default', or the group's`default` is a box and a label; `card` is a bordered row that is one big click target and fills with the accent while checked.
size'sm' | 'md''md', or the group's`md` is a 16px box with 13px text; `sm` a 14px box with 12px text.
invalidboolean—Sets aria-invalid and a danger border. Inside a Field, the field's `invalid` does this.
…restInputHTMLAttributes<HTMLInputElement>—name, value, form, required, aria-* and the other input attributes, and the ref, reach the native checkbox. className and style go on the <label> around it.

Keyboard

Tabmoves to the box; a disabled box is skipped
Spacetoggles it; an indeterminate box becomes checked

Accessibility

  • The box is a native <input type="checkbox">, visually hidden inside a <label>, so screen readers, form validation and form data treat it as any checkbox. The drawn square and its check are aria-hidden, and the focus ring shows on the square.
  • The children name the box and the description describes it (aria-describedby), so a long hint isn't read as part of the name. Without children, name the box with aria-label.
  • checked="indeterminate" sets the input's indeterminate property, which screen readers announce as mixed. A controlled caller that keeps it indeterminate after a click keeps the dash.
  • In a Field, one box takes the field's id, its description and error, aria-invalid and required. Its own text still names it.

A checkbox is a choice the form sends later. For a setting that applies the moment it changes, use a switch; for one choice out of several, a radio group; for several boxes with one value, a checkbox group.