# Checkbox

> Turns an independent option on or off, including an indeterminate “some selected” state.

Category: Selection · Atomic level: atom · Built on Base UI Checkbox
Page: https://ui.dtheo.dev/docs/components/checkbox

## Install

```bash
npx dtheo@latest add checkbox
# or, with the shadcn CLI
npx shadcn@latest add https://ui.dtheo.dev/r/checkbox.json
```

Dependencies: @base-ui/react, clsx, tailwind-merge, lucide-react. Registry dependencies: utils.

## Exports

`Checkbox`, `CheckboxGroup`

## Use it for

- Selecting rows
- Options applied with a Save button

## Reach for something else

- Immediate on/off settings: use Switch

## Keyboard

| Keys | Action |
| --- | --- |
| Space | Toggles the checkbox |

## Accessibility

- The 14px box has a 24px hit area. Wrap it in a label so the text is clickable too.

## Examples

### checkbox

```tsx
import * as React from 'react';
import { Button } from '@/components/ui/button';
import { Checkbox, CheckboxGroup } from '@/components/ui/checkbox';
import { Label } from '@/components/ui/field';
import { useToast } from '@/components/ui/toast';

const channels = [
  { value: 'torque', label: 'Torque', unit: 'N·m' },
  { value: 'current', label: 'Phase current', unit: 'A' },
  { value: 'position', label: 'Position error', unit: 'mrad' },
  { value: 'temperature', label: 'Winding temperature', unit: '°C' },
];

export default function CheckboxExample() {
  const id = React.useId();
  const toast = useToast();
  const [value, setValue] = React.useState(['torque', 'current']);
  const count = value.length;
  return (
    <div className="flex w-full max-w-xs flex-col gap-4">
      {/* The parent checkbox turns indeterminate when some, but not all, channels are selected. */}
      <CheckboxGroup
        aria-labelledby={id}
        value={value}
        onValueChange={setValue}
        allValues={channels.map((c) => c.value)}
      >
        <Label id={id} className="flex items-center gap-2.5">
          <Checkbox parent />
          Export channels from R-0412
        </Label>
        <div className="flex flex-col gap-2 pl-6">
          {channels.map((c) => (
            <Label key={c.value} className="flex items-center gap-2.5 font-normal">
              <Checkbox value={c.value} />
              <span className="flex-1">{c.label}</span>
              <span className="text-caption text-subtle-foreground">{c.unit}</span>
            </Label>
          ))}
        </div>
      </CheckboxGroup>
      <Button
        className="self-start"
        disabled={count === 0}
        onClick={() =>
          toast.add({
            title: `Exporting ${count} ${count === 1 ? 'channel' : 'channels'}`,
            description: 'R-0412.csv downloads when it is ready.',
          })
        }
      >
        {count === 0
          ? 'Select a channel to export'
          : `Export ${count} ${count === 1 ? 'channel' : 'channels'}`}
      </Button>
    </div>
  );
}
```

## Used in

- Data table pattern (template): https://ui.dtheo.dev/docs/patterns/data-table
