Checkbox
An easily stylable checkbox component.
View as MarkdownUsage guidelines
- Form controls must have an accessible name: The checkbox must have a meaningful label. Prefer using
<Field>to provide a visible text label and description, or use thearia-labelattribute as an alternative. See the forms guide for more on building form controls.
Anatomy
Import the component and assemble its parts:
import { Checkbox } from '@base-ui-components/react/checkbox';
<Checkbox.Root>
<Checkbox.Indicator />
</Checkbox.Root>Examples
Form integration
To use a single checkbox in a form, pass the checkbox’s name to <Field> and use <Field.Label> to label the control.
<Form>
<Field.Root name="stayLoggedIn">
<Field.Label>
<Checkbox.Root />
Stay logged in for 7 days
</Field.Label>
</Field.Root>
</Form>API reference
Root
Represents the checkbox itself.
Renders a <span> element and a hidden <input> beside.
namestringundefined
- Name
- Description
Identifies the field when a form is submitted.
- Type
string | undefined- Default
undefined
defaultCheckedbooleanfalse
- Name
- Description
Whether the checkbox is initially ticked.
To render a controlled checkbox, use the
checkedprop instead.- Type
boolean | undefined- Default
false
checkedbooleanundefined
- Name
- Description
Whether the checkbox is currently ticked.
To render an uncontrolled checkbox, use the
defaultCheckedprop instead.- Type
boolean | undefined- Default
undefined
onCheckedChangefunction—
- Name
- Description
Event handler called when the checkbox is ticked or unticked.
- Type
| (( checked: boolean, eventDetails: Checkbox.Root.ChangeEventDetails, ) => void) | undefined
indeterminatebooleanfalse
- Name
- Description
Whether the checkbox is in a mixed state: neither ticked, nor unticked.
- Type
boolean | undefined- Default
false
valuestring—
- Name
- Description
The value of the selected checkbox.
- Type
string | undefined
nativeButtonbooleanfalse
- Name
- Description
Whether the component renders a native
<button>element when replacing it via therenderprop. Set totrueif the rendered element is a native button.- Type
boolean | undefined- Default
false
parentbooleanfalse
- Name
- Description
Whether the checkbox controls a group of child checkboxes.
Must be used in a Checkbox Group.
- Type
boolean | undefined- Default
false
disabledbooleanfalse
- Name
- Description
Whether the component should ignore user interaction.
- Type
boolean | undefined- Default
false
readOnlybooleanfalse
- Name
- Description
Whether the user should be unable to tick or untick the checkbox.
- Type
boolean | undefined- Default
false
requiredbooleanfalse
- Name
- Description
Whether the user must tick the checkbox before submitting a form.
- Type
boolean | undefined- Default
false
inputRefRef<HTMLInputElement>—
- Name
- Description
A ref to access the hidden
<input>element.- Type
React.Ref<HTMLInputElement> | undefined
idstring—
- Name
- Description
The id of the input element.
- Type
string | undefined
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Checkbox.Root.State) => string | undefined)
styleReact.CSSProperties | function—
- Name
- Type
| React.CSSProperties | (( state: Checkbox.Root.State, ) => CSSProperties | undefined) | undefined
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Checkbox.Root.State, ) => ReactElement)
data-checked
data-unchecked
data-disabled
data-readonly
data-required
data-valid
data-invalid
data-dirty
data-touched
data-filled
data-focused
Indicator
Indicates whether the checkbox is ticked.
Renders a <span> element.
classNamestring | function—
- Name
- Description
CSS class applied to the element, or a function that returns a class based on the component’s state.
- Type
| string | ((state: Checkbox.Indicator.State) => string | undefined)
styleReact.CSSProperties | function—
- Name
- Type
| React.CSSProperties | (( state: Checkbox.Indicator.State, ) => CSSProperties | undefined) | undefined
keepMountedbooleanfalse
- Name
- Description
Whether to keep the element in the DOM when the checkbox is not checked.
- Type
boolean | undefined- Default
false
renderReactElement | function—
- Name
- Description
Allows you to replace the component’s HTML element with a different tag, or compose it with another component.
Accepts a
ReactElementor a function that returns the element to render.- Type
| ReactElement | (( props: HTMLProps, state: Checkbox.Indicator.State, ) => ReactElement)