<!-- index: /slotted/llms.txt -->
# checkbox-group

`data-ui="checkbox-group"` · Set of checkboxes under one label.

## Import

```tsx
import { Checkbox, CheckboxGroup, CheckboxGroupDefaultsProvider } from '@slotted/react';
```

## Minimal example

Example

```tsx
import { Checkbox } from '@slotted/react/checkbox';
import { CheckboxGroup } from '@slotted/react/checkbox-group';
import { FieldGroup, FieldGroupContent, FieldGroupLegend } from '@slotted/react/field-group';

export default function CheckboxGroupBasic() {
  return (
    <FieldGroup>
      <FieldGroupLegend>Notify me about failed payments by</FieldGroupLegend>
      <FieldGroupContent>
        <CheckboxGroup name="channels" defaultValue={['email', 'in-app']}>
          <Checkbox value="email">Email</Checkbox>
          <Checkbox value="sms">Text message</Checkbox>
          <Checkbox value="in-app">In-app notification</Checkbox>
          <Checkbox value="slack">Slack, #billing-alerts</Checkbox>
        </CheckboxGroup>
      </FieldGroupContent>
    </FieldGroup>
  );
}
```

## Anatomy

| Part | Element | Required |
| --- | --- | --- |
| `root` | div, or the element render returns | yes |

## Props

| Name | Type | Default | Applies to | Description |
| --- | --- | --- | --- | --- |
| `value` | `readonly string[]` | — | CheckboxGroup | The controlled selection. When present, the consumer owns the selection for the life of the component. When absent, the checkbox group owns it and starts from defaultValue. |
| `defaultValue` | `readonly string[]` | [] | CheckboxGroup | The selection an uncontrolled group starts from, read once. |
| `allValues` | `readonly string[]` | — | CheckboxGroup | Every value the checkbox group holds. A parent Checkbox needs it: the parent is checked when every value is selected and mixed when some are, which a selection alone cannot tell. It also keeps the selection in the order of this list rather than in the order the boxes were pressed. |
| `variant` | `'plain' \| 'tile'` | plain | CheckboxGroup, CheckboxGroupDefaultsProvider | How every choice is drawn, passed to every box. tile frames each choice and makes its whole surface the target. Arranging the tiles is left to Grid. |
| `orientation` | `'vertical' \| 'horizontal'` | vertical | CheckboxGroup, CheckboxGroupDefaultsProvider | How the choices flow. A horizontal set wraps instead of scrolling. |
| `size` | `'sm' \| 'md' \| 'lg'` | md | CheckboxGroup, CheckboxGroupDefaultsProvider | The type step, passed to every box. A field or field group around the checkbox group decides it first, then the provider. |
| `labelPlacement` | `'end' \| 'start'` | end | CheckboxGroup, CheckboxGroupDefaultsProvider | Which side of its label each box is on, set once for the set so the boxes line up. |
| `name` | `string` | — | CheckboxGroup | The submission name every box shares when it sets none of its own. No name is generated: the platform does not group checkboxes by name the way it groups radios. |
| `disabled` | `boolean` | from the field, or false | CheckboxGroup | Unset rather than false. Reaches every box as the native attribute, which takes the boxes out of the tab order and out of form submission. |
| `readOnly` | `boolean` | from the field, or false | CheckboxGroup | Unset rather than false. Every box keeps its tab stop, its focus ring and its contrast, writes aria-readonly, and refuses every change. |
| `invalid` | `boolean` | from the field, or false | CheckboxGroup | Unset rather than false. Writes aria-invalid on the group and on every box, and data-invalid on the group’s root and on every box’s root. |
| `required` | `boolean` | from the field, or false | CheckboxGroup | Unset rather than false. Writes aria-required on the group, never the native required, which on a checkbox would demand that one particular box be checked. |
| `showCount` | `boolean` | false | CheckboxGroup | Inside a field, draws how many of allValues are selected at the end of the field’s label row, as 2/4, and adds that count to the group’s accessible description. |
| `render` | `(props: CheckboxGroupRootProps) => ReactNode` | — | CheckboxGroup | Replaces the root and hands it every attribute. The props are typed as HTMLAttributes<HTMLElement>, so a set of choices can render as a ul with each box as an li. |
| `useCheckboxGroup()` | `() => CheckboxGroupContextValue \| undefined` | — | inside a CheckboxGroup | Returns what a box reads: whether it is selected, its size, and how to request a change. It returns undefined outside a checkbox group, so one Checkbox serves both a single consent box and a member of a set. |
| `CheckboxGroupDefaultsProvider` | `{ size, variant, orientation, labelPlacement }` | — | a subtree | The four decisions an application takes once for every checkbox group under it. A field or field group around a checkbox group decides size before the provider. |
| `className / style` | `string / CSSProperties` | — | CheckboxGroup | Reach the root. Every other native prop reaches it too, because the root is the only element the checkbox group renders. |
| `role` | `string` | group, or none inside a field group | CheckboxGroup | A role the consumer writes wins. Otherwise the role is group, withdrawn inside a field group, because a fieldset with a legend is already a named group and a second group named by the same legend announces the set twice. |
| `aria-label / aria-labelledby` | `string` | the field’s label, inside a field | CheckboxGroup | Names the group. Inside a field, aria-labelledby points at the field’s label unless the consumer writes either attribute. Outside a field and a field group, write one of them, because a group needs a name. |

## Events

| Name | Payload | When |
| --- | --- | --- |
| `onValueChange` | `(value: readonly string[], request: ChangeRequest<readonly string[]>)` | Before the selection moves. request.value is the new array and request.previous the array it replaces. request.reason is pointer or keyboard, recovered from the box’s click; request.source is the originating event; request.cancel() refuses the move before anything commits, including the platform’s own toggle. |
| `onCheckedChange, on each Checkbox` | `(checked: boolean, request: ChangeRequest<boolean>)` | Before that one box moves, and before the checkbox group builds its own request. Cancelling it also stops the group’s request, so one option can refuse to be chosen without the rest of the set taking part. |

## Axes and defaults

| Axis | Values | Default |
| --- | --- | --- |
| `size` | `sm`, `md`, `lg` | `md` |
| `variant` | `plain`, `tile` | `plain` |
| `orientation` | `vertical`, `horizontal` | `vertical` |
| `labelPlacement` | `end`, `start` | `end` |

## Rules

- The WAI-ARIA Authoring Practices have no pattern for a group of checkboxes: the Checkbox pattern covers one box, and grouping is a question of naming rather than of keyboard behavior. The sources are the ARIA group role and the Providing Accessible Names and Descriptions practice (w3.org/WAI/ARIA/apg/practices/names-and-descriptions/).
- Every box is its own tab stop, and Space toggles the box that has focus. The platform provides this whole keyboard model. It is what sets a checkbox group apart from a radio group, where the set is a single tab stop and the arrow keys move the choice.
- The role depends on the container. A field group is a fieldset with a legend, which is already a named group to assistive technology; a role="group" inside it, named by the same legend, would announce the set twice. Inside a field group the checkbox group renders no role, and the legend names the set. Inside a field, the field’s label names the group through aria-labelledby.
- The group writes aria-required, never the native required on a box. The native attribute on a checkbox demands that one particular box be checked, which is not what "choose at least one" means, and it turns on browser constraint validation.
- A checkbox has no native read-only state, so the checkbox group enforces one: every box refuses the change on the click that a pointer and Space share, aria-readonly announces it, and every tab stop and focus ring stays.
- The parent box is derived from the set rather than kept as a state of its own, so it always agrees with the boxes it controls. It reports aria-checked="mixed" through the platform’s indeterminate property, and pressing it replaces the selection instead of adding a value of its own.

## Tokens

`--slotted-checkbox-group-gap`, `--slotted-control-gap-lg`, `--slotted-control-gap-md`, `--slotted-control-gap-sm`

## Examples

See [checkbox-group.examples.md](./checkbox-group.examples.md).
