Skip to content

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

Components

Free

Checkbox for Radix UI

A checkbox for React on Radix 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 Radix 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 <button> with the checkbox role and aria-checked, which reads mixed while checked is "indeterminate". Radix UI stops Enter from toggling it, as the ARIA pattern asks. 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
Toggle the checkbox.

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. Its argument is typed boolean | "indeterminate", so compare it with true before you store it, as the Table demo does. Use defaultChecked instead when you only need a starting value.

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?

Set the header box's checked to "indeterminate". It draws a dash instead of the check mark, Radix UI reports it as mixed, and a click on it asks for true.

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

Radix UI renders the hidden input only when the box sits inside a <form> element, so keep it in one. 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 Radix 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

<label> data-slot="checkbox"

PropType
checked?boolean | "indeterminate"

Whether it's checked. Pair with onCheckedChange to control it.

Type
boolean | "indeterminate"
defaultChecked?boolean | "indeterminate"

Whether it starts checked, when you don't control checked.

Type
boolean | "indeterminate"
onCheckedChange?{ bivarianceHack(checked: CheckedState): void; }["bivarianceHack"]

Called when the checked state changes.

Type
{ bivarianceHack(checked: CheckedState): void; }["bivarianceHack"]
required?boolean

Whether a value is required to submit its form.

asChild?boolean

Merge props onto the child element instead of rendering a host.

className?string

Merged with the part's classes.

CheckboxIndicator

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

PropType
forceMount?true

Used to force mounting when more control is needed. Useful when controlling animation with React animation libraries.