Checkbox

A control element that allows for multiple selections within a set.

Loading...

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 the label element the direct child of Checkbox.Root.

API Reference

Props

Root

Defines the <label> element.

AttributeDefaultType
asChild
boolean

Use 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
CheckedState

The controlled checked state of the checkbox

defaultChecked
CheckedState

The initial checked state of the checkbox when rendered. Use when you don't need to control the checked state of the checkbox.

disabled
boolean

Whether the checkbox is disabled

form
string

The id of the form that the checkbox belongs to.

id
string

The unique identifier of the machine.

ids
ElementIds

The ids of the elements in the checkbox. Useful for composition.

invalid
boolean

Whether the checkbox is invalid

name
string

The name of the input field in a checkbox. Useful for form submission.

onCheckedChange
(details: CheckedChangeDetails) => void

The callback invoked when the checked state changes.

readOnly
boolean

Whether 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
boolean

Whether the checkbox is required

value'on'
string

The value of checkbox input. Useful for form submission.

AttributeDescription
[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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[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.

AttributeDefaultType
asChild
boolean

Use 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
boolean

If `true`, the checkbox group is disabled

invalid
boolean

If `true`, the checkbox group is invalid

maxSelectedValues
number

The maximum number of selected values

name
string

The name of the input fields in the checkbox group (Useful for form submission).

onValueChange
(value: string[]) => void

The callback to call when the value changes

readOnly
boolean

If `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.

AttributeDefaultType
value
UseCheckboxGroupContext

asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[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.

AttributeDefaultType
asChild
boolean

Use 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.

AttributeDescription
[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.

AttributeDefaultType
value
UseCheckboxReturn

asChild
boolean

Use 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

PropertyType
checked
boolean

Whether the checkbox is checked

disabled
boolean | undefined

Whether the checkbox is disabled

indeterminate
boolean

Whether the checkbox is indeterminate

focused
boolean | undefined

Whether the checkbox is focused

checkedState
CheckedState

The checked state of the checkbox

setChecked
(checked: CheckedState) => void

Function to set the checked state of the checkbox

toggleChecked
VoidFunction

Function to toggle the checked state of the checkbox

getRootState
() => RootState

Returns the state of the checkbox

Accessibility

Complies with the Checkbox WAI-ARIA design pattern.

Keyboard Support

KeyDescription
Space
Toggle the checkbox