Checkbox
A control for a binary or tri-state choice, built on Radix Checkbox. Supports the indeterminate state a “select all” needs. It's the same control the DataTable uses for row selection.
Installation#
Usage#
import { Checkbox } from "@/components/ui/checkbox"
export function Example() {
return (
<label htmlFor="terms" className="flex items-center gap-2.5">
<Checkbox id="terms" defaultChecked />
Accept terms and conditions
</label>
)
}Anatomy#
import { Checkbox, CheckboxGroup } from "@/components/ui/checkbox"
<Checkbox />
<CheckboxGroup allValues={[…]}>
<Checkbox parent />
<Checkbox value="…" />
<CheckboxGroup allValues={[…]}>
<Checkbox parent />
<Checkbox value="…" />
</CheckboxGroup>
</CheckboxGroup>States#
Checked and indeterminate both fill with the primary (ink) color; indeterminate shows a minus rather than a check. Drive the tri-state by passing checked="indeterminate".
Sizes#
sm (16px) sits inside dense tables; md (20px) is the default for forms. The indicator scales with the box.
With a label#
Checkbox renders only the box; pair it with a <label htmlFor> for an accessible name and a larger hit target. With a description, nudge the box to the first line.
Group#
CheckboxGroup holds one string[] for a whole set. Each member is a plain Checkbox with a value, so there is no per-box state to keep. Use defaultValue, or value with onValueChange to control it. Name the group with aria-labelledby.
Select all#
Pass allValues and add a Checkbox parent. It reads checked when every child is, indeterminate on a partial selection and unchecked when none are, and a click toggles them all. From a partial selection it cycles through all, none, then back to the selection you had, so a stray click never loses it. The same tri-state the DataTable header uses.
Nested#
Put a CheckboxGroup inside another and leave out its value: it shares the outer group’s list and only scopes its own allValues. A tree of permissions stays one flat array, and every parent at every level follows its children with no wiring.
API reference#
Checkbox
The box, Radix Checkbox.Root. Renders a <button role="checkbox"> and forwards all its props. Pair it with a <label htmlFor>.
| Prop | Type | Default | Description |
|---|---|---|---|
| checked | boolean | "indeterminate" | - | Controlled state. Ignored inside a CheckboxGroup, where the group decides. |
| defaultChecked | boolean | "indeterminate" | false | Initial state when uncontrolled. |
| onCheckedChange | (checked: boolean | "indeterminate") => void | - | Called when the box toggles, inside a group too. |
| value | string | "on" | The form value. Inside a CheckboxGroup it is also the member's identity in the group's list. |
| parent | boolean | false | Inside a CheckboxGroup: governs the group's allValues, reading checked, indeterminate or unchecked from them. |
| size | "sm" | "md" | "md" | 16px for dense tables, 20px for forms. |
| disabled | boolean | false | Disables the box. Falls back to the group's, then the Field's. |
| name | string | - | Submitted with the form when checked. |
| required | boolean | false | Marks the box required in a form. |
| Data attribute | Description |
|---|---|
| data-state | "checked", "unchecked" or "indeterminate". |
| data-disabled | Present when disabled. |
| data-parent | Present on a parent box inside a group. |
CheckboxGroup
A set of checkboxes sharing one string[]. Renders a <div role="group">, a column with a 12px gap; name it with aria-labelledby or aria-label. Forwards all native <div> props.
| Prop | Type | Default | Description |
|---|---|---|---|
| value | string[] | - | The checked values (controlled). |
| defaultValue | string[] | [] | The values checked at first (uncontrolled). |
| onValueChange | (value: string[]) => void | - | Called with the new list when a member or a parent toggles. |
| allValues | string[] | - | Every value the group's parent box governs. Required for a parent. On a nested group, its slice of the outer list. |
| disabled | boolean | false | Disables every checkbox in the group. |
| Data attribute | Description |
|---|---|
| data-disabled | Present when the group is disabled. |