# Checkbox

A drawn box that ticks with a pull up and out past the corner, and dashes when indeterminate.

Docs: https://ballpoint.st9wd.com/docs/checkbox

```bash
npx shadcn@latest add @ballpoint/checkbox
```

```tsx
import { Checkbox } from "@/components/ui/checkbox"
import { Label } from "@/components/ui/label"

<Label>
  <Checkbox defaultChecked />
  Send me the newsletter
</Label>
```

Props:

- `indeterminate`: `boolean`. Draws a dash instead of a tick.
- `seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `roughness`, `passes`, `radius`, `corners`, `weight`, `speed` (see Pen settings).

Every other prop goes to Base UI's Checkbox: https://base-ui.com/react/components/checkbox

## Example

```tsx
"use client";

import { useState } from "react";
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel } from "@/components/ui/field";
import { Label } from "@/components/ui/label";

const items = ["Paper", "Ink", "A steady hand"];

export default function CheckboxDemo() {
  const [picked, setPicked] = useState(["Paper"]);
  const all = picked.length === items.length;
  return (
    <FieldGroup className="max-w-md">
      <Field orientation="horizontal">
        <Checkbox id="cb-terms" defaultChecked seed="cb-terms" />
        <FieldContent>
          <FieldLabel htmlFor="cb-terms">Send me the newsletter</FieldLabel>
          <FieldDescription>Once a month, written by hand. Well, typed.</FieldDescription>
        </FieldContent>
      </Field>
      <div className="flex flex-col gap-3">
        <Label>
          <Checkbox
            seed="cb-all"
            checked={all}
            indeterminate={picked.length > 0 && !all}
            onCheckedChange={(checked) => setPicked(checked ? items : [])}
          />
          Everything you need
        </Label>
        {items.map((item) => (
          <Label key={item} className="pl-7">
            <Checkbox
              seed={`cb-${item}`}
              checked={picked.includes(item)}
              onCheckedChange={(checked) => setPicked((p) => (checked ? [...p, item] : p.filter((x) => x !== item)))}
            />
            {item}
          </Label>
        ))}
      </div>
      <Label>
        <Checkbox disabled seed="cb-disabled" />
        Disabled
      </Label>
    </FieldGroup>
  );
}
```

## States

Unchecked, checked, mixed, disabled, and invalid with an error.

```tsx
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldContent, FieldDescription, FieldError, FieldLabel } from "@/components/ui/field";
import { Label } from "@/components/ui/label";

export default function CheckboxStates() {
  return (
    <div className="grid w-full max-w-md gap-5">
      <div className="flex flex-wrap gap-x-8 gap-y-4">
        <Label>
          <Checkbox seed="cs-off" />
          Unchecked
        </Label>
        <Label>
          <Checkbox defaultChecked seed="cs-on" />
          Checked
        </Label>
        <Label>
          <Checkbox indeterminate seed="cs-mixed" />
          Mixed
        </Label>
      </div>
      <div className="flex flex-wrap gap-x-8 gap-y-4">
        <Label>
          <Checkbox disabled seed="cs-disabled" />
          Disabled
        </Label>
        <Label>
          <Checkbox disabled defaultChecked seed="cs-disabled-on" />
          Disabled, checked
        </Label>
      </div>
      <Field orientation="horizontal" data-invalid>
        <Checkbox id="cs-terms" aria-invalid seed="cs-terms" />
        <FieldContent>
          <FieldLabel htmlFor="cs-terms">Accept the terms</FieldLabel>
          <FieldDescription>You need to agree to continue.</FieldDescription>
          <FieldError>Please tick the box.</FieldError>
        </FieldContent>
      </Field>
    </div>
  );
}
```

## Choice cards

Wrap a Field in FieldLabel and the whole row becomes the target.

```tsx
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldContent, FieldDescription, FieldGroup, FieldLabel, FieldTitle } from "@/components/ui/field";

const extras = [
  ["gift", "Gift wrap", "Tied with string."],
  ["note", "Handwritten note", "In blue ballpoint, naturally."],
  ["express", "Express post", "Arrives in two days."],
] as const;

// Wrap a whole Field in FieldLabel and the entire row becomes the click target.
export default function CheckboxCards() {
  return (
    <FieldGroup className="w-full max-w-md gap-4">
      {extras.map(([id, title, description], i) => (
        <FieldLabel key={id} htmlFor={`cc-${id}`}>
          <Field orientation="horizontal">
            <Checkbox id={`cc-${id}`} defaultChecked={i === 1} seed={`cc-${id}`} />
            <FieldContent>
              <FieldTitle>{title}</FieldTitle>
              <FieldDescription>{description}</FieldDescription>
            </FieldContent>
          </Field>
        </FieldLabel>
      ))}
    </FieldGroup>
  );
}
```

## Select all

A parent that is mixed when only some children are ticked.

```tsx
"use client";

import { useState } from "react";
import { Checkbox } from "@/components/ui/checkbox";
import { Label } from "@/components/ui/label";

const items = ["Notebooks", "Stamps", "Stickers", "Tape"];

export default function CheckboxSelectAll() {
  const [picked, setPicked] = useState<string[]>(["Stamps", "Tape"]);
  const all = picked.length === items.length;
  return (
    <div className="flex flex-col gap-4">
      <Label className="font-bold">
        <Checkbox
          checked={all}
          indeterminate={picked.length > 0 && !all}
          onCheckedChange={(on) => setPicked(on ? items : [])}
          seed="csa-all"
        />
        Everything on the desk
      </Label>
      <div className="flex flex-col gap-3 pl-8">
        {items.map((item) => (
          <Label key={item}>
            <Checkbox checked={picked.includes(item)} onCheckedChange={(on) => setPicked((list) => (on ? [...list, item] : list.filter((x) => x !== item)))} seed={`csa-${item}`} />
            {item}
          </Label>
        ))}
      </div>
    </div>
  );
}
```

## In a form

A required box with a description and an error after a try.

```tsx
"use client";

import { useState } from "react";
import { Button } from "@/components/ui/button";
import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldDescription, FieldError, FieldGroup, FieldLabel } from "@/components/ui/field";

export default function CheckboxForm() {
  const [agreed, setAgreed] = useState(false);
  const [tried, setTried] = useState(false);
  const [done, setDone] = useState(false);
  const invalid = tried && !agreed;
  return (
    <form
      className="w-full max-w-md"
      onSubmit={(event) => {
        event.preventDefault();
        setTried(true);
        setDone(agreed);
      }}
    >
      <FieldGroup>
        <Field orientation="horizontal" data-invalid={invalid || undefined}>
          <Checkbox id="cf-agree" checked={agreed} onCheckedChange={(on) => setAgreed(on)} aria-invalid={invalid} seed="cf-agree" />
          <div className="grid gap-1">
            <FieldLabel htmlFor="cf-agree">I have read the terms</FieldLabel>
            <FieldDescription>Nothing in them is surprising. We checked.</FieldDescription>
            {invalid && <FieldError>Tick the box to go on.</FieldError>}
          </div>
        </Field>
        <div className="flex items-center gap-4">
          <Button type="submit" seed="cf-submit">
            Sign up
          </Button>
          {done && <span className="text-ink-2">Welcome aboard.</span>}
        </div>
      </FieldGroup>
    </form>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `passes`: `1 | 2 | 3`. How many times an outline is gone over. Each component picks its own default.
- `radius`: `number | "full"` (default `0`). Corner radius in px; "full" draws a pill.
- `corners`: `"crossed" | "joined"` (default `"crossed"`). Square corners: sides pulled separately past each other, or the box drawn in one motion.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
