# Checkbox

> A native checkbox with a label, optional description, and a mixed state for partially selected groups.

- Kind: ui · Category: Forms · Interactivity: script
- Install: `npx shadcn@latest add @haze/checkbox`
- File: `src/components/haze/ui/checkbox.astro`
- npm dependencies: @lucide/astro
- Registry dependencies: @haze/utils
- Docs page: https://hazeui.com/ui/checkbox

## Use when
- Accepting terms or opting in to emails
- Independent yes/no settings that are saved with a submit button
- A list where several options can be on at once

## Avoid when
- Settings that apply the moment they are flipped; use switch
- Exactly one choice from a group; use radio-group

## Alternatives
- [switch](https://hazeui.com/ui/switch.md)
- [radio-group](https://hazeui.com/ui/radio-group.md)
- [field](https://hazeui.com/ui/field.md)

## Props

| Name | Type | Default | Required | Description |
|---|---|---|---|---|
| id | `string` |  | yes | Input id; the label's for and the description id (<id>-description) derive from it. |
| name | `string` | id | no | Form field name. |
| label | `string` |  | yes | Visible label text. |
| description | `string` |  | no | Secondary line; the input points at it with aria-describedby. |
| checked | `boolean` |  | no | Initial checked state. |
| indeterminate | `boolean` |  | no | Shows a mixed state initially and after a form reset. Checked still determines the submitted value. |
| disabled | `boolean` |  | no | Dims the box and its label (the description stays readable) and blocks input. |
| value | `string` |  | no | Value submitted when checked; the browser default is "on". |
| class | `string` |  | no | Extra classes on the wrapper. |
| ...rest | `attributes` |  | no | Any other input attribute, for example required or aria-invalid. |

## Accessibility
- The input stays a real checkbox, so checked state, form submission and the label click target all come from the browser.
- The mixed-state indicator uses the native indeterminate property. Without JavaScript the checked state remains available.
- The check mark scales in over 200 ms and a press shrinks the box. The box border is foreground at 40%, which meets 3:1 against the surface.
- In Windows forced-colors mode the custom drawing is dropped (appearance-auto) and the system checkbox shows instead.
- The description is linked with aria-describedby only when it exists, so no dangling id reaches the accessibility tree.
- Key Space: Toggles the checkbox
- Key Tab: Moves to the next control

## Agent prompts

### Install and use

Install the Haze UI component "Checkbox" with `npx shadcn@latest add @haze/checkbox`. If components.json has no "@haze" registry yet, add "registries": { "@haze": "https://hazeui.com/r/{name}.json" }. If src/styles/haze.css does not exist yet, run `npx shadcn@latest add @haze/theme` once and import it from the global stylesheet after Tailwind. The CLI stops at an overwrite question when a Haze file already exists and differs, even with --yes, and then installs nothing after it: read its output. If the user has not edited their Haze files, re-run with --overwrite to take the registry versions; if they have, ask them before overwriting. Then import Checkbox from "@/components/haze/ui/checkbox.astro" (or the relative path) and render it where it fits the page. Keep Haze token classes (bg-background, text-muted-foreground, border-border...) and never hardcode colors. Docs: https://hazeui.com/ui/checkbox.md

### Customize with real content

Adapt the Haze checkbox ui for our product. Replace the demo values of these props with our real content: id, name, label, description, checked, indeterminate, disabled, value, class, ...rest. Keep the layout, the heading level rules and these accessibility details: The input stays a real checkbox, so checked state, form submission and the label click target all come from the browser. The mixed-state indicator uses the native indeterminate property. Without JavaScript the checked state remains available. The check mark scales in over 200 ms and a press shrinks the box. The box border is foreground at 40%, which meets 3:1 against the surface. In Windows forced-colors mode the custom drawing is dropped (appearance-auto) and the system checkbox shows instead. The description is linked with aria-describedby only when it exists, so no dangling id reaches the accessibility tree. Do not add raw color classes; use the Haze tokens.

### Choose the right block

Before using checkbox, check it fits: use it when accepting terms or opting in to emails; independent yes/no settings that are saved with a submit button; a list where several options can be on at once. Avoid it when settings that apply the moment they are flipped; use switch; exactly one choice from a group; use radio-group. Alternatives: @haze/switch, @haze/radio-group, @haze/field.

## Example requests that should lead here
- Add a terms checkbox above the sign-up button
- Create a list of notification checkboxes with descriptions

## Source

```astro
---
import { Check, Minus } from "@lucide/astro";
import { cn } from "../../../lib/haze/utils";

interface Props {
  id: string;
  name?: string;
  label: string;
  description?: string;
  checked?: boolean;
  indeterminate?: boolean;
  disabled?: boolean;
  value?: string;
  class?: string;
  /** Any other input attribute, for example required, aria-invalid or form. */
  [key: string]: unknown;
}

const { id, name = id, label, description, checked, indeterminate, disabled, value, class: className, ...rest } = Astro.props;
---

<div class={cn("flex items-start gap-3 has-[:disabled]:[&>span]:opacity-50 has-[:disabled]:[&_label]:opacity-50", className)}>
  <span class="relative mt-0.75 inline-grid size-4.5 shrink-0 place-items-center">
    <input
      id={id}
      name={name}
      type="checkbox"
      value={value}
      checked={checked}
      data-haze-checkbox={indeterminate ? "" : undefined}
      disabled={disabled}
      aria-describedby={description ? `${id}-description` : undefined}
      class="peer col-start-1 row-start-1 size-4.5 cursor-pointer appearance-none rounded-sm border border-border-strong bg-field transition-[background-color,border-color,scale] duration-150 ease-snappy hover:border-foreground active:scale-90 active:duration-75 checked:border-primary checked:hover:border-primary checked:bg-primary indeterminate:border-primary indeterminate:bg-primary focus-visible:outline-2 focus-visible:outline-offset-1 focus-visible:outline-ring disabled:cursor-not-allowed disabled:hover:border-border-strong disabled:active:scale-100 aria-invalid:border-destructive! forced-colors:appearance-auto"
      {...rest}
    />
    <Check
      class="pointer-events-none col-start-1 row-start-1 size-3.5 scale-50 text-primary-foreground opacity-0 transition-[opacity,scale] duration-200 ease-spring peer-checked:scale-100 peer-checked:opacity-100 peer-indeterminate:opacity-0 forced-colors:hidden"
      stroke-width={3}
      aria-hidden="true"
    />
    <Minus class="pointer-events-none col-start-1 row-start-1 size-3.5 scale-50 text-primary-foreground opacity-0 transition-[opacity,scale] duration-200 ease-spring peer-indeterminate:scale-100 peer-indeterminate:opacity-100 forced-colors:hidden" stroke-width={3} aria-hidden="true" />
  </span>
  <div class="grid gap-1">
    <label for={id} class="text-[0.8125rem]/6 font-medium text-foreground">{label}</label>
    {description && <p id={`${id}-description`} class="text-[0.8125rem]/5 text-muted-foreground">{description}</p>}
  </div>
</div>

<script>
  const init = () => {
    for (const input of document.querySelectorAll<HTMLInputElement>("[data-haze-checkbox]:not([data-enhanced])")) {
      input.indeterminate = true;
      input.form?.addEventListener("reset", () => { input.indeterminate = true; });
      input.setAttribute("data-enhanced", "");
    }
  };
  init();
  if (!document.documentElement.hasAttribute("data-haze-checkbox-listener")) {
    document.documentElement.setAttribute("data-haze-checkbox-listener", "");
    document.addEventListener("astro:page-load", init);
  }
</script>
```
