# Checkbox

A box people tick to choose any number of options, or to agree to something.

```tsx
import { Checkbox } from '@halo-ds/react';
```

## When to use it
Use checkboxes when people can pick none, one or several options from a list, or for a single yes or no that takes effect when a form is sent. Checkbox is the box on its own; Checkbox field adds a label and description; Checkbox group holds a labelled list of fields in a slot.

## Do
- Write labels as positive statements, like "Send me updates".
- Keep lists short, about 10 options at most.
- Put options in a logical order, like most common first.
- Show an error message next to a box in the Error state.

## Don't
- Use a checkbox for a setting that changes straight away; use a Switch.
- Pre-tick boxes for marketing or legal consent.
- Leave a checkbox without a label.
- Put a link inside a label; link to the terms next to it instead.

## Checkbox, Radio or Switch?
All three make choices. Pick by how many options there are, and when the choice takes effect.
- **Checkbox**: Pick none, one or several options from a list. The choice takes effect when the form is sent. Also for a single "I agree" before continuing.
- **Radio or Switch**: Radio: pick exactly one option from a short list. Switch: turns one setting on or off straight away. Don't use a checkbox for something that changes immediately.

## Props
### Checkbox props
| Prop | Type | What it does |
| --- | --- | --- |
| `label` | `ReactNode` | What ticking the box means. Leave it out only when the box is labelled some other way. Figma: Label. |
| `description` | `ReactNode` | Extra detail under the label. Figma: Description. |
| `size` | `CheckboxSize \| undefined` | 16px (sm) or 20px (md) box. Figma: Size. |
| `indeterminate` | `boolean \| undefined` | Shows a dash for "some, not all" options chosen. Read as "mixed". Figma: Checked=Indeterminate. |
| `invalid` | `boolean \| undefined` | Shows the error look and tells screen readers the value is invalid. Figma: State=Error. |
| `onCheckedChange` | `((checked: boolean) => void) \| undefined` | Called with true or false when the box is ticked or unticked. |
| `classNames` | `Partial<Record<CheckboxPart, string>> \| undefined` | Extra class names for the parts inside the component, by part name, like `{ header: 'my-header' }`. |
| `styles` | `Partial<Record<CheckboxPart, CSSProperties>> \| undefined` | Inline styles for the parts inside the component, by part name, like `{ footer: { gap: 4 } }`. |
It also takes the usual HTML attributes for its element.

## Examples
### Overview
```tsx
import { Checkbox } from '@halo-ds/react';

export default function Example() {
  return (
    <div style={{ display: 'grid', gap: 12 }}>
      <Checkbox label="Email me about new comments" defaultChecked />
      <Checkbox label="Weekly summary" description="Every Monday morning." />
      <Checkbox label="Accept the terms" invalid />
    </div>
  );
}
```

### Checked
Off, on and indeterminate, in both sizes.
```tsx
import { Checkbox } from '@halo-ds/react';

export default function Demo() {
  return (
    <div className="demo-row" style={{ alignItems: 'flex-start', gap: 40 }}>
      {(['sm', 'md'] as const).map((s) => (
        <div key={s} className="demo-stack">
          <Checkbox size={s} label="Off" />
          <Checkbox size={s} label="On" defaultChecked />
          <Checkbox size={s} label="Indeterminate" indeterminate />
        </div>
      ))}
    </div>
  );
}
```

### Group
A labelled group, read as one question by screen readers.
```tsx
import { Checkbox, CheckboxGroup } from '@halo-ds/react';

export default function Demo() {
  return (
    <CheckboxGroup label="Notifications">
      <Checkbox label="Email" description="A weekly summary and account updates." defaultChecked />
      <Checkbox label="Push notifications" description="Mentions, replies and reminders." defaultChecked />
      <Checkbox label="Text messages" description="Only for sign-in codes." />
    </CheckboxGroup>
  );
}
```

### Disabled and error
Pair an error with a message that says what to do.
```tsx
import { Checkbox } from '@halo-ds/react';

export default function Demo() {
  return (
    <div className="demo-stack">
      <Checkbox label="Disabled" disabled />
      <Checkbox label="Disabled and ticked" disabled defaultChecked />
      <Checkbox label="I agree to the terms" invalid description="Tick this box to continue." />
    </div>
  );
}
```

More: https://halodesignsystem.dev/components/checkbox
