Skip to content

shadcn.io is an independent project built on shadcn/ui

Components

Free

Checkbox for React

A checkbox for React on Base UI: one yes or no per box, for terms, filters and table rows, with a mixed state for a partly selected group.

On this page9 sections

A checkbox is a small box beside a label that someone ticks to say yes to one thing: accepting the terms, picking a filter, selecting a row in a table. Boxes in a list are independent of each other, and each ticked one goes in with the form when it's submitted.

When the change should take effect the moment it's made, such as turning on notifications, use a Switch. When only one option of a few can be picked, use a Radio Group.

Preview width

Installation

Sign in to get your install command
Free with an account. It carries your token, so the CLI installs everything in one go.

Anatomy

Checkbox anatomy: root, control, indicator, label

Usage

import { Checkbox } from "@/components/ui/checkbox";
import { Field, FieldGroup, FieldLabel } from "@/components/ui/field";
<FieldGroup className="w-fit whitespace-nowrap">
  <Field orientation="horizontal">
    <Checkbox name="terms" />
    <FieldLabel>Accept terms</FieldLabel>
  </Field>
</FieldGroup>

With Base UI, the Field only lays the two side by side. Give Checkbox an id and set htmlFor on FieldLabel to the same value, or clicking the words does nothing and the box has no name.

Accessibility

The box is a <span> with the checkbox role and aria-checked, which reads mixed while indeterminate is set, and a hidden native input beside it carries the value for the form. Name it with a FieldLabel whose htmlFor matches its id, or with aria-label where no visible label fits, such as a table row.

Space
Toggles the checkbox.
Enter
Leaves the box as it is and submits the form it belongs to, through the form's default submit button.

Demos

Invalid

An Accept terms box in its error state, the way it should look after someone submits without ticking a box they had to tick. The demo sets aria-invalid on Checkbox and data-invalid on Field, which turns the border and the label red. Show it only after a submit, with a FieldError that says what's missing.

Preview width

Basic

An Accept terms box with a label and a name, the smallest checkbox worth shipping. In a form, a ticked box sends its name with the value on, and an unticked one sends nothing. Start here for a single agreement, and add a description only when the label can't carry the consequence on its own.

Preview width

Description

Accept terms starts ticked, with a second, quieter line under the label. FieldContent stacks the label over that description, and the field then aligns the box with the first line of text instead of centering it on the block. Use it when the label alone would hide a consequence, such as a charge or data being shared, and keep the second line to that consequence.

Preview width

Disabled

A Notifications box that can't be changed. Checkbox gets disabled and Field gets data-disabled, so the label fades along with the box and clicks do nothing. Use it for a preference the current plan or role doesn't allow, and say why next to it. Leave the row out when the person will never be able to change it.

Preview width

Group

Three kinds of item to show on the desktop, under a legend that reads Desktop: hard disks and external disks start ticked, servers doesn't. Each box has its own name, so the form sends one entry per ticked box. Put boxes that answer one question in a FieldSet with a FieldLegend, which renders a real fieldset and legend, so a screen reader names the group as focus enters it.

Preview width

Table

A table of three people with a box in each row and one in the header, with Sarah Chen selected to start. The header box is ticked when every row is and mixed while only some are, and a click on it selects every row or clears them all. The selected ids live in a Set in state, which the boxes and the row highlight both read.

Preview width

RTL

The Accept terms box in Arabic, with dir set to rtl on the FieldGroup around it. The box moves to the right edge and the label follows it to the left, with no change to the markup. Set the direction on the nearest wrapper like this when the block sits inside a page that otherwise reads left to right.

Preview width

FAQ

When should I use a checkbox instead of a switch or radio buttons?

Use a checkbox when the choice takes effect later, usually when a form is submitted, and when several options in a list can be picked at once: accepting terms, choosing filters, selecting rows for a bulk action.

A Switch reads as an on and off control that acts immediately, like a setting saved as soon as it changes. A Radio Group is for exactly one choice out of a few, all of them visible. A single box that turns a setting on or off right away is a switch in disguise.

How do I control a Checkbox's checked state in React?

Pass checked and update it in onCheckedChange, whose first argument is the new boolean. Use defaultChecked instead when you only need a starting value and the box can keep its own state.

Hold the state yourself when something else reads it, such as a submit button that stays disabled until the terms box is ticked.

How do I make a select-all Checkbox with an indeterminate state?

Pass indeterminate to the header box. It draws a dash instead of the check mark, and Base UI reports it as mixed until you clear the prop.

Work the state out from the rows on every render. Keep the selected ids in a Set: the header is checked when the set holds every row, mixed when it holds some, and on change it fills the set or empties it, as the Table demo does.

How do I add a label to a Checkbox in Base UI?

Give Checkbox an id and set htmlFor on FieldLabel to the same value, as the demos do. The Field around them only handles the layout, so without the pair, clicking the words does nothing and the box has no name. Where no visible label fits, as in a table row, set aria-label on the box, such as Select Sarah Chen.

A visible label also makes the target bigger. The box has an invisible hit area a little wider than itself, but on a phone the words are what people aim for.

How do I show a Checkbox error in a form?

Set invalid on the Field around the box. The field marks the checkbox with aria-invalid, which a screen reader announces and the styles draw as a red border, and the field's label turns red with it.

Put the message in a FieldError inside the same field, so it sits under the label, and show it after a submit rather than while someone is still filling the form in.

What value does a Checkbox send when the form is submitted?

A ticked box sends its name with the value on, or with its value when you set one, and an unticked box sends nothing, the way a native checkbox does. On the server, read a missing key as false.

Set uncheckedValue when the server needs an entry for an unticked box too. With a form library such as React Hook Form, connect the library's field value to checked and its change handler to onCheckedChange.

How do I get the checked values of a group of checkboxes?

Keep an array of values in state. Each box is checked when the array includes its value, and its change callback adds or removes that value. components/ui/checkbox exports no group part on Base UI, so the array is the group.

On submit, a FormData built from the form returns every ticked box that shares one name through getAll.

How do I change the Checkbox size or colors?

Edit components/ui/checkbox, which is your file once installed. The box's size, border and checked color are utility classes on the root, and the check icon's size is a class on the indicator, so change those to restyle every checkbox at once.

To change one box, add a class to Checkbox. It is merged over the defaults, so a size or a background color you add there wins over the one in the file.

API Reference

Checkbox

<span> data-slot="checkbox"

PropType
checked?boolean

Whether the checkbox is currently ticked. To render an uncontrolled checkbox, use the defaultChecked prop instead.

Default
undefined
defaultChecked?boolean

Whether the checkbox is initially ticked. To render a controlled checkbox, use the checked prop instead.

Default
false
onCheckedChange?(checked: boolean, eventDetails: CheckboxRootChangeEventDetails) → void

Event handler called when the checkbox is ticked or unticked.

Type
(checked: boolean, eventDetails: CheckboxRootChangeEventDetails) → void
Parameters

checked – boolean

eventDetails – CheckboxRootChangeEventDetails

Returns
void
disabled?boolean

Whether the component should ignore user interaction.

Default
false
form?string

Identifies the form that owns the hidden input. Useful when the checkbox is rendered outside the form.

id?string

The id of the input element.

indeterminate?boolean

Whether the checkbox is in a mixed state: neither ticked, nor unticked.

Default
false
inputRef?React.Ref<HTMLInputElement>

A ref to access the hidden <input> element.

Type
React.Ref<HTMLInputElement>
name?string

Identifies the field when a form is submitted.

Default
undefined
nativeButton?boolean

Whether the component renders a native <button> element when replacing it via the render prop. Set to true if the rendered element is a native button.

Default
false
parent?boolean

Whether the checkbox controls a group of child checkboxes. Must be used in a [Checkbox Group](https://base-ui.com/react/components/checkbox-group).

Default
false
readOnly?boolean

Whether the user should be unable to tick or untick the checkbox.

Default
false
required?boolean

Whether the user must tick the checkbox before submitting a form.

Default
false
uncheckedValue?string

The value submitted with the form when the checkbox is unchecked. By default, unchecked checkboxes do not submit any value, matching native checkbox behavior.

value?string

The checkbox's value. Identifies it within a [Checkbox Group](https://base-ui.com/react/components/checkbox-group), falling back to name when omitted. When submitting a form, a checked box submits value; with no value, it submits the native "on".

Default
"on"
render?ReactElement | ((props: object, state: object) => ReactElement)

Replace the host element or compose it with another component.

Type
ReactElement | ((props: object, state: object) => ReactElement)
className?string

Merged with the part's classes.

CheckboxIndicator

<span> data-slot="checkbox-indicator"

PropType
keepMounted?boolean

Whether to keep the element in the DOM when the checkbox is not checked.

Default
false