Checkbox

A checkbox on a <span role=checkbox> with a hidden native input beside it for forms.

WAI-ARIA pattern

Installation

npx nuecrea-ui add checkbox

Usage

import * as Checkbox from '@/components/ui/checkbox';
import * as Field from '@/components/ui/field';

const label = 'flex items-center gap-2 text-sm';

export default function CheckboxStory() {
  return (
    <main className="grid max-w-sm gap-4">
      <label className={label}>
        <Checkbox.Root>
          <Checkbox.Indicator />
        </Checkbox.Root>
        Email me product updates
      </label>
      <label className={label}>
        <Checkbox.Root defaultChecked>
          <Checkbox.Indicator />
        </Checkbox.Root>
        Remember this device
      </label>
      <label className={label}>
        <Checkbox.Root indeterminate>
          <Checkbox.Indicator />
        </Checkbox.Root>
        Select all
      </label>
      <label className={label}>
        <Checkbox.Root disabled readOnly defaultChecked>
          <Checkbox.Indicator />
        </Checkbox.Root>
        Managed by your admin
      </label>
      <Field.Root validationMode="onChange">
        <Field.Label className={label}>
          <Checkbox.Root name="terms" required>
            <Checkbox.Indicator />
          </Checkbox.Root>
          Accept the terms
        </Field.Label>
        <Field.Error match="valueMissing">You must accept the terms.</Field.Error>
      </Field.Root>
    </main>
  );
}

API reference

Checkbox.Root

The checkbox. Labelled by a wrapping <label> or a Field.Label. Renders <span> with role checkbox.

PropTypeDefaultDescription
checkedboolean—Controlled checked state.
defaultCheckedbooleanfalseInitial checked state when uncontrolled.
indeterminatebooleanfalseMixed state (aria-checked=mixed). Controlled by you; clicking still toggles `checked`.
namestring—Name of the hidden input for form submission.
valuestring—Value of the hidden input when checked.
disabledbooleanfalseIgnores interaction and is removed from the tab order.
readOnlybooleanfalseFocusable but cannot be toggled.
requiredbooleanfalseMust be checked for the form/field to be valid (valueMissing).
EventPayloadDescription
onCheckedChangebooleanFired when the user toggles the checkbox. Angular: the `checked` model's change event.
Data attributePresent when
data-checkedchecked
data-uncheckednot checked
data-indeterminateindeterminate
data-disableddisabled
data-readonlyread-only
data-requiredrequired
data-validin a Field: validated and valid
data-invalidin a Field: validated and invalid
data-dirtyin a Field: checked state differs from initial
data-touchedin a Field: has been blurred
data-filledin a Field: checked
data-focusedin a Field: focused

Checkbox.Indicator

Shown while checked or indeterminate. The styled version renders a check (or dash) icon by default. Renders <span>.

PropTypeDefaultDescription
keepMountedbooleanfalseKeep in the DOM while unchecked.
Data attributePresent when
data-checkedchecked
data-uncheckednot checked
data-indeterminateindeterminate
data-disableddisabled
data-readonlyread-only
data-requiredrequired
data-validin a Field: validated and valid
data-invalidin a Field: validated and invalid
data-dirtyin a Field: checked state differs from initial
data-touchedin a Field: has been blurred
data-filledin a Field: checked
data-focusedin a Field: focused
data-starting-styleanimating in
data-ending-styleanimating out

Keyboard

KeyAction
SpaceToggles the checkbox.