Menu

Components

Checkbox Group

Several related checkboxes that share one label, one value and one error. Built on react-aria-components.

Introduction

Checkbox Group wraps a set of CheckboxGroupItem elements (each one a Checkbox) that belong together, such as “notify me about” options. The group keeps one value: an array with the value of every checked box.

  • One label, description and error message for the whole set, and a screen reader announces the label with each checkbox.
  • Disabled, read-only, invalid and size are set once, on the group, and every checkbox follows. A checkbox can still override them.
  • Stack the boxes or lay them out in a row, with orientation.
  • Works in a form: give the group a name and each checked box is submitted under it.

For a single on-or-off box, use Checkbox on its own.

Installation

centerUI has no CLI or registry yet: components are copied into your project. Checkbox Group is made of Checkbox items, so add Checkbox first.

# bun
bun add react-aria-components tailwind-variants tailwind-merge clsx

# pnpm
pnpm add react-aria-components tailwind-variants tailwind-merge clsx

# npm
npm install react-aria-components tailwind-variants tailwind-merge clsx

Add Checkbox first (see its Manual installation), then follow the steps below.

Manual installation

Copy these files into your project and keep the paths, or adjust the imports.

1. Add the styles

src/types/checkbox-group-styles.tsShow code
import { tv } from "tailwind-variants";

// Layout of a CheckboxGroup: its label, the list of checkboxes, and the
// description or error under them.
export const checkboxGroupStyles = tv({
  slots: {
    root: "group/checkbox-group flex flex-col gap-2",
    items: "flex gap-x-6 gap-y-3",
  },
  variants: {
    orientation: {
      vertical: { items: "flex-col" },
      horizontal: { items: "flex-row flex-wrap" },
    },
  },
  defaultVariants: { orientation: "vertical" },
});

2. Add the context

It carries the group's size down to the checkboxes. Checkbox already reads it, so the file must exist in your project.

src/types/checkbox-group-context.tsShow code
import { createContext, useContext } from "react";
import type { VariantProps } from "tailwind-variants";
import type { checkboxStyles } from "./checkbox-styles";

/** Defaults a CheckboxGroup passes down to the checkboxes inside it. */
export type CheckboxGroupContextValue = Pick<
  VariantProps<typeof checkboxStyles>,
  "size"
>;

const CheckboxGroupContext = createContext<CheckboxGroupContextValue | null>(
  null,
);

export const CheckboxGroupProvider = CheckboxGroupContext.Provider;

/** The enclosing CheckboxGroup's defaults, or null outside of one. */
export function useCheckboxGroup() {
  return useContext(CheckboxGroupContext);
}

3. Add the Checkbox Group file

src/components/ui/checkbox-group/checkbox-group.tsxShow code
"use client";

import { type ReactNode, useMemo } from "react";
import {
  CheckboxGroup as AriaCheckboxGroup,
  type CheckboxGroupProps as AriaCheckboxGroupProps,
  FieldError,
  Label,
  Text,
  type ValidationResult,
} from "react-aria-components";
import {
  Checkbox,
  type CheckboxProps,
} from "@/components/ui/checkbox/checkbox";
import { cn } from "@/lib/cn";
import { CheckboxGroupProvider } from "@/types/checkbox-group-context";
import { checkboxGroupStyles } from "@/types/checkbox-group-styles";

export interface CheckboxGroupProps
  extends Omit<AriaCheckboxGroupProps, "children" | "className"> {
  label?: ReactNode;
  /** Help text under the group. */
  description?: ReactNode;
  /** Shown when the group is invalid. A function receives the validation result. */
  errorMessage?: string | ((validation: ValidationResult) => string);
  /** Stack the checkboxes, or lay them out in a row. */
  orientation?: "vertical" | "horizontal";
  /** Size of every checkbox. A checkbox can override it. */
  size?: "sm" | "md" | "lg";
  className?: string;
  children: ReactNode;
}

/**
 * A set of related checkboxes that share one value: the array of the `value`
 * of every checked box. Built on react-aria's CheckboxGroup, so the selection,
 * keyboard support and form submission (`name`) come from there. Disabling,
 * the invalid state, the size and the name are set once, on the group.
 *
 * Give the group a `label`, or an `aria-label` when the label is written
 * elsewhere.
 *
 * @example
 * <CheckboxGroup label="Notify me about" defaultValue={["news"]}>
 *   <CheckboxGroupItem value="news">News</CheckboxGroupItem>
 *   <CheckboxGroupItem value="offers">Offers</CheckboxGroupItem>
 * </CheckboxGroup>
 */
export function CheckboxGroup({
  label,
  description,
  errorMessage,
  orientation = "vertical",
  size,
  className,
  children,
  ...props
}: CheckboxGroupProps) {
  const styles = checkboxGroupStyles({ orientation });
  const defaults = useMemo(() => ({ size }), [size]);

  return (
    <CheckboxGroupProvider value={defaults}>
      <AriaCheckboxGroup {...props} className={cn(styles.root(), className)}>
        {label ? (
          <Label className="text-sm font-medium group-data-required/checkbox-group:after:ml-0.5 group-data-required/checkbox-group:after:text-danger group-data-required/checkbox-group:after:content-['*']">
            {label}
          </Label>
        ) : null}
        <div className={styles.items()}>{children}</div>
        {description ? (
          <Text slot="description" className="text-sm text-muted-fg">
            {description}
          </Text>
        ) : null}
        <FieldError className="text-sm text-danger">{errorMessage}</FieldError>
      </AriaCheckboxGroup>
    </CheckboxGroupProvider>
  );
}

export interface CheckboxGroupItemProps extends CheckboxProps {
  /** The entry this checkbox adds to the group's `value` when it is checked. */
  value: string;
}

/**
 * One item of a CheckboxGroup: a Checkbox whose `value` is required, because
 * it is what appears in the group's `value`. It takes every Checkbox prop
 * (description, size, isDisabled...). A plain `Checkbox` with a `value` works
 * in a group too.
 */
export function CheckboxGroupItem(props: CheckboxGroupItemProps) {
  return <Checkbox {...props} />;
}

4. Use it

import {
  CheckboxGroup,
  CheckboxGroupItem,
} from "@/components/ui/checkbox-group/checkbox-group";

Anatomy

A group is a label, a list of checkboxes, and a description or error.

<CheckboxGroup label="Notify me about" description="You can change this later.">
  <CheckboxGroupItem value="news" description="Product news, once a month">News</CheckboxGroupItem>
  <CheckboxGroupItem value="offers">Offers</CheckboxGroupItem>
</CheckboxGroup>
PartRendered asNotes
LabellabelNames the whole set. Marked with * when required.
CheckboxesCheckboxEach one needs a value. They read their state from the group.
Descriptiontext under the groupHelp text for the whole set.
Errortext under the groupReplaces the description while the group is invalid. Every box turns red.

Without a label, give the group an aria-label so a screen reader can name it.

Examples

Live previews with code you can copy.

Usage

Notify me about

Label

Every group needs a name. Use label, or aria-label when the label is written elsewhere.

Notify me about
Notify me about

Description

Help text under the group.

Notify me about
You can change this later in your settings.

Item descriptions

A smaller line under each checkbox.

Notify me about

Horizontal

Lay the checkboxes out in a row. They wrap when there is no room.

Days

Uncontrolled

Let the group keep its own state. Start with defaultValue.

Toppings

Controlled

Keep the value in your own state with value and onChange, so you can read it, set it or clear it from outside.

Toppings

value: [cheese]

Select all

A parent checkbox that shows a dash when only some children are checked. Build it from a controlled group.

In a form

With a name on the group, each checked box is submitted as its own entry with that name.

Toppings

Not submitted yet.

Sizes

Set size on the group and every checkbox follows.

Disabled

Disable the whole group, or a single checkbox.

Disabled group
One disabled option

Validation

Set isInvalid with an errorMessage. Every box gets a red border and the message replaces the description.

Contact me by

Component API

Checkbox Group and CheckboxGroupItem take the props below. CheckboxGroup also accepts everything the react-aria CheckboxGroup does, and CheckboxGroupItem everything Checkbox does.

CheckboxGroup

PropTypeDefault
labelReactNode-
descriptionReactNode-
errorMessagestring | (validation) => string-
valuestring[]-
defaultValuestring[][]
onChange(value: string[]) => void-
orientation"vertical" | "horizontal""vertical"
size"sm" | "md" | "lg""md"
namestring-
isDisabledbooleanfalse
isReadOnlybooleanfalse
isInvalidbooleanfalse
isRequiredbooleanfalse
aria-labelstring- (when there is no label)

A checkbox's own size wins over the group's.

CheckboxGroupItem

PropTypeDefault
valuestring- (required)
childrenReactNode-
descriptionReactNode-
size"sm" | "md" | "lg"the group's size
isDisabledbooleanthe group's

It is a Checkbox with a required value. A plain Checkbox with a value works inside a group too.