# Checkbox

A square that picks items out of a list, with a mixed state for a header box.

Live variants: https://design.arcadia.is/components/checkbox

```tsx
import { Checkbox } from "@arcadia/ui";
```

## In context

Select all tasks at once, or choose them individually.

```tsx
import { useState } from "react";
import { Checkbox, Separator } from "@arcadia/ui";

function Selection() {
  const [picked, setPicked] = useState<ReadonlySet<string>>(new Set([ROWS[0]!]));
  const all = picked.size === ROWS.length ? true : picked.size ? "mixed" : false;
  const flip = (r: string, on: boolean) => setPicked((p) => { const n = new Set(p); if (on) n.add(r); else n.delete(r); return n; });
  return (
    <div role="group" aria-label="Tasks" {...sx("cmp-list", cmp.list)}>
      <Checkbox showLabel label="Select all tasks" checked={all} onCheckedChange={(on) => setPicked(new Set(on ? ROWS : []))} />
      <Separator />
      {ROWS.map((r) => <Checkbox key={r} showLabel label={r} checked={picked.has(r)} onCheckedChange={(on) => flip(r, on)} />)}
      <span {...sx("cmp-result", cmp.result)} role="status">{picked.size} selected</span>
    </div>
  );
}
```

## When to use

- Select rows for a bulk action.
- Show a partial selection in the header checkbox.
- Offer a choice to submit later in a form.

## When not to use

- Use `Switch` for an immediate on or off setting.
- Use `ChoiceList` for one answer with several described choices.

## Usage

```tsx
import { useState } from "react";
import { Checkbox } from "@arcadia/ui";

function Usage() {
  const [on, setOn] = useState(false);
  return <Checkbox label="Select Review the checkout flow" checked={on} onCheckedChange={setOn} />;
}
```

Use `checked="mixed"` when only some rows are selected.

Pressing the mixed checkbox selects all rows in this example.

## Examples

### States

Compare the unchecked, checked, mixed and disabled glyphs.

```tsx
import { useState } from "react";
import { Checkbox } from "@arcadia/ui";

function States() {
  const [checked, setChecked] = useState<boolean | "mixed">("mixed");
  return (
    <>
      <Checkbox label="Off" />
      <Checkbox label="On" checked />
      <Checkbox label="Mixed" checked={checked} onCheckedChange={setChecked} />
      <Checkbox label="Disabled" disabled />
      <Checkbox label="Disabled and on" checked disabled />
    </>
  );
}
```

### In a form

Add a visible label and describe what checking the box does.

```tsx
import { useState } from "react";
import { Checkbox } from "@arcadia/ui";

function InAForm() {
  const [on, setOn] = useState(false);
  return (
    <Checkbox showLabel label="Arcadia may contact me about this" description="This shares your name and email with Arcadia's staff."
      checked={on} onCheckedChange={setOn} />
  );
}
```

## Customising

`rootStyle` takes layout and the glyph's ink, off and on.

- `--checkbox-off` (`Paint`, default `var(--icon-2)`): The glyph's ink when unchecked.
- `--checkbox-on` (`Paint`, default `var(--icon-1)`): The glyph's ink when checked or mixed.

## Accessibility

The platform's own checkbox, laid clear over the box: it takes the focus, the press and the keys, and the kit draws the glyph. Mixed is its indeterminate state. It is named by `label`: say what it selects ("Select Fix the pricing bug"), since the row's text is not its name. Space and a press toggle it. `name`, `value`, `required` and `form` reach the checkbox, so a form sends it, and a form's reset puts back `defaultChecked`. Disabled boxes keep their place and leave the tab order. With `showLabel` the words are its name and the `description` its description.

## API reference

### Checkbox

- `checked` (`boolean | "mixed"`): Controlled state; `"mixed"` for a header box over a partial selection.
- `defaultChecked` (`boolean`, default `false`): Initial state when uncontrolled, and what a form's reset puts back.
- `onCheckedChange` (`(checked: boolean, details: { event: Event }) => void`): Called with the new state when the box toggles (mixed always goes to checked).
- `label` (`string`, required): Required: the accessible name ("Select Fix the pricing bug"). A checkbox in a row names what it selects.
- `showLabel` (`boolean`, default `false`): The label stands after the box as its words, and the box and its words are one target: a box inside a form ("Arcadia may contact me about this"). Without it the box stands alone, named by `label`.
- `description` (`ReactNode`): With `showLabel`: one quiet line under the words, what ticking the box does. It describes the box.
- `rootStyle` (`CheckboxStyle`): Typed StyleX override on the outermost element (the box, or its row with `showLabel`): layout and the `--checkbox-*` knobs.

Rest props go to `<input>` props.
