Components
Radio Group
A set of options of which exactly one can be chosen, sharing one label, one value and one error. Built on react-aria-components.
Introduction
Radio Group wraps a set of RadioGroupItem options (each one a Radio) of which the user picks exactly one. The group keeps one value: the value of the chosen option.
- One label, description and error message for the whole set, and a screen reader announces the label with each option.
- The arrow keys move between options and choose them. Tab moves in and out of the group as a single stop.
- Disabled, read-only, invalid, size and variant are set once, on the group, and every option follows. An option can still override them.
- Stack the options or lay them out in a row, and switch to a card look for bigger choices.
- Works in a form: give the group a
nameand the chosen value is submitted under it.
To allow several choices use Checkbox Group.
Installation
centerUI has no CLI or registry yet: components are copied into your project. Radio Group is made of Radio options, so add Radio 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 clsxAdd Radio 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/radio-group-styles.tsShow code
import { tv } from "tailwind-variants";
// Layout of a RadioGroup: its label, the list of options, and the description
// or error under them.
export const radioGroupStyles = tv({
slots: {
root: "group/radio-group flex flex-col gap-2",
items: "flex",
},
variants: {
orientation: {
vertical: { items: "flex-col" },
horizontal: { items: "flex-row flex-wrap" },
},
variant: {
default: {},
card: {},
},
},
compoundVariants: [
{ variant: "default", class: { items: "gap-x-6 gap-y-3" } },
{ variant: "card", class: { items: "gap-2" } },
// Cards in a row share the width.
{
variant: "card",
orientation: "horizontal",
class: { items: "[&>*]:flex-1" },
},
],
defaultVariants: { orientation: "vertical", variant: "default" },
});2. Add the Radio Group file
The context file radio-group-context.ts comes with Radio.
src/components/ui/radio-group/radio-group.tsxShow code
"use client";
import { type ReactNode, useMemo } from "react";
import {
RadioGroup as AriaRadioGroup,
type RadioGroupProps as AriaRadioGroupProps,
FieldError,
Label,
Text,
type ValidationResult,
} from "react-aria-components";
import { Radio, type RadioProps } from "@/components/ui/radio/radio";
import { cn } from "@/lib/cn";
import { RadioGroupProvider } from "@/types/radio-group-context";
import { radioGroupStyles } from "@/types/radio-group-styles";
export interface RadioGroupProps
extends Omit<AriaRadioGroupProps, "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 options, or lay them out in a row. */
orientation?: "vertical" | "horizontal";
/** Look of every option. A radio can override it. */
variant?: "default" | "card";
/** Size of every option. A radio can override it. */
size?: "sm" | "md" | "lg";
className?: string;
children: ReactNode;
}
/**
* A set of options of which exactly one can be chosen. The group keeps the
* `value` of the chosen radio. Built on react-aria's RadioGroup, so the
* selection, keyboard support (arrow keys move and select) and form
* submission (`name`) come from there. Disabling, read-only, 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
* <RadioGroup label="Plan" defaultValue="pro">
* <RadioGroupItem value="free">Free</RadioGroupItem>
* <RadioGroupItem value="pro">Pro</RadioGroupItem>
* </RadioGroup>
*/
export function RadioGroup({
label,
description,
errorMessage,
orientation = "vertical",
variant,
size,
className,
children,
...props
}: RadioGroupProps) {
const styles = radioGroupStyles({ orientation, variant });
const defaults = useMemo(() => ({ size, variant }), [size, variant]);
return (
<RadioGroupProvider value={defaults}>
<AriaRadioGroup
{...props}
orientation={orientation}
className={cn(styles.root(), className)}
>
{label ? (
<Label className="text-sm font-medium group-data-required/radio-group:after:ml-0.5 group-data-required/radio-group:after:text-danger group-data-required/radio-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>
</AriaRadioGroup>
</RadioGroupProvider>
);
}
export interface RadioGroupItemProps extends RadioProps {
/** What the group's `value` is when this option is chosen. */
value: string;
}
/**
* One option of a RadioGroup: a Radio whose `value` is required, because it is
* what the group's `value` becomes when it is chosen. It takes every Radio prop
* (description, size, isDisabled...).
*/
export function RadioGroupItem(props: RadioGroupItemProps) {
return <Radio {...props} />;
}3. Use it
import {
RadioGroup,
RadioGroupItem,
} from "@/components/ui/radio-group/radio-group";Anatomy
A group is a label, a list of options, and a description or error.
<RadioGroup label="Plan" description="You can change it later.">
<RadioGroupItem value="free" description="For trying things out">Free</RadioGroupItem>
<RadioGroupItem value="pro">Pro</RadioGroupItem>
</RadioGroup>| Part | Rendered as | Notes |
|---|---|---|
| Label | label | Names the whole set. Marked with * when required. |
| Options | Radio | Each one needs a value. They read their size, variant and state from the group. |
| Description | text under the group | Help text for the whole set. |
| Error | text under the group | Replaces the description while the group is invalid. Every circle 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
Label
Every group needs a name. Use label, or aria-label when the label is written elsewhere.
Description
Help text under the group.
Item descriptions
A smaller line under each option.
Horizontal
Lay the options out in a row. They wrap when there is no room, and the Left and Right arrow keys move between them.
Card
Each option becomes a bordered box that highlights when chosen. In a horizontal group the cards share the width.
Uncontrolled
Let the group keep its own value. Start with defaultValue.
Controlled
Keep the value in your own state with value and onChange, so you can read it or set it from outside.
value: “standard”
In a form
With a name on the group, the chosen option is submitted under that name.
Sizes
Set size on the group and every option follows.
Disabled
Disable the whole group, or a single option.
Read only
The chosen option can be seen and focused, but not changed. It is still submitted with a form.
Validation
Set isInvalid with an errorMessage. Every circle gets a red border and the message replaces the description.
Component API
Radio Group and RadioGroupItem take the props below. RadioGroup also accepts everything the react-aria RadioGroup does, and RadioGroupItem everything Radio does.
RadioGroup
| Prop | Type | Default |
|---|---|---|
| label | ReactNode | - |
| description | ReactNode | - |
| errorMessage | string | (validation) => string | - |
| value | string | null | - |
| defaultValue | string | - |
| onChange | (value: string) => void | - |
| orientation | "vertical" | "horizontal" | "vertical" |
| variant | "default" | "card" | "default" |
| size | "sm" | "md" | "lg" | "md" |
| name | string | - |
| isDisabled | boolean | false |
| isReadOnly | boolean | false |
| isInvalid | boolean | false |
| isRequired | boolean | false |
| aria-label | string | - (when there is no label) |
RadioGroupItem
| Prop | Type | Default |
|---|---|---|
| value | string | - (required) |
| children | ReactNode | - |
| description | ReactNode | - |
| variant | "default" | "card" | the group's |
| size | "sm" | "md" | "lg" | the group's |
| isDisabled | boolean | the group's |
It is a Radio with a required value. A plain Radio with a value works inside a group too.