# Checkbox

Native checkboxes, restyled, with select-all groups.

Source: https://www.zazz.sh/api/primitives/checkbox/

## Installation

```bash
pnpm dlx zazz-ui add checkbox
```

Also adds `fields`, which it depends on.

Run `zazz-ui init` first if the project has no `zazz/` folder yet.

## Examples

### Default

```html
<fieldset data-ui="field-group">
  <legend>Preferences</legend>
  <div
    style="
      --display: flex-col;
      --gap: 2;
    "
  >
    <label data-ui="field" data-field-orientation="horizontal">
      <input type="checkbox" name="preferences-all" data-checkbox-controls="preferences" />
      <span data-field-slot="label">Select all</span>
    </label>
    <label data-ui="field" data-field-orientation="horizontal">
      <input type="checkbox" name="preferences" value="newsletter" />
      <span data-field-slot="label">Subscribe to the newsletter</span>
    </label>
    <label data-ui="field" data-field-orientation="horizontal">
      <input type="checkbox" name="preferences" value="terms" checked />
      <span data-field-slot="label">Accept terms &amp; conditions</span>
    </label>
  </div>
</fieldset>
```

### Tasklist

```html
<table data-ui="table">
  <caption>
    Sprint tasks with managed selection.
  </caption>
  <thead>
    <tr>
      <th>
        <input
          type="checkbox"
          name="tasks-all"
          data-checkbox-controls="tasks"
          aria-label="Select all tasks"
        />
      </th>
      <th>Task</th>
      <th>Assignee</th>
      <th style="--text-align: end">Due</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>
        <input
          type="checkbox"
          name="tasks"
          value="ZAZZ-101"
          aria-label="Select Audit focus rings"
          checked
        />
      </td>
      <td>Audit focus rings</td>
      <td>Robin</td>
      <td style="--text-align: end">Aug 28</td>
    </tr>
    <tr>
      <td>
        <input
          type="checkbox"
          name="tasks"
          value="ZAZZ-102"
          aria-label="Select Document table variants"
        />
      </td>
      <td>Document table variants</td>
      <td>Sasha</td>
      <td style="--text-align: end">Aug 29</td>
    </tr>
    <tr>
      <td>
        <input
          type="checkbox"
          name="tasks"
          value="ZAZZ-103"
          aria-label="Select Ship checkbox groups"
          checked
        />
      </td>
      <td>Ship checkbox groups</td>
      <td>Quinn</td>
      <td style="--text-align: end">Sep 01</td>
    </tr>
    <tr>
      <td>
        <input
          type="checkbox"
          name="tasks"
          value="ZAZZ-104"
          aria-label="Select Retire legacy tokens"
        />
      </td>
      <td>Retire legacy tokens</td>
      <td>Robin</td>
      <td style="--text-align: end">Sep 03</td>
    </tr>
  </tbody>
</table>
```

## Behavior

`primitives/checkbox/checkbox.js`

Checkbox select-all groups.
Derives a select-all checkbox's tri-state from its members.
A controller declares `data-checkbox-controls="<name>"` and manages every
checkbox sharing that `name` within the same form (or the document when
unassociated). Checking the controller checks or unchecks all members;
member changes roll back up as checked (all), unchecked (none), or
indeterminate (a subset) — the mixed state is the JS-only `indeterminate`
property (the platform has no content attribute for it), painted by
`:indeterminate` in checkbox.css. Members inside a `<tr>` reflect their
state as `data-table-state="selected"` on the row, lighting up the table
primitive's selected-row styling.

State follows the kit's signals division of labor (`base/signals.ts`): the
delegated change listener is the input adapter writing member states into
`state`; `deriveTriState` is the pure derived logic under `computed`; one
`effect` per group writes the controller property and row attributes back
to the DOM, batched to a microtask.

## Attributes

| Attribute | Kind | Values |
| --- | --- | --- |
| `data-checkbox-controls` | config | free value |

Requires: fields.

## Hooks

| Hook | Default | State |
| --- | --- | --- |
| `--ui-checkbox-bg` | `var(--ui-field-bg)` | — |
| `--ui-checkbox-bg--checked` | `var(--color-primary)` | checked |
| `--ui-checkbox-border-color` | `var(--ui-field-border-color)` | — |
| `--ui-checkbox-border-color--checked` | `var(--color-primary)` | checked |
| `--ui-checkbox-border-color--hover` | `var(--ui-field-border-color--hover)` | hover |
| `--ui-checkbox-border-style` | `var(--ui-field-border-style)` | — |
| `--ui-checkbox-border-width` | `var(--ui-field-border-width)` | — |
| `--ui-checkbox-checkmark-mask` | `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' fill='none' stroke='currentColor' stroke-linecap='square' stroke-linejoin='square' stroke-width='3' viewBox='0 0 24 24'%3E%3Cpath d='M20 6 9 17l-5-5'/%3E%3C/svg%3E") center / contain no-repeat` | — |
| `--ui-checkbox-corner-shape` | `var(--radius-corner-shape)` | — |
| `--ui-checkbox-glyph-color` | `var(--color-primary-foreground)` | — |
| `--ui-checkbox-glyph-size` | `1em` | — |
| `--ui-checkbox-indeterminate-mask` | `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='1em' height='1em' fill='none' stroke='currentColor' stroke-linecap='square' stroke-linejoin='square' stroke-width='3' viewBox='0 0 24 24'%3E%3Cpath d='M5 12h14'/%3E%3C/svg%3E") center / contain no-repeat` | — |
| `--ui-checkbox-radius` | `var(--radius-xs)` | — |
| `--ui-checkbox-size` | `calc(var(--spacing) * 4.5)` | — |
