Checkbox
Web/MobileA control that toggles between checked, unchecked, and indeterminate. Compose with Label via id and htmlFor.
Installation
pnpm dlx cubix-ui@latest add checkboxUsage
import { Label } from "@/components/cubix/label"
import { Checkbox } from "@/components/cubix/checkbox"
<div className="flex items-center gap-2">
<Checkbox id="checkbox-demo-terms" defaultChecked />
<Label htmlFor="checkbox-demo-terms">پذیرش قوانین و شرایط</Label>
</div>
Composition
Checkbox is the control only. Pair it with Label using matching id and htmlFor. Use indeterminate for partial selection and aria-invalid for error states.
div
├── Checkbox (id)
└── Label (htmlFor)Accessibility
Every base exposes a native checkbox role with aria-checked, including the mixed state for indeterminate. Space toggles the focused control, and clicking the Label toggles it too. Link helper or error text with aria-describedby, and group related options in a fieldset with a legend. While pending is set the control is disabled and reports aria-busy.
Examples
With description
Add a helper line for consent and settings rows, linked with aria-describedby.
ماهی یک ایمیل درباره قابلیتهای جدید و تغییرات مهم دریافت میکنید.
Indeterminate
A parent checkbox shows indeterminate while only some children are selected, and toggles all of them at once.
Invalid
Set aria-invalid when a required choice is missing. The control gets a destructive border and ring; show the error copy below it.
برای ادامه باید قوانین را بپذیرید.
Disabled
Disabled checkboxes cannot be toggled. Checked and indeterminate controls fade to reduced opacity.
Pending
Set pending while a save is in flight. The square is replaced by a circular spinner and interaction is blocked until the request finishes.
Form
Give each checkbox a name and value so checked options are submitted with the form.
API Reference
Note: Parts render data-slot attributes (checkbox, checkbox-indicator) for targeting in tests and parent selectors. Use the same props on Base UI, React Aria, and Radix.
Checkbox
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | boolean | - | Controlled checked state. Pair with onCheckedChange when the parent owns the value. |
| defaultChecked | boolean | false | Initial checked state for uncontrolled usage. |
| onCheckedChange | (checked: boolean) => void | - | Called with the next boolean value whenever the checkbox is toggled. |
| indeterminate | boolean | false | Shows the mixed state (minus icon). Use for partial selection in grouped options. |
| disabled | boolean | false | Disables the control and blocks interaction. |
| pending | boolean | false | Shows a circular spinner in place of the box while a save is in flight. Blocks interaction and sets aria-busy. |
| required | boolean | false | Marks the control as required for form validation. |
| name | string | - | Name submitted with the form when the checkbox is checked. |
| value | string | "on" | Value submitted with the form when the checkbox is checked. |
| id | string | - | Associates the control with a Label through htmlFor for accessible naming. |
| aria-invalid | boolean | false | Marks the control as invalid. Adds a destructive border and ring; link the error text with aria-describedby. |
| className | string | - | Additional Tailwind classes merged with the control styles (last one wins). |
States
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | - | - | Primary fill with a check icon. |
| indeterminate | - | - | Primary fill with a minus icon for partial selection. |
| disabled | - | - | Unchecked stays full opacity; checked or indeterminate use reduced opacity. |
| pending | - | - | Borderless control with a spinning loader; interaction is blocked. |
| aria-invalid | - | - | Destructive border and ring. Show error copy below the control. |