# Checkbox

Checkbox with checked, unchecked and indeterminate states.

Docs: https://corsairui.vercel.app/docs/checkbox

## Install

```bash
pnpm dlx shadcn@latest add @corsair-ui/checkbox
```

Also adds: `utils`.
npm: `@radix-ui/react-checkbox`, `clsx`, `lucide-react`, `tailwind-merge`.

## API

Built on Radix Checkbox (https://www.radix-ui.com/primitives/docs/components/checkbox); its props pass through.

## Styling

Restyle it in the project that installs it: `className` on each part, the theme variables, and these attributes with arbitrary variants (`[&_[data-slot=…]]:…`, `data-[state=…]:…`).

data-slot: `checkbox`, `checkbox-indicator`

## Accessibility

- Space toggles it; checked="indeterminate" is announced as mixed.
- The mark animates only when motion is allowed.

## Examples

On the docs page: Default, States, Select all. The usage examples in the source comments below are the reference.

## Source

What `shadcn add` writes into the project:

### checkbox.tsx

```tsx
"use client";

import * as CheckboxPrimitive from "@radix-ui/react-checkbox";
import { CheckIcon, MinusIcon } from "lucide-react";
import type { ComponentProps } from "react";

import { cn } from "@/registry/default/lib/utils";

/** Pass `checked="indeterminate"` for a partially selected group. Pair it with a Label. */
function Checkbox({ className, ...props }: ComponentProps<typeof CheckboxPrimitive.Root>) {
  return (
    <CheckboxPrimitive.Root
      data-slot="checkbox"
      className={cn(
        "peer group border-input bg-field text-primary-foreground size-4 shrink-0 cursor-pointer rounded-[4px] border",
        "transition-[color,background-color,border-color,box-shadow] outline-none motion-reduce:transition-none",
        "focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px]",
        "aria-[invalid=true]:border-destructive aria-[invalid=true]:ring-destructive/20",
        "data-[state=checked]:border-primary data-[state=checked]:bg-primary",
        "data-[state=indeterminate]:border-primary data-[state=indeterminate]:bg-primary",
        "disabled:cursor-not-allowed disabled:opacity-50",
        className
      )}
      {...props}
    >
      {/* The mark scales in while the box fills, and back out when cleared. */}
      <CheckboxPrimitive.Indicator
        data-slot="checkbox-indicator"
        className={cn(
          "flex items-center justify-center text-current",
          "zoom-in-50 fade-in-0 zoom-out-50 fade-out-0 ease-out",
          "motion-safe:data-[state=checked]:animate-in motion-safe:data-[state=indeterminate]:animate-in motion-safe:data-[state=unchecked]:animate-out"
        )}
      >
        <CheckIcon className="size-3.5 group-data-[state=indeterminate]:hidden" />
        <MinusIcon className="hidden size-3.5 group-data-[state=indeterminate]:block" />
      </CheckboxPrimitive.Indicator>
    </CheckboxPrimitive.Root>
  );
}

export { Checkbox };
```
