[ Forms ]

Checkbox

A checkbox with an optional label and description. Supports the indeterminate state for "select all" patterns and works controlled or uncontrolled like a native checkbox.

4 examplesDark modeAccessible

Import#

import { Checkbox } from 'reusify-ui';

When to use#

  • Turning a single option on or off as part of a form that is submitted later, such as "I accept the terms".
  • Choosing any number of items from a list.
  • For a setting that applies immediately, use a Switch instead.

Examples#

Basic#

<Checkbox label="Accept terms and conditions" defaultChecked />
<Checkbox label="Subscribe to newsletter" />
<Checkbox label="Disabled option" disabled />

With description#

description adds secondary text under the label.

<Checkbox
  label="Marketing emails"
  description="Product news and tips, about once a month."
/>

Colors#

color sets the checked fill, the indeterminate dash and the focus ring. Colors come from the theme tokens, so they follow your brand and dark mode.

<Checkbox color="primary" label="Primary" defaultChecked />
<Checkbox color="secondary" label="Secondary" defaultChecked />
<Checkbox color="success" label="Success" defaultChecked />
<Checkbox color="danger" label="Danger" defaultChecked />
<Checkbox color="warning" label="Warning" defaultChecked />
<Checkbox color="info" label="Info" defaultChecked />
<Checkbox color="success" label="Partially done" indeterminate />

Indeterminate (select all)#

indeterminate shows a dash when only some children are selected. Click the items to see it change.

function NotificationSettings() {
  const items = ['Email', 'SMS', 'Push'];
  const [checked, setChecked] = useState(['Email']);
  const all = checked.length === items.length;

  return (
    <>
      <Checkbox
        label="All notifications"
        checked={all}
        indeterminate={checked.length > 0 && !all}
        onChange={() => setChecked(all ? [] : items)}
      />
      {items.map((item) => (
        <Checkbox
          key={item}
          label={item}
          checked={checked.includes(item)}
          onChange={(e) =>
            setChecked(e.target.checked
              ? [...checked, item]
              : checked.filter((c) => c !== item))
          }
        />
      ))}
    </>
  );
}

Used in designs#

Checkbox in real layouts: these 2 designs use it. Open one to try it and copy the code.

API reference#

<Checkbox />

Also accepts native checkbox attributes (checked, defaultChecked, onChange, name, value…) and forwards its ref to the <input>.

label-
ReactNode

Clickable label next to the box.

description-
ReactNode

Secondary text below the label.

indeterminatefalse
boolean

Shows a dash for a partially selected group.

color'primary'
'primary' | 'secondary' | 'success' | 'danger' | 'warning' | 'info'

Checked fill and focus ring color.

disabledfalse
boolean

Disables the checkbox.

Accessibility#

  • A native <input type="checkbox"> inside a <label>, so clicking the text toggles it and Space works.
  • indeterminate is set on the DOM node, so screen readers announce "mixed".