# Checkbox and CheckboxGroup

> Native checkboxes from @salmonui/inputs/checkbox with a mixed state, label placement, form submission and a CheckboxGroup whose value is a string array.

Canonical: https://salmonui.dev/docs/inputs/checkbox

`Checkbox` is a native
`<input type="checkbox">` drawn with CSS (`appearance: none` and a mask
icon), labelled by its children through a real `<label for>`, with the
shared description and error parts (see [Field](https://salmonui.dev/docs/inputs/field)).
`CheckboxGroup` (a Salmon addition) holds several checkboxes as one
`string[]` value inside a `<fieldset>`.

```tsx
import { Checkbox, CheckboxGroup } from '@salmonui/inputs/checkbox';
import '@salmonui/inputs/checkbox/styles.css';

<Checkbox name="terms" value="yes" required>I accept the terms</Checkbox>
<CheckboxGroup name="fruit" label="Fruit" defaultValue={['apple']} orientation="horizontal">
  <Checkbox value="apple">Apple</Checkbox>
  <Checkbox value="pear">Pear</Checkbox>
</CheckboxGroup>
```

### Live demo: Checkbox basics

Live demo: https://salmonui.dev/demos/inputs/checkbox-basics

A checkbox with a mixed parent state, label placement and a horizontal CheckboxGroup bound to a string array.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Checkbox, CheckboxGroup } from '@salmonui/inputs/checkbox';

// A parent checkbox in the mixed state over a CheckboxGroup, and label placement.
export default function CheckboxBasicsDemo() {
  const [toppings, setToppings] = useState<readonly string[]>(['basil']);
  const all = ['basil', 'olives', 'chili'];
  return (
    <div style={{ display: 'grid', gap: 12 }}>
      <Checkbox checked={toppings.length === all.length} indeterminate={toppings.length > 0 && toppings.length < all.length} onCheckedChange={(checked) => setToppings(checked ? all : [])}>
        All toppings
      </Checkbox>
      <CheckboxGroup label="Toppings" name="toppings" value={toppings} onValueChange={setToppings} orientation="horizontal">
        <Checkbox value="basil">Basil</Checkbox>
        <Checkbox value="olives">Olives</Checkbox>
        <Checkbox value="chili">Chili</Checkbox>
      </CheckboxGroup>
      <Checkbox labelPlacement="start" description="Label before the box">
        Remember me
      </Checkbox>
      <output>Value: {JSON.stringify(toppings)}</output>
    </div>
  );
}
```

## Checked state

`checked` / `defaultChecked` / `onCheckedChange(checked, details)`. `details` is a frozen `{ source, reason }`: `source` is
`'pointer'`, `'touch'`, `'keyboard'` or `'api'`, `reason` is `'toggle'` or
`'reset'` (the owning form was reset). Native `onChange`
still fires. Without `checked` the checkbox is uncontrolled and a form reset
restores `defaultChecked`.

### Live demo: Choice events

Live demo: https://salmonui.dev/demos/inputs/choice-events

Change events of RadioGroup, Radio, Checkbox and Switch with their details, radio labels before or after the control, and standalone radios grouped by a shared native name.

`Demo.tsx`:

```tsx
'use client';

import { useState } from 'react';
import { Checkbox } from '@salmonui/inputs/checkbox';
import { Radio, RadioGroup } from '@salmonui/inputs/radio-group';
import { Switch } from '@salmonui/inputs/switch';
import { EventLog, useEventLog } from '../../components/demo-kit';

// Change events of the choice controls, radio labels before the control and
// standalone radios grouped only by a shared native `name`.
export default function ChoiceEventsDemo() {
  const [log, push] = useEventLog(12);
  const [shipping, setShipping] = useState<string | null>('standard');
  const [standalone, setStandalone] = useState('monthly');
  return (
    <>
      <div style={{ display: 'grid', gap: 16, maxInlineSize: 420 }}>
        <RadioGroup
          label="Shipping"
          name="ce-shipping"
          labelPlacement="start"
          value={shipping}
          onValueChange={(value, details) => {
            setShipping(value);
            push(`RadioGroup onValueChange ${JSON.stringify(value)} (${details.reason}, ${details.source})`);
          }}
        >
          <Radio value="standard">Standard, 3 to 5 days</Radio>
          <Radio value="express">Express, next day</Radio>
          <Radio value="pickup" labelPlacement="end">
            Pick up in store (label after)
          </Radio>
        </RadioGroup>
        <div role="group" aria-labelledby="ce-billing-label" style={{ display: 'flex', flexWrap: 'wrap', alignItems: 'center', gap: '4px 16px' }}>
          <span id="ce-billing-label" style={{ flexBasis: '100%', fontWeight: 'var(--salmon-font-weight-medium)' }}>
            Billing (standalone radios)
          </span>
          <Radio name="ce-billing" value="monthly" checked={standalone === 'monthly'} onChange={(event) => (setStandalone(event.target.value), push(`Radio onChange ${event.target.value}`))}>
            Monthly
          </Radio>
          <Radio name="ce-billing" value="yearly" checked={standalone === 'yearly'} onChange={(event) => (setStandalone(event.target.value), push(`Radio onChange ${event.target.value}`))}>
            Yearly
          </Radio>
        </div>
        <Checkbox name="ce-terms" value="accepted" onCheckedChange={(checked, details) => push(`Checkbox onCheckedChange ${checked} (${details.source})`)}>
          I accept the terms
        </Checkbox>
        <Switch name="ce-dark" value="on" onCheckedChange={(checked, details) => push(`Switch onCheckedChange ${checked} (${details.source})`)}>
          Dark receipts
        </Switch>
      </div>
      <p className="demo-status">
        Shipping {JSON.stringify(shipping)} · Billing {JSON.stringify(standalone)}
      </p>
      <EventLog entries={log} label="Change events" />
    </>
  );
}
```

## Mixed state

`indeterminate` is a DOM property, so the component sets it in an
effect after every commit; the server markup already carries
`aria-checked="mixed"` and `data-indeterminate`, so the first paint and the
accessibility tree are right before hydration and there is no hydration
difference. Clicking a mixed checkbox checks it; a controlled parent decides
whether it stays mixed.

```tsx
<Checkbox checked={all} indeterminate={some && !all} onCheckedChange={(c) => setItems(c ? ids : [])}>
  All toppings
</Checkbox>
```

## Label and placement

Children are the label; without children, name the
checkbox with `aria-label`. `labelPlacement: 'start' | 'end'` (default
`'end'`) also reorders the DOM, so reading order matches the visual order.
The 16px box answers clicks inside a 26px area (a pseudo-element, WCAG 2.2
target size) and the whole label is clickable.

## Form submission

`name`, `value` (default `'on'`), `form` and `required` are native: a checked box submits `name=value` with `FormData` and React 19
`<form action>`.

## CheckboxGroup

- `value` / `defaultValue` / `onValueChange(value, details)` with a
  `string[]` in the order the items were checked; `details.item` is the
  toggled value.
- `name` and `form` apply to every item; each checked item submits one
  `name=value` entry.
- `label` (the `<legend>`), `description`, `errorMessage`, `invalid`,
  `orientation: 'vertical' | 'horizontal'`, `disabled` (the fieldset is
  disabled, so every item is).
- `required` means at least one item; the error appears once the group
  becomes empty or on submit.
- A `Checkbox` inside a group takes its checked state from the group and
  ignores `checked` / `defaultChecked`.

## Keyboard

| Key | Action |
| --- | --- |
| Space | Toggles the focused checkbox |
| Tab / Shift+Tab | Moves between checkboxes (each is a tab stop) |

Screen readers hear a native checkbox ("checked", "not checked", "mixed")
named by its label; a group is announced by its legend and description.
Try it in the [keyboard demo](https://salmonui.dev/demos/inputs/inputs-keyboard), and
see the [form demo](https://salmonui.dev/demos/inputs/field-forms) for submission.

## SSR

The checkbox is a native input, so the server markup is final (ids from
`useId`); only `indeterminate`, a DOM property, is set after hydration, and
the server already renders `aria-checked="mixed"` for it.

The shared field behavior (labels, descriptions, validation) is described
on [Field](https://salmonui.dev/docs/inputs/field#design-notes).

## API

### Checkbox

| Prop | Type | Default |
| --- | --- | --- |
| `checked`, `defaultChecked` | `boolean` | `false` |
| `onCheckedChange` | `(checked, { source, reason }) => void` | |
| `indeterminate` | `boolean` | `false` |
| `children` | `ReactNode` (label) | |
| `labelPlacement` | `'start' \| 'end'` | `'end'` |
| `name`, `value`, `form`, `required`, `disabled` | native | `value: 'on'` |
| `description`, `errorMessage`, `invalid`, `validation`, `validateOn` | see [Field](https://salmonui.dev/docs/inputs/field) | |
| `size` | `'sm' \| 'md' \| 'lg'` | `'md'` |
| `classNames` | `{ input, label, description, error }` | |
| `ref` | `HTMLInputElement` | |

Other props go to the native input; `className` / `style` go to the root.

### CheckboxGroup

| Prop | Type | Default |
| --- | --- | --- |
| `value`, `defaultValue` | `readonly string[]` | `[]` |
| `onValueChange` | `(value, { source, reason, item }) => void` | |
| `name`, `form` | `string` | |
| `label`, `description`, `errorMessage`, `invalid` | see [Field](https://salmonui.dev/docs/inputs/field) | |
| `required` | `boolean` (at least one) | `false` |
| `disabled` | `boolean` | `false` |
| `orientation` | `'vertical' \| 'horizontal'` | `'vertical'` |
| `classNames` | `{ label, items, description, error }` | |
| `ref` | `HTMLFieldSetElement` | |

Full signatures: [`@salmonui/inputs/checkbox` API reference](https://salmonui.dev/docs/reference/inputs/checkbox).
