Checkbox
An easily stylable checkbox component.
View as MarkdownUsage guidelines
- Form controls must have an accessible name: It can be created using a
<label>element or theFieldcomponent. See Labeling a checkbox and the forms guide.
Anatomy
Import the component and assemble its parts:
import { Checkbox } from '@base-ui/react/checkbox';
<Checkbox.Root>
<Checkbox.Indicator />
</Checkbox.Root>Examples
Labeling a checkbox
An enclosing <label> is the simplest labeling pattern:
<label>
<Checkbox.Root />
Accept terms and conditions
</label>Rendering as a native button
By default, <Checkbox.Root> renders a <span> element to support enclosing labels. Prefer rendering the checkbox as a native button when using sibling labels (htmlFor/id).
<div>
<label htmlFor="notifications-checkbox">Enable notifications</label>
<Checkbox.Root id="notifications-checkbox" nativeButton render={<button />}>
<Checkbox.Indicator />
</Checkbox.Root>
</div>Native buttons with wrapping labels are supported by using the render callback to avoid invalid HTML, so the hidden input is placed outside the label:
<Checkbox.Root
nativeButton
render={(buttonProps) => (
<label>
<button {...buttonProps} />
Enable notifications
</label>
)}
/>Form integration
Use Field to handle label associations and form integration:
<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
formstring—
- Name
- Description
Identifies the form that owns the hidden input. Useful when the checkbox is rendered outside the form.
- 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
uncheckedValuestring—
- Name
- Description
The value submitted with the form when the checkbox is unchecked. By default, unchecked checkboxes do not submit any value, matching native checkbox behavior.
- Type
string | undefined
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
inputRefReact.Ref<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) | undefined
styleReact.CSSProperties | function—
- Name
- Type
| React.CSSProperties | (( state: Checkbox.Root.State, ) => React.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) | undefined
data-checked
Present when the checkbox is checked.
data-unchecked
Present when the checkbox is not checked.
data-disabled
Present when the checkbox is disabled.
data-readonly
Present when the checkbox is readonly.
data-required
Present when the checkbox is required.
data-valid
Present when the checkbox is in valid state (when wrapped in Field.Root).
data-invalid
Present when the checkbox is in invalid state (when wrapped in Field.Root).
data-dirty
Present when the checkbox’s value has changed (when wrapped in Field.Root).
data-touched
Present when the checkbox has been touched (when wrapped in Field.Root).
data-filled
Present when the checkbox is checked (when wrapped in Field.Root).
data-focused
Present when the checkbox is focused (when wrapped in Field.Root).
data-indeterminate
Present when the checkbox is in an indeterminate state.
| Attribute | Description | |
|---|---|---|
data-checked | Present when the checkbox is checked. | |
data-unchecked | Present when the checkbox is not checked. | |
data-disabled | Present when the checkbox is disabled. | |
data-readonly | Present when the checkbox is readonly. | |
data-required | Present when the checkbox is required. | |
data-valid | Present when the checkbox is in valid state (when wrapped in Field.Root). | |
data-invalid | Present when the checkbox is in invalid state (when wrapped in Field.Root). | |
data-dirty | Present when the checkbox’s value has changed (when wrapped in Field.Root). | |
data-touched | Present when the checkbox has been touched (when wrapped in Field.Root). | |
data-filled | Present when the checkbox is checked (when wrapped in Field.Root). | |
data-focused | Present when the checkbox is focused (when wrapped in Field.Root). | |
data-indeterminate | Present when the checkbox is in an indeterminate state. | |
Checkbox.Root.StateHide
type CheckboxRootState = {
/** Whether the checkbox is currently ticked. */
checked: boolean;
/** Whether the component should ignore user interaction. */
disabled: boolean;
/** Whether the user should be unable to tick or untick the checkbox. */
readOnly: boolean;
/** Whether the user must tick the checkbox before submitting a form. */
required: boolean;
/** Whether the checkbox is in a mixed state: neither ticked, nor unticked. */
indeterminate: boolean;
/** Whether the field has been touched. */
touched: boolean;
/** Whether the field value has changed from its initial value. */
dirty: boolean;
/** Whether the field is valid. */
valid: boolean | null;
/** Whether the field has a value. */
filled: boolean;
/** Whether the field is focused. */
focused: boolean;
}Checkbox.Root.ChangeEventReasonHide
type CheckboxRootChangeEventReason = 'none'Checkbox.Root.ChangeEventDetailsHide
type CheckboxRootChangeEventDetails = {
/** The reason for the event. */
reason: 'none';
/** The native event associated with the custom event. */
event: Event;
/** Cancels Base UI from handling the event. */
cancel: () => void;
/** Allows the event to propagate in cases where Base UI will stop the propagation. */
allowPropagation: () => void;
/** Indicates whether the event has been canceled. */
isCanceled: boolean;
/** Indicates whether the event is allowed to propagate. */
isPropagationAllowed: boolean;
/** The element that triggered the event, if applicable. */
trigger: Element | undefined;
}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) | undefined
styleReact.CSSProperties | function—
- Name
- Type
| React.CSSProperties | (( state: Checkbox.Indicator.State, ) => React.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) | undefined
data-checked
Present when the checkbox is checked.
data-unchecked
Present when the checkbox is not checked.
data-disabled
Present when the checkbox is disabled.
data-readonly
Present when the checkbox is readonly.
data-required
Present when the checkbox is required.
data-valid
Present when the checkbox is in valid state (when wrapped in Field.Root).
data-invalid
Present when the checkbox is in invalid state (when wrapped in Field.Root).
data-dirty
Present when the checkbox’s value has changed (when wrapped in Field.Root).
data-touched
Present when the checkbox has been touched (when wrapped in Field.Root).
data-filled
Present when the checkbox is checked (when wrapped in Field.Root).
data-focused
Present when the checkbox is focused (when wrapped in Field.Root).
data-indeterminate
Present when the checkbox is in an indeterminate state.
data-starting-style
Present when the checkbox indicator is animating in.
data-ending-style
Present when the checkbox indicator is animating out.
| Attribute | Description | |
|---|---|---|
data-checked | Present when the checkbox is checked. | |
data-unchecked | Present when the checkbox is not checked. | |
data-disabled | Present when the checkbox is disabled. | |
data-readonly | Present when the checkbox is readonly. | |
data-required | Present when the checkbox is required. | |
data-valid | Present when the checkbox is in valid state (when wrapped in Field.Root). | |
data-invalid | Present when the checkbox is in invalid state (when wrapped in Field.Root). | |
data-dirty | Present when the checkbox’s value has changed (when wrapped in Field.Root). | |
data-touched | Present when the checkbox has been touched (when wrapped in Field.Root). | |
data-filled | Present when the checkbox is checked (when wrapped in Field.Root). | |
data-focused | Present when the checkbox is focused (when wrapped in Field.Root). | |
data-indeterminate | Present when the checkbox is in an indeterminate state. | |
data-starting-style | Present when the checkbox indicator is animating in. | |
data-ending-style | Present when the checkbox indicator is animating out. | |
Checkbox.Indicator.StateHide
type CheckboxIndicatorState = {
/** The transition status of the component. */
transitionStatus: TransitionStatus;
/** Whether the checkbox is currently ticked. */
checked: boolean;
/** Whether the component should ignore user interaction. */
disabled: boolean;
/** Whether the user should be unable to tick or untick the checkbox. */
readOnly: boolean;
/** Whether the user must tick the checkbox before submitting a form. */
required: boolean;
/** Whether the checkbox is in a mixed state: neither ticked, nor unticked. */
indeterminate: boolean;
/** Whether the field has been touched. */
touched: boolean;
/** Whether the field value has changed from its initial value. */
dirty: boolean;
/** Whether the field is valid. */
valid: boolean | null;
/** Whether the field has a value. */
filled: boolean;
/** Whether the field is focused. */
focused: boolean;
}