# Checkbox

Turns one option on or off, or shows a mix for a group. Built on ui-sdk's Checkbox: Space toggles it, it submits with its form, and a parent checkbox controls its group. Sm (14 px) in forms, md (16 px) in task lists.

- Beta: ready to use; its API can still change in a minor release.
- Category: Selection

```tsx
import { Checkbox, CheckboxGroup } from "@leap-crm/design-system";
```

## Best practices

- Do: Name every checkbox with a `Label` pointing at its id, a wrapping `<label>`, or `aria-label`. Pressing a label toggles the box, which makes the target larger.
- Do: Use a `parent` checkbox in a `CheckboxGroup` for select all. It shows indeterminate while only some are checked.
- Do: Keep the checkbox at the start of a list or table row, with `md` in task lists and `sm` in forms. Only a menu's selection tick sits on the right.
- Do: Mark an invalid box with `aria-invalid` and say what to do in text near it, like Please confirm the conflict check. The red edge alone is colour alone.
- Don't: Use a checkbox for a setting that takes effect at once. Use `Switch`; a checkbox waits for its form to be saved.
- Don't: Use checkboxes for options where only one can be true, like how a matter is billed. Use `RadioGroup`.

## Keyboard

| Keys | What they do |
| --- | --- |
| Space | Toggles it. |
| Enter | Submits the form, and never toggles it. |
| Tab | Moves through checkboxes in order. |

## Related

- [Switch](./switch.md)
- [Radio](./radio.md)
- [Toggle](./toggle.md)

## Examples

What the examples import:

```tsx
import { Fragment, type ReactNode } from "react";
import { Checkbox, CheckboxGroup, Label } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const sizes = ["sm", "md"] as const;

const states = [
  "Unchecked",
  "Checked",
  "Indeterminate",
  "Invalid",
  "Disabled",
  "Disabled checked",
] as const;

function Row({ children }: { children: ReactNode }) {
  return <div className="text-body flex items-center gap-2">{children}</div>;
}
```

### States and sizes

Hover an unchecked box for its navy edge, press one to watch the tick draw itself, and Tab for focus. Invalid uses aria-invalid.

```tsx
<div className="grid grid-cols-[auto_repeat(6,auto)] items-center justify-start gap-x-8 gap-y-4">
  <span />
  {states.map(state => (
    <span key={state} className="text-small text-neutral-text-muted">
      {state}
    </span>
  ))}
  {sizes.map(size => (
    <Fragment key={size}>
      <span className="text-small text-neutral-text-muted">{size}</span>
      {states.map(state => (
        <Checkbox
          key={`${size}-${state}`}
          size={size}
          aria-label={`${state}, ${size}`}
          {...stateProps[state]}
        />
      ))}
    </Fragment>
  ))}
</div>
```

### With a label

Name each checkbox with a Label pointing at its id. Pressing the label toggles the box.

```tsx
<div className="flex flex-col gap-3">
  <Row>
    <Checkbox id="terms" required />
    <Label htmlFor="terms" required>
      I have read the costs disclosure
    </Label>
  </Row>
  <Row>
    <Checkbox id="newsletter" />
    <Label htmlFor="newsletter" optional>
      Send me the monthly newsletter
    </Label>
  </Row>
</div>
```

### Group with select all

A parent checkbox checks or clears the group, and shows indeterminate when only some are checked.

```tsx
<CheckboxGroup
  defaultValue={["email"]}
  allValues={["email", "phone", "letter"]}
  aria-label="Contact preferences"
>
  <Row>
    <Checkbox id="all" parent />
    <Label htmlFor="all">All channels</Label>
  </Row>
  {[
    ["email", "Email"],
    ["phone", "Phone"],
    ["letter", "Letter"],
  ].map(([value, label]) => (
    <div key={value} className="ps-6">
      <Row>
        <Checkbox id={`channel-${value}`} value={value} />
        <Label htmlFor={`channel-${value}`}>{label}</Label>
      </Row>
    </div>
  ))}
</CheckboxGroup>
```

### Task list

The md size, for ticking tasks off a list.

```tsx
<div className="flex flex-col gap-2">
  {["Call the client back", "Send the costs agreement"].map(task => (
    <Row key={task}>
      <Checkbox size="md" id={task} />
      <Label htmlFor={task}>{task}</Label>
    </Row>
  ))}
</div>
```

## Props

Both also take ui-sdk's props: checked, defaultChecked, onCheckedChange, disabled, readOnly, required, name, value, form and parent on Checkbox; value, defaultValue, onValueChange, allValues and name on CheckboxGroup.

### Checkbox

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `size` | `"sm" \| "md"` | `"sm"` | sm (14 px) in forms, md (16 px) in task lists. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### CheckboxGroup

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
