Checkbox
A control element that allows for multiple selections within a set.
Anatomy
<Checkbox.Root>
<Checkbox.Control>
<Checkbox.Indicator />
</Checkbox.Control>
<Checkbox.Label />
<Checkbox.HiddenInput />
</Checkbox.Root>
Examples
Default Checked
Set defaultChecked for the initial state. The checkbox then manages its own state.
Controlled
Use checked and onCheckedChange to control the state.
Root Provider
Use RootProvider with the useCheckbox hook to access the state and methods from outside the component.
Disabled
Set disabled to make the checkbox non-interactive.
Indeterminate
Set indeterminate to show a partially checked state.
Field
Wrap the checkbox in Field to wire up helper text, error text and form state accessibly.
Form
Pass name and value to Checkbox.Root to submit the checkbox value with a form.
Context
Access the checkbox's state and methods with Checkbox.Context or the useCheckboxContext hook.
Checkbox Group
Checkbox.Group manages the state of a set of checkboxes and exposes the checked values.
<Checkbox.Group>
<Checkbox.Root>
<Checkbox.Control>
<Checkbox.Indicator />
</Checkbox.Control>
<Checkbox.Label />
<Checkbox.HiddenInput />
</Checkbox.Root>
</Checkbox.Group>
Controlled
Use value and onValueChange to control the group. The value is an array of checked values.
Root Provider
Create the group store with useCheckboxGroup and pass it to Checkbox.GroupProvider. This works like RootProvider
for a single checkbox.
Invalid
Set invalid on Checkbox.Group to mark every checkbox in the group as invalid.
Max Selected
Set maxSelectedValues to cap how many checkboxes can be checked. Once the cap is reached, the rest are disabled.
Select All
A parent checkbox can toggle every item in the group. It shows an indeterminate state when some items are checked and is checked when all are.
Form
Set name on Checkbox.Group to submit the checked values with a form. Read them as an array with FormData.getAll().
Fieldset
Wrap Checkbox.Group in Fieldset for a semantic group with a legend, helper text and error text.
Guides
asChild Behavior
Checkbox.Root renders a label element so the control is always labelled. Without it, the HTML and accessibility
structure is invalid.
When you use
asChild, make thelabelelement the direct child ofCheckbox.Root.
API Reference
Props
Root
Defines the <label> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
checked | CheckedStateThe controlled checked state of the checkbox | |
defaultChecked | CheckedStateThe initial checked state of the checkbox when rendered. Use when you don't need to control the checked state of the checkbox. | |
disabled | booleanWhether the checkbox is disabled | |
form | stringThe id of the form that the checkbox belongs to. | |
id | stringThe unique identifier of the machine. | |
ids | ElementIdsThe ids of the elements in the checkbox. Useful for composition. | |
invalid | booleanWhether the checkbox is invalid | |
name | stringThe name of the input field in a checkbox. Useful for form submission. | |
onCheckedChange | (details: CheckedChangeDetails) => voidThe callback invoked when the checked state changes. | |
readOnly | booleanWhether the checkbox is read-only | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<CheckboxRootState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
required | booleanWhether the checkbox is required | |
value | 'on' | stringThe value of checkbox input. Useful for form submission. |
| Attribute | Description |
|---|---|
[data-checkbox-root] | <uid> |
[data-active] | Present when active or pressed |
[data-focus] | Present when focused |
[data-focus-visible] | Present when focused with keyboard |
[data-readonly] | Present when read-only |
[data-hover] | Present when hovered |
[data-disabled] | Present when disabled |
[data-state] | "indeterminate" | "checked" | "unchecked" |
[data-invalid] | Present when invalid |
[data-required] | Present when required |
Control
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-checkbox-control] | <uid> |
[data-active] | Present when active or pressed |
[data-focus] | Present when focused |
[data-focus-visible] | Present when focused with keyboard |
[data-readonly] | Present when read-only |
[data-hover] | Present when hovered |
[data-disabled] | Present when disabled |
[data-state] | "indeterminate" | "checked" | "unchecked" |
[data-invalid] | Present when invalid |
[data-required] | Present when required |
Group
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
defaultValue | string[]The initial value of `value` when uncontrolled | |
disabled | booleanIf `true`, the checkbox group is disabled | |
invalid | booleanIf `true`, the checkbox group is invalid | |
maxSelectedValues | numberThe maximum number of selected values | |
name | stringThe name of the input fields in the checkbox group (Useful for form submission). | |
onValueChange | (value: string[]) => voidThe callback to call when the value changes | |
readOnly | booleanIf `true`, the checkbox group is read-only | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. | |
value | string[]The controlled value of the checkbox group |
GroupProvider
Defines the <div> element.
| Attribute | Default | Type |
|---|---|---|
value | UseCheckboxGroupContext | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
HiddenInput
Defines the <input> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Indicator
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
indeterminate | boolean | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-checkbox-indicator] | <uid> |
[data-active] | Present when active or pressed |
[data-focus] | Present when focused |
[data-focus-visible] | Present when focused with keyboard |
[data-readonly] | Present when read-only |
[data-hover] | Present when hovered |
[data-disabled] | Present when disabled |
[data-state] | "indeterminate" | "checked" | "unchecked" |
[data-invalid] | Present when invalid |
[data-required] | Present when required |
Label
Defines the <span> element.
| Attribute | Default | Type |
|---|---|---|
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<EmptyState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
| Attribute | Description |
|---|---|
[data-checkbox-label] | <uid> |
[data-active] | Present when active or pressed |
[data-focus] | Present when focused |
[data-focus-visible] | Present when focused with keyboard |
[data-readonly] | Present when read-only |
[data-hover] | Present when hovered |
[data-disabled] | Present when disabled |
[data-state] | "indeterminate" | "checked" | "unchecked" |
[data-invalid] | Present when invalid |
[data-required] | Present when required |
RootProvider
Defines the <label> element.
| Attribute | Default | Type |
|---|---|---|
value | UseCheckboxReturn | |
asChild | booleanUse the provided child element as the default rendered element, combining their props and behavior. Use `render` when you need the part's state: `render={(props, state) => <MyButton {...props} />}`. For more details, read our Composition guide. | |
render | ReactElement<unknown, string | JSXElementConstructor<any>> | RenderFn<CheckboxRootProviderState>Render the part as a custom element, combining their props and behavior. Pass an element to have the part's props merged into it, or a function to control the merge yourself and read the part's state. |
Context
API
| Property | Type |
|---|---|
checked | booleanWhether the checkbox is checked |
disabled | boolean | undefinedWhether the checkbox is disabled |
indeterminate | booleanWhether the checkbox is indeterminate |
focused | boolean | undefinedWhether the checkbox is focused |
checkedState | CheckedStateThe checked state of the checkbox |
setChecked | (checked: CheckedState) => voidFunction to set the checked state of the checkbox |
toggleChecked | VoidFunctionFunction to toggle the checked state of the checkbox |
getRootState | () => RootStateReturns the state of the checkbox |
Accessibility
Complies with the Checkbox WAI-ARIA design pattern.
Keyboard Support
| Key | Description |
|---|---|
Space | Toggle the checkbox |