Checkbox
A native checkbox with a label, optional description, and a mixed state for partially selected groups.
pnpm dlx shadcn@latest add @haze/checkbox---
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>When to use
- 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
When not to use
- Settings that apply the moment they are flipped; use switch
- Exactly one choice from a group; use radio-group
Installation
Add the Haze registry to components.json once, then install Checkbox with the shadcn CLI. Works in any Astro project with Tailwind CSS v4.
pnpm dlx shadcn@latest add @haze/checkboxnpx shadcn@latest add @haze/checkboxyarn dlx shadcn@latest add @haze/checkboxbunx --bun shadcn@latest add @haze/checkboxFirst time? Registry and theme setup
1. Add to components.json:
{
"registries": {
"@haze": "http://localhost:4329/r/{name}.json"
}
}2. Install the theme once: npx shadcn@latest add @haze/theme, then import it after Tailwind in your global CSS:@import "./haze.css";
API reference
| Prop | Type | Default | Description |
|---|---|---|---|
| id* | string | – | Input id; the label's for and the description id (<id>-description) derive from it. |
| name | string | id | Form field name. |
| label* | string | – | Visible label text. |
| description | string | – | Secondary line; the input points at it with aria-describedby. |
| checked | boolean | – | Initial checked state. |
| indeterminate | boolean | – | Shows a mixed state initially and after a form reset. Checked still determines the submitted value. |
| disabled | boolean | – | Dims the box and its label (the description stays readable) and blocks input. |
| value | string | – | Value submitted when checked; the browser default is "on". |
| class | string | – | Extra classes on the wrapper. |
| ...rest | attributes | – | Any other input attribute, for example required or aria-invalid. |
Keyboard interactions
- Space
- Toggles the checkbox
- Tab
- Moves to the next control
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.
Notes for AI
Copy into Claude Code, Cursor, Codex or any coding agent. Agents can also read the Markdown version of this page.
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": "http://localhost:4329/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: http://localhost:4329/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.
Dependencies
- npm: @lucide/astro
- @haze/utils
- @haze/theme (one-time setup)
Source · Original Haze UI work.