# Checkbox

> Circular check that fills with ink and pops its check on the spring, alone or in a group with a parent that checks them all.

Docs: https://plumbago.ochotzas.com/components/checkbox

## Installation

```sh
npx plumbago add checkbox
```

This copies `components/ui/checkbox.tsx` into your project, along with `cn`, and installs `@base-ui/react`.

## Usage

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

<label className="flex items-center gap-3">
  <Checkbox defaultChecked />
  Print a kitchen ticket
</label>
```

## Examples

### Preview

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

export default function CheckboxDemo() {
  return (
    <label className="flex items-center gap-3 font-medium">
      <Checkbox defaultChecked />
      Print a kitchen ticket
    </label>
  )
}
```

### Group with parent

```tsx
import { Checkbox, CheckboxGroup } from "@/components/ui/checkbox"
import { useState } from "react"

const stations = [
  { value: "grill", label: "Grill" },
  { value: "pastry", label: "Pastry" },
  { value: "bar", label: "Bar" },
]

export default function CheckboxGroupWithParent() {
  const [value, setValue] = useState(["grill"])
  return (
    <CheckboxGroup aria-labelledby="stations-label" value={value} onValueChange={setValue} allValues={stations.map((station) => station.value)}>
      <label id="stations-label" className="flex h-11 items-center gap-3 font-semibold">
        <Checkbox parent />
        All stations
      </label>
      {stations.map((station) => (
        <label key={station.value} className="ml-9 flex h-11 items-center gap-3">
          <Checkbox value={station.value} />
          {station.label}
        </label>
      ))}
    </CheckboxGroup>
  )
}
```

### Task list

```tsx
import { Checkbox } from "@/components/ui/checkbox"
import { Island, ListRow, ListRowSubtitle, ListRowText, ListRowTitle } from "@/components/ui/island"
import { useState } from "react"

const startingTasks = [
  { id: "oil", title: "Restock olive oil", due: "Today", done: true },
  { id: "fish", title: "Call the fish supplier", due: "Thursday", done: false },
  { id: "fryer", title: "Deep clean the fryer", due: "Saturday", done: false },
]

export default function CheckboxTaskList() {
  const [tasks, setTasks] = useState(startingTasks)
  const toggle = (id: string, done: boolean) => setTasks((list) => list.map((task) => (task.id === id ? { ...task, done } : task)))
  return (
    <Island variant="list" className="w-full max-w-md">
      {tasks.map((task) => (
        <ListRow key={task.id} done={task.done} render={<label />}>
          <Checkbox checked={task.done} onCheckedChange={(done) => toggle(task.id, done)} />
          <ListRowText>
            <ListRowTitle>{task.title}</ListRowTitle>
            <ListRowSubtitle>{task.due}</ListRowSubtitle>
          </ListRowText>
        </ListRow>
      ))}
    </Island>
  )
}
```

### Disabled

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

export default function CheckboxDisabled() {
  return (
    <div className="flex flex-col gap-1">
      <label className="flex h-11 items-center gap-3">
        <Checkbox disabled />
        Tips on receipts
      </label>
      <label className="flex h-11 items-center gap-3">
        <Checkbox defaultChecked disabled />
        Service charge
      </label>
    </div>
  )
}
```

## Built on Base UI

Checkbox wraps these Base UI parts and keeps their props:

- [Checkbox](https://base-ui.com/react/components/checkbox)
- [Checkbox group](https://base-ui.com/react/components/checkbox-group)

## Source

```tsx
import { Checkbox as BaseCheckbox } from "@base-ui/react/checkbox"
import { CheckboxGroup as BaseCheckboxGroup } from "@base-ui/react/checkbox-group"
import { cn } from "@/lib/cn"
import type { ComponentProps } from "react"

export type CheckboxProps = Omit<ComponentProps<typeof BaseCheckbox.Root>, "className"> & {
  className?: string
}

export function Checkbox({ className, ...props }: CheckboxProps) {
  return (
    <BaseCheckbox.Root
      data-slot="checkbox"
      className={cn(
        "relative grid size-6 shrink-0 cursor-pointer place-items-center rounded-full ring-[1.5px] ring-ring ring-inset transition-[background-color,box-shadow] duration-base ease-out [-webkit-tap-highlight-color:transparent]",
        "data-checked:bg-ink data-checked:ring-ink data-indeterminate:bg-ink data-indeterminate:ring-ink",
        "hover:not-data-checked:not-data-indeterminate:not-data-disabled:ring-ink-3 group-hover/row:not-data-checked:not-data-indeterminate:not-data-disabled:ring-ink-3",
        "data-disabled:cursor-default data-disabled:opacity-40",
        "after:absolute after:-inset-2.5 after:content-['']",
        className,
      )}
      {...props}
    >
      <BaseCheckbox.Indicator
        data-slot="checkbox-indicator"
        className="group/indicator grid place-items-center text-on-ink transition-[scale,opacity,filter] duration-spring ease-spring data-starting-style:scale-80 data-starting-style:opacity-0 data-starting-style:blur-[2px] data-ending-style:scale-80 data-ending-style:opacity-0 data-ending-style:blur-[2px] data-ending-style:duration-base data-ending-style:ease-in motion-reduce:blur-none"
      >
        <svg
          aria-hidden="true"
          viewBox="0 0 12 12"
          className="size-3 fill-none stroke-current group-data-indeterminate/indicator:hidden"
          strokeWidth="2.4"
          strokeLinecap="round"
          strokeLinejoin="round"
        >
          <path d="m2.25 6.25 2.5 2.5 5-5.5" />
        </svg>
        <svg
          aria-hidden="true"
          viewBox="0 0 12 12"
          className="hidden size-3 fill-none stroke-current group-data-indeterminate/indicator:block"
          strokeWidth="2.4"
          strokeLinecap="round"
        >
          <path d="M2.5 6h7" />
        </svg>
      </BaseCheckbox.Indicator>
    </BaseCheckbox.Root>
  )
}

export type CheckboxGroupProps = Omit<ComponentProps<typeof BaseCheckboxGroup>, "className"> & {
  className?: string
}

export function CheckboxGroup({ className, ...props }: CheckboxGroupProps) {
  return <BaseCheckboxGroup data-slot="checkbox-group" className={cn("flex flex-col gap-1", className)} {...props} />
}
```
