Components
Choicebox
Cards to choose from, each with a name and a line that says what it means. One card, or several.
Introduction
A choicebox turns radios or checkboxes into cards. Use it when each option needs a name, a line of explanation, and room to be read and pressed: a plan and its price, a way to ship and how long it takes, a set of add-ons. For a short list of plain options, a Radio Group or a Checkbox Group is enough.
selectionModesingle is a group of radios, one card is chosen. Multiple is a group of checkboxes, each card is on or off.- A grid of one to six columns, three sizes, and the mark of a chosen card before the content, after it, or left out.
- Each card can have an icon, a line of description, and something at the end, such as a price.
- Built on react-aria's RadioGroup and CheckboxGroup: the choice goes with a form (
name), can be required and validated, and a screen reader hears the group with its label.
Installation
centerUI has no CLI or registry yet: components are copied into your project.
# 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 clsxManual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/choicebox-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for Choicebox. The card reads its state (selected, focused, hovered,
// disabled...) from data attributes that react-aria sets on it.
export const choiceboxStyles = tv({
slots: {
root: "group/choicebox flex w-full flex-col gap-2",
label:
"text-sm font-medium group-data-required/choicebox:after:ml-0.5 group-data-required/choicebox:after:text-danger group-data-required/choicebox:after:content-['*']",
description: "m-0 text-sm text-muted-fg",
error: "m-0 text-sm text-danger",
list: "grid gap-3",
item: [
"group/item relative flex cursor-pointer items-start rounded-xl border bg-card text-card-fg outline-none transition-colors",
"data-hovered:bg-muted/50",
"data-selected:border-primary data-selected:bg-primary/5 data-selected:ring-1 data-selected:ring-primary",
"data-focus-visible:ring-2 data-focus-visible:ring-ring data-focus-visible:ring-offset-2 data-focus-visible:ring-offset-bg",
"data-invalid:border-danger data-invalid:data-selected:ring-danger",
"data-disabled:cursor-not-allowed data-disabled:opacity-50",
"data-readonly:cursor-default",
],
// The round or square mark that shows whether a card is chosen.
indicator: [
"mt-0.5 flex size-4 shrink-0 items-center justify-center border bg-bg text-primary-fg transition-colors [&_svg]:size-3",
"group-data-selected/item:border-primary group-data-selected/item:bg-primary",
],
dot: "size-1.5 rounded-full bg-primary-fg",
icon: "flex size-9 shrink-0 items-center justify-center rounded-lg bg-muted text-fg [&_svg]:size-4.5",
text: "flex min-w-0 flex-1 flex-col gap-0.5",
title: "text-sm font-medium leading-snug",
itemDescription: "text-sm leading-snug text-muted-fg",
suffix: "ml-auto shrink-0 text-sm font-medium tabular-nums",
},
variants: {
size: {
sm: { item: "gap-2.5 p-3", title: "text-sm", itemDescription: "text-xs" },
md: { item: "gap-3 p-4" },
lg: { item: "gap-4 p-5", title: "text-base" },
},
// Radio for one choice, check for several.
selectionMode: {
single: { indicator: "rounded-full" },
multiple: { indicator: "rounded-[5px]" },
},
},
defaultVariants: { size: "md", selectionMode: "single" },
});2. Add the Choicebox file
src/components/ui/choicebox/choicebox.tsxShow code
"use client";
import { createContext, type ReactNode, useContext, useId } from "react";
import {
Checkbox as AriaCheckbox,
CheckboxGroup as AriaCheckboxGroup,
type CheckboxGroupProps as AriaCheckboxGroupProps,
Radio as AriaRadio,
RadioGroup as AriaRadioGroup,
type RadioGroupProps as AriaRadioGroupProps,
FieldError,
Label,
Text,
type ValidationResult,
} from "react-aria-components";
import { CheckIcon } from "@/components/icons";
import { cn } from "@/lib/cn";
import { choiceboxStyles } from "@/types/choicebox-styles";
interface ChoiceboxContext {
selectionMode: "single" | "multiple";
size: "sm" | "md" | "lg";
indicator: "start" | "end" | "none";
}
const ChoiceboxContext = createContext<ChoiceboxContext>({
selectionMode: "single",
size: "md",
indicator: "start",
});
interface CommonProps {
/** A label above the cards. Without one, name the group with `aria-label`. */
label?: ReactNode;
/** A line that says more, under the cards. */
description?: ReactNode;
/** Shown under the cards when the choice is not valid. A function receives the validation result. */
errorMessage?: string | ((validation: ValidationResult) => string);
/** The number of cards in a row. Defaults to `1`. For a row that changes with the screen, set it to `1` and put grid classes in `className`. */
columns?: 1 | 2 | 3 | 4 | 5 | 6;
/** How much room a card has. Defaults to `md`. */
size?: "sm" | "md" | "lg";
/** Where the mark that shows a card is chosen goes: before the content, after it, or nowhere (only the card changes). Defaults to `start`. */
indicator?: "start" | "end" | "none";
/** The name of the form field, so that the choice goes with the form. */
name?: string;
"aria-label"?: string;
className?: string;
/** `ChoiceboxItem`s. */
children: ReactNode;
}
export type ChoiceboxProps =
| (CommonProps &
Omit<
AriaRadioGroupProps,
keyof CommonProps | "orientation" | "className" | "children"
> & {
/** One card can be chosen (like radios), or several (like checkboxes). Defaults to `single`. */
selectionMode?: "single";
/** The value of the chosen card, for a controlled group. */
value?: string;
/** The value of the card chosen at first, for an uncontrolled group. */
defaultValue?: string;
/** Called with the value of the card that was chosen. */
onChange?: (value: string) => void;
})
| (CommonProps &
Omit<
AriaCheckboxGroupProps,
keyof CommonProps | "className" | "children"
> & {
selectionMode: "multiple";
/** The values of the chosen cards, for a controlled group. */
value?: string[];
/** The values of the cards chosen at first, for an uncontrolled group. */
defaultValue?: string[];
/** Called with the values of the cards that are chosen. */
onChange?: (value: string[]) => void;
});
/**
* A group of cards to choose from, where each card says what the choice is and
* what it means: a plan and its price, a way to ship and how long it takes, a
* set of features. Use it in place of radios or checkboxes when each option
* needs a name, a line of explanation, and room to be read and pressed.
*
* With `selectionMode="single"` it is a radio group: one card is chosen, and the
* arrow keys move and choose. With `"multiple"` it is a checkbox group: each
* card is on or off. Built on react-aria's RadioGroup and CheckboxGroup, so the
* choice goes with a form (`name`), can be required and validated, and is read
* out by a screen reader as a group with its label.
*
* @example
* <Choicebox label="Plan" columns={2} defaultValue="pro">
* <ChoiceboxItem value="free" description="For trying it out.">Free</ChoiceboxItem>
* <ChoiceboxItem value="pro" description="For a team." suffix="$12">Pro</ChoiceboxItem>
* </Choicebox>
*/
export function Choicebox(props: ChoiceboxProps) {
const {
label,
description,
errorMessage,
columns = 1,
size = "md",
indicator = "start",
className,
children,
...rest
} = props;
const selectionMode = props.selectionMode ?? "single";
const classes = choiceboxStyles();
const body = (
<ChoiceboxContext.Provider value={{ selectionMode, size, indicator }}>
{label ? <Label className={classes.label()}>{label}</Label> : null}
<div
className={classes.list()}
style={{ gridTemplateColumns: `repeat(${columns}, minmax(0, 1fr))` }}
>
{children}
</div>
{description ? (
<Text slot="description" className={classes.description()}>
{description}
</Text>
) : null}
<FieldError className={classes.error()}>{errorMessage}</FieldError>
</ChoiceboxContext.Provider>
);
if (rest.selectionMode === "multiple") {
const { selectionMode: _mode, ...groupProps } = rest;
return (
<AriaCheckboxGroup
{...groupProps}
className={cn(classes.root(), className)}
>
{body}
</AriaCheckboxGroup>
);
}
const { selectionMode: _mode, ...groupProps } = rest;
return (
<AriaRadioGroup {...groupProps} className={cn(classes.root(), className)}>
{body}
</AriaRadioGroup>
);
}
export interface ChoiceboxItemProps {
/** What the group's value holds when this card is chosen. */
value: string;
/** The name of the card. */
children: ReactNode;
/** A line under the name that says more. */
description?: ReactNode;
/** An icon in a small box before the name. */
icon?: ReactNode;
/** Something at the end of the card: a price, a badge. */
suffix?: ReactNode;
/** Names the card for a screen reader when its name is not text. */
"aria-label"?: string;
isDisabled?: boolean;
className?: string;
}
/**
* One card of a `Choicebox`. Its `value` is what the group holds when it is
* chosen. The whole card can be pressed.
*/
export function ChoiceboxItem({
value,
children,
description,
icon,
suffix,
"aria-label": ariaLabel,
isDisabled,
className,
}: ChoiceboxItemProps) {
const { selectionMode, size, indicator } = useContext(ChoiceboxContext);
const classes = choiceboxStyles({ size, selectionMode });
const descriptionId = useId();
const mark =
indicator === "none" ? null : (
<span aria-hidden="true" className={classes.indicator()}>
{selectionMode === "multiple" ? (
<CheckIcon className="hidden group-data-selected/item:block" />
) : (
<span
className={cn(
classes.dot(),
"hidden group-data-selected/item:block",
)}
/>
)}
</span>
);
const content = (
<>
{indicator === "start" ? mark : null}
{icon ? (
<span aria-hidden="true" className={classes.icon()}>
{icon}
</span>
) : null}
<span className={classes.text()}>
<span className={classes.title()}>{children}</span>
{description ? (
<span id={descriptionId} className={classes.itemDescription()}>
{description}
</span>
) : null}
</span>
{suffix ? <span className={classes.suffix()}>{suffix}</span> : null}
{indicator === "end" ? mark : null}
</>
);
const shared = {
value,
isDisabled,
"aria-label": ariaLabel,
"aria-describedby": description ? descriptionId : undefined,
className: cn(classes.item(), className),
};
return selectionMode === "multiple" ? (
<AriaCheckbox {...shared}>{content}</AriaCheckbox>
) : (
<AriaRadio {...shared}>{content}</AriaRadio>
);
}3. Use it
import { Choicebox, ChoiceboxItem } from "@/components/ui/choicebox/choicebox";Anatomy
A group, and the cards in it.
<Choicebox
label="Plan"
description="You can change it later."
columns={2}
defaultValue="pro"
name="plan"
>
<ChoiceboxItem value="free" icon={<UserIcon />} description="For trying it out.">Free</ChoiceboxItem>
<ChoiceboxItem value="pro" description="For a team." suffix="$12">Pro</ChoiceboxItem>
</Choicebox>| Part | Rendered as | Notes |
|---|---|---|
| Label | span | Above the cards. It names the group for a screen reader. |
| Choicebox | radiogroup or group | The group of cards, in a grid with the number of columns you set. |
| ChoiceboxItem | label with an input | One card: a mark, an icon, a name with a line of explanation, and something at the end. The whole card can be pressed. |
| Description, error | p | A line under the cards that says more, and one in red for what is wrong. |
Keyboard
| Key | What it does |
|---|---|
| Tab | Moves into the group, to the chosen card or the first, and out of it. In a group of several, to each card. |
| ← → ↑ ↓ | In a group of one, moves to the next or the previous card and chooses it. |
| Space | Chooses the card, or in a group of several, turns it on or off. |
Examples
Usage
Each card needs a value.
Controlled
value and onChange. In a group of one the value is a string.
Chosen: pro
Several cards
selectionMode multiple. The value is a list of strings, and each card is on or off.
Chosen: analytics
Icons
Columns
The number of cards in a row, from one to six.
Indicators
Where the mark of a chosen card goes. With none, only the border and the tint of the card change.
Sizes
Disabled
In a form
Give it a name and the choice goes with the form. isRequired makes it invalid until a card is chosen, and errorMessage says why. Several cards send one value each, under the same name.
Component API
Choicebox takes these props, and each ChoiceboxItem the second table. value, defaultValue and onChange have the shape of the selection mode.
Choicebox
| Prop | Type | Default |
|---|---|---|
| selectionMode | "single" | "multiple" | "single" |
| value | string | string[] | - |
| defaultValue | string | string[] | - |
| onChange | (value) => void | - |
| label | ReactNode | - |
| description | ReactNode | - |
| errorMessage | string | (validation) => string | - |
| columns | 1 | 2 | 3 | 4 | 5 | 6 | 1 |
| size | "sm" | "md" | "lg" | "md" |
| indicator | "start" | "end" | "none" | "start" |
| name | string | - |
| isRequired | boolean | false |
| isInvalid | boolean | false |
| isDisabled | boolean | false |
| isReadOnly | boolean | false |
| aria-label | string | - |
| className | string | - |
ChoiceboxItem
| Prop | Type | Default |
|---|---|---|
| value | string | - |
| children | ReactNode | - |
| description | ReactNode | - |
| icon | ReactNode | - |
| suffix | ReactNode | - |
| aria-label | string | - |
| isDisabled | boolean | false |
| className | string | - |