oakoss/ui
Components

Checkbox Group

A set of options where any number can be chosen.

SourceReact Aria
Notify me bySelect all that apply.
Standard rates apply.

Installation

npx shadcn@latest add @oakoss/checkbox-group

Usage

import {
  CheckboxGroup,
  CheckboxGroupItem,
} from '#/components/ui/inputs/checkbox-group';

<CheckboxGroup description="Select all that apply." label="Notify me by">
  <CheckboxGroupItem value="email">Email</CheckboxGroupItem>
  <CheckboxGroupItem value="sms">Text message</CheckboxGroupItem>
</CheckboxGroup>;

The group's description and error sit between its label and the options, so "Select all that apply" and what went wrong are read before the list. Keep the description short: screen readers repeat it on every option.

Don't also wrap the group in a Field FieldSet with a legend: the group is already named by its label, and a legend would name it twice. Past about 15 options, a list is easier to scan as a Select with selectionMode="multiple". An option that excludes the rest, such as "None", goes last.

Examples

Horizontal

orientation="horizontal" lays the options out in a row, which wraps on a narrow screen. Keep it for two or three short options; a vertical list is easier to scan.

Days

Cards

variant="card" makes each option a card, with its description under the title; anywhere on a card toggles it.

Add-ons
Daily copies, kept for 30 days.
Answers within four hours.

Select all

A select-all checkbox is a Checkbox outside the group that follows its value: isIndeterminate while only some options are checked, checking all or none when pressed.

Notify me by

Validation

isRequired goes on the group, not each option: React Aria then requires an option only while none is checked. Its error defaults to "Select at least one option." (requiredLabel), where the browser's own message speaks of a single box. validate checks other rules, such as a minimum count.

InterestsSelect at least two.

Accessibility

React Aria renders a group named by label, with a native checkbox for each option inside a <label>, so an option's label is its name and its click target.

  • Keys: Tab moves through the options and Space toggles the focused one.
  • Description and errors: the group's description and error describe the group and each of its options; an option's own description describes that option alone. The error shows only while the group is invalid, and a form's submit focuses the first option when it is.
  • Required: the asterisk after a required group's label is hidden from screen readers, which hear each option's required state until one is checked.
  • Cards: the option's label stretches over its card, so the name stays the title, and the card shows focus. A link or button in a card's description sits above the label, so it stays clickable.
  • Target size: each box's hit area grows to 44px, and options sit far enough apart that a box's area never covers a neighbor. Set targetSize={false} for tighter options.

API

CheckboxGroup

Renders React Aria's CheckboxGroup. Children are its options.

Props

Prop

Type

Also accepts React Aria CheckboxGroup props.

Data attributes

AttributePresent when
data-slotAlways checkbox-group
data-variantAlways; default or card
data-orientationAlways; vertical or horizontal
data-invalidIt's invalid
data-disabledIt's disabled
data-readonlyIt's read-only
data-requiredIt's required

CheckboxGroupItem

Renders React Aria's CheckboxField, with its label in a CheckboxButton. Children are the label.

Props

Prop

Type

Also accepts React Aria CheckboxField props, including value and isIndeterminate. It takes no errorMessage or errors: the group shows errors for all its options.

Data attributes

AttributePresent when
data-slotAlways: checkbox-group-item-field on the root, checkbox-group-item on the label, checkbox-indicator on the box
data-selectedIt's checked
data-disabledIt's disabled
data-invalidIts group is invalid