# Checkbox

> A labelled checkbox with checked, unchecked and indeterminate states.

Use a checkbox for an option people turn on or off as part of a form they submit, or to pick several items from a list. For a setting that applies at once, use a [switch](/docs/components/switch).

```tsx
import { Checkbox } from "@aloxe/react";

export default function CheckboxBasic() {
  return (
    <div className="flex flex-col gap-3">
      <Checkbox label="Email me when someone comments" defaultChecked />
      <Checkbox label="Email me about product updates" />
      <Checkbox label="Some projects selected" indeterminate />
    </div>
  );
}
```

## Installation

```bash
npm i @aloxe/react @aloxe/tokens
```

Then add the CSS imports from [Installation](/docs/getting-started/installation).

## Usage

```tsx
import { Checkbox } from "@aloxe/react";

<Checkbox label="Email me when someone comments" name="comments" defaultChecked />
```

The label and the box sit in one `<label>`, so clicking the text toggles the checkbox.

## Examples

### Indeterminate

`indeterminate` shows a dash and is announced as mixed. Use it on a parent checkbox when some, but not all, of its children are checked.

```tsx
"use client";

import { Checkbox } from "@aloxe/react";
import { useState } from "react";

const channels = ["Email", "Push", "SMS"] as const;
type Channel = (typeof channels)[number];

export default function CheckboxIndeterminate() {
  const [selected, setSelected] = useState<Channel[]>(["Email"]);
  const all = selected.length === channels.length;
  const some = selected.length > 0 && !all;

  return (
    <fieldset className="m-0 flex flex-col gap-3 border-0 p-0">
      <legend className="sr-only">Notification channels</legend>
      <Checkbox
        label="All channels"
        checked={all}
        indeterminate={some}
        onCheckedChange={(checked) => setSelected(checked ? [...channels] : [])}
      />
      <div className="flex flex-col gap-3 pl-6">
        {channels.map((channel) => (
          <Checkbox
            key={channel}
            label={channel}
            checked={selected.includes(channel)}
            onCheckedChange={(checked) =>
              setSelected((current) =>
                checked ? [...current, channel] : current.filter((item) => item !== channel),
              )
            }
          />
        ))}
      </div>
    </fieldset>
  );
}
```

### States

An error for a required consent, and disabled checkboxes, checked and not. A disabled checkbox with a description says why it can't change.

```tsx
import { Checkbox } from "@aloxe/react";

export default function CheckboxStates() {
  return (
    <div className="flex max-w-sm flex-col gap-4">
      <Checkbox
        label="I agree to the terms of service"
        required
        error="Accept the terms to create an account."
      />
      <Checkbox
        label="Two-factor authentication"
        description="Required by your organisation."
        defaultChecked
        disabled
      />
      <Checkbox label="Beta features" disabled />
    </div>
  );
}
```

## Accessibility

| Key | Action |
| --- | --- |
| Tab | Moves focus to the checkbox. |
| Space | Toggles it. |

- The label is the accessible name. With `showLabel={false}` it is hidden visually and still announced.
- The box is 16 px with an invisible 24 px hit area. With a visible label, the whole row is clickable.
- Group related checkboxes in a `<fieldset>` with a `<legend>`.

## API reference

Every part accepts `className` and renders a `data-aloxe` attribute you can target in CSS, such as `[data-aloxe="checkbox"]`.

**Checkbox** (extends Base UI Checkbox.Root)

| Prop | Type | Default | Required | Description |
| --- | --- | --- | --- | --- |
| `label` | `ReactNode` |  | Yes | The visible label and the checkbox's accessible name. Clicking it toggles the checkbox. |
| `checked` | `boolean` |  |  | Controlled state. Pair with onCheckedChange. |
| `defaultChecked` | `boolean` | `false` |  | Initial state when uncontrolled. |
| `onCheckedChange` | `(checked: boolean, details) => void` |  |  | Called when the user toggles the checkbox. |
| `indeterminate` | `boolean` | `false` |  | Figma Mixed. Shows a dash and is announced as mixed, e.g. for a select-all box. |
| `showLabel` | `boolean` | `true` |  | false hides the label visually only. |
| `description` | `ReactNode` |  |  | Supporting text under the label, linked with aria-describedby. |
| `error` | `ReactNode` |  |  | Error message, e.g. for a required consent. Sets aria-invalid. |
| `disabled` | `boolean` | `false` |  | Disables the checkbox and dims its label. |
| `name` | `string` |  |  | Form field name. |
| `required` | `boolean` | `false` |  | Must be checked before the form submits. |
| `className` | `string` |  |  | Classes for the wrapper. |

## Figma

Matches the **Checkbox** component. Figma `Checked` maps to `checked` (`Off`, `On`) and `indeterminate` (`Mixed`). `Label` and `Show label` map to `label` and `showLabel`.

See [Figma](/docs/getting-started/figma) for where to get the file and how its names map to code.
