Checkbox
A control that lets the user toggle between checked and not checked.
Installation
Install the following dependencies:
Set up the token layer and the StyleX compiler, if you have not already. See
Installation, and paste states.ts and
state-styles.ts from the same page. The checkbox imports both. It also needs
interop.css, which sizes the tick. The
examples caption it with a Label.
Paste field-state.ts from the same installation page.
Copy and paste the following code into your project.
Update the import paths to match your project setup.
Usage
Lay a checkbox and its label out in an HStack, or in a horizontal
Field for automatic label association.
Checked state
Use defaultChecked for an uncontrolled checkbox, or checked and
onCheckedChange to control it.
Invalid state
Set invalid on Field or aria-invalid on a bare checkbox. Both show the destructive border and ring.
Basic
Pair the checkbox with a Label in an HStack.
Description
Stack the label and a line of helper text in a VStack beside the checkbox,
and align the row to start so the box sits on the first line.
Disabled
Set disabled on Field to disable its control and dim its label, or on a bare control directly.
Group
Use Checkbox Group to share selection state and Field validation.
Select the items you want to show on the desktop.
Indeterminate
Use parent inside a controlled Checkbox Group with allValues to derive the checked and indeterminate states automatically.
Use the indeterminate prop for a parent that controls a set of children. It
draws a dash in place of the tick and is independent of checked.
indeterminate is a display state. Clicking the checkbox still fires
onCheckedChange with the opposite of checked, so decide what a click on a
mixed parent means and set checked from that.
API reference
Pass render to replace a part's element or compose it with another component.
Checkbox wraps the Base UI Checkbox.Root and renders its Indicator
inside. See the Base UI reference
for the full list of props.
Checkbox
| Prop | Type | Default |
|---|---|---|
checked | boolean | — |
defaultChecked | boolean | false |
onCheckedChange | (checked: boolean, details) => void | — |
indeterminate | boolean | false |
disabled | boolean | false |
readOnly | boolean | false |
required | boolean | false |
name | string | — |
value | string | — |
id | string | — |
inputRef | Ref<HTMLInputElement> | — |
xstyle | StyleXStyles | — |
style | CSSProperties | — |
className | string | — |
The root is a <span role="checkbox">. Base UI renders a hidden <input>
beside it that carries id, name and value. A label's htmlFor and a
form submission reach the control through that input.
checkboxStyles
The style maps behind the component. Spread them onto any element.
| Key | What it is |
|---|---|
root | The box: size, border width, corner, hit area |
choice | The hairline edge and fill at a choice's size |
checked | The flat primary fill and inverted tick colour |
disabled | The dimmed, non-interactive state |
invalid | The destructive border and ring |
root carries no colour and no focus ring. Everything else comes from states
in theme/states.ts. Merge choice, then states.focusRing. Merge invalid
before checked, so a ticked invalid box keeps the primary fill and shows only
the destructive ring.
Data attributes
| Attribute | On | Effect |
|---|---|---|
data-slot="checkbox" | the root | Targets the box from a parent |
data-slot="checkbox-indicator" | the indicator | Targets the animated indicator |
data-checked, data-unchecked | root, indicator | Base UI writes them |
data-parent | root | Controls the group selection |
data-indeterminate | root, indicator | Base UI writes it |
data-disabled, data-readonly, data-required | root, indicator | Base UI writes them |
data-starting-style, data-ending-style | the indicator | Base UI writes them while the tick enters or leaves |