Skip to content

Checkbox

Lets people turn one option on or off, or pick any number of options from a list.

Brevia pays within 60 days of the invoice date.

Installation

pnpm dlx shadcn@latest add @silkware/checkbox

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn lucide-react

Then copy the source into components/ui/checkbox.tsx:

components/ui/checkbox.tsx
"use client"
import { cn } from "cn"import {  Checkbox as CheckboxPrimitive,  composeRenderProps,  type CheckboxProps,} from "@animate-ui/react/primitives/react-aria"import { CheckIcon, MinusIcon } from "lucide-react"
function Checkbox({ className, children, ...props }: CheckboxProps) {  return (    <CheckboxPrimitive      data-slot="checkbox"      className={cn(        "peer relative flex size-4 shrink-0 items-center justify-center rounded-[4px] border border-input shadow-xs transition-shadow outline-none group-has-disabled/field:opacity-50 group-has-[:focus-visible]/field-label:ring-0 group-has-[:focus-visible]/field-label:not-data-checked:border-input after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 aria-invalid:aria-checked:border-primary data-focus-visible:border-ring data-focus-visible:ring-3 data-focus-visible:ring-ring/50 data-invalid:border-destructive data-invalid:ring-3 data-invalid:ring-destructive/20 data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 dark:data-invalid:border-destructive/50 dark:data-invalid:ring-destructive/40 data-checked:border-primary data-checked:bg-primary data-checked:text-primary-foreground group-has-[:focus-visible]/field-label:data-checked:border-primary dark:data-checked:bg-primary data-selected:border-primary data-selected:bg-primary data-selected:text-primary-foreground data-invalid:data-selected:border-primary dark:data-selected:bg-primary",        className      )}      {...props}    >      {composeRenderProps(        children,        (children, { isSelected, isIndeterminate }) => (          <>            <span              data-slot="checkbox-indicator"              className="grid place-content-center text-current transition-none [&>svg]:size-3.5"            >              {isIndeterminate ? <MinusIcon /> : isSelected && <CheckIcon />}            </span>            {children}          </>        )      )}    </CheckboxPrimitive>  )}
export { Checkbox }

Usage

import { Checkbox } from "@/components/ui/checkbox"import { Field, FieldLabel } from "@/components/ui/field"
<Field orientation="horizontal">  <Checkbox id="audit" name="audit" />  <FieldLabel htmlFor="audit">Include in the next quality audit</FieldLabel></Field>

The checkbox is a 16 pixel box. It does not render its label, so place a FieldLabel beside it and match htmlFor to the id. Use isSelected and onChange for state, defaultSelected for an initial value you do not control. The names differ from the native checked and defaultChecked.

Examples

Default

An unchecked box with a label. The label is part of the click target.

Checked

defaultSelected starts the box checked and leaves it uncontrolled.

With a description

Wrap the label and the description in FieldContent when the option needs a sentence of explanation.

Brevia pays within 60 days of the invoice date.

Controlled

Hold the state yourself with isSelected and onChange. onChange receives a boolean.

Normal

Indeterminate

isIndeterminate shows a dash for a parent box whose children are partly selected, and assistive technology reports the box as mixed. You compute the state, and you decide what a press does. Here, pressing it selects all the sites.

Disabled

isDisabled removes the box from the tab order and ignores presses. Put data-disabled on the Field so the label dims too.

Invalid

isInvalid draws the destructive border. Put data-invalid on the Field, show the message in a FieldError and link it with aria-describedby.

Group

Several checkboxes that answer one question go in a FieldSet with a FieldLegend. Each box has its own Field, id and label.

Notify me when an order is

Choose every event you want to hear about.

Choice card

Wrap a Field in a FieldLabel to make the whole card the target. The card takes a tint when its box is checked.

Accessibility

KeyAction
SpaceChecks or unchecks the focused box.
TabMoves focus to the next control. A disabled box is skipped.
ShiftTabMoves focus to the previous control.

React Aria provides:

  • A native checkbox input, hidden visually, with the checkbox role and aria-checked or the mixed state.
  • Press behaviour that is the same for mouse, touch and keyboard.
  • A focus ring for keyboard focus only.
  • isDisabled, isReadOnly, isRequired and isInvalid states, and the data attributes the styles read.

You provide:

  • A visible label that matches the id of the checkbox, or an aria-label.
  • A FieldSet and FieldLegend that name a group of boxes.
  • aria-describedby for a description or an error.
  • Text that makes the checked state clear without colour: the label says what turns on.

Guidelines

When to use

  • For a choice that is on or off and applies when the form is saved.
  • For a list where people can pick none, one or several options.
  • For a confirmation that must be given before a form can be sent, such as accepting terms.

When not to use

  • For a setting that takes effect at once. Use Switch.
  • For one choice from several. Use Radio Group.
  • For an option that starts an action. Use Button or Toggle.
  • For a long list that needs search. Use Combobox.

Content

  • Write the label as the thing that will be true when checked: "Include in the next quality audit".
  • Keep labels positive. "Notify me" is clearer than "Do not stop notifications".
  • Start each label of a group in the same way, in sentence case, and order them by importance or alphabetically.
  • Give a group a legend that says what the boxes decide: "Notify me when an order is".

Choice type

Sites that receive this order
Do

Use checkboxes when people can pick any number of options.

Priority
Don’t

Don’t use checkboxes for options that exclude each other. Use radio buttons.

API reference

Checkbox

Renders the React Aria Checkbox with a check or a dash inside the box. It accepts every React Aria Checkbox prop. The box does not render a label; the children render after the indicator inside the box.

PropTypeDefaultDescription
isSelected?booleanNoneWhether the box is checked, for a controlled checkbox.
defaultSelected?booleanNoneWhether the box starts checked, for an uncontrolled checkbox.
onChange?(isSelected: boolean) => voidNoneCalled when the box is checked or unchecked.
isIndeterminate?booleanNoneShows the mixed state. It does not change when the box is pressed.
isDisabled?booleanfalseRemoves the box from the tab order and ignores presses.
isReadOnly?booleanfalseKeeps the box focusable but stops it from changing.
isRequired?booleanfalseAsks for the box to be checked before the form is submitted.
isInvalid?booleanNoneMarks the box as invalid and draws the destructive border.
name?stringNoneThe name used when the form is submitted.
value?stringNoneThe value submitted when the box is checked.
className?stringNoneClasses merged after the base classes.