Components
Radio
One choice of a radio group: a circle that is filled when it is the chosen one. Built on react-aria-components.
Introduction
Use radios when the user must choose exactly one option from a short list and should see all of them at once: a plan, a shipping method. Choosing one turns the others off. For a long list use Select, and to allow several choices use Checkbox.
- A radio only works inside a
Radio Group, which keeps the chosen value. The group page covers the label, value, layout and form use. - Keyboard support (the arrow keys move and choose, Tab enters and leaves the group) and screen reader support come from react-aria-components.
- A description under the label, and a card look that turns the whole option into a box that highlights when chosen.
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/radio-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for Radio. The circle and the label read the state of the radio
// (selected, focused, invalid...) from data attributes on the root, which
// react-aria sets, through the `group/radio` name.
export const radioStyles = tv({
slots: {
root: [
"group/radio relative flex cursor-pointer items-start gap-2.5",
"data-disabled:cursor-not-allowed data-disabled:opacity-50",
],
box: [
"flex shrink-0 items-center justify-center rounded-full border bg-bg text-primary-fg transition-colors",
"group-data-hovered/radio:border-fg/50",
"group-data-selected/radio:border-primary group-data-selected/radio:bg-primary",
"group-data-focus-visible/radio:ring-2 group-data-focus-visible/radio:ring-ring group-data-focus-visible/radio:ring-offset-2 group-data-focus-visible/radio:ring-offset-bg",
"group-data-invalid/radio:border-danger group-data-invalid/radio:group-data-selected/radio:bg-danger group-data-invalid/radio:group-data-selected/radio:text-danger-fg",
],
// The dot inside a selected circle. It takes the color of the text.
dot: "rounded-full bg-current",
label: "font-medium leading-snug",
description: "text-muted-fg leading-snug",
error: "text-danger leading-snug",
},
variants: {
size: {
sm: {
root: "text-sm",
box: "mt-[3px] size-3.5",
dot: "size-1",
description: "text-xs",
error: "text-xs",
},
md: {
root: "text-sm",
box: "mt-0.5 size-4",
dot: "size-1.5",
description: "text-sm",
error: "text-sm",
},
lg: {
root: "text-base",
box: "mt-0.5 size-5",
dot: "size-2",
description: "text-sm",
error: "text-sm",
},
},
// "card" turns the whole option into a bordered box that highlights when
// it is selected.
variant: {
default: {},
card: {
root: [
"rounded-lg border p-3 transition-colors",
"data-hovered:bg-muted/50",
"data-selected:border-primary data-selected:bg-primary/5",
"data-invalid:border-danger",
],
},
},
},
defaultVariants: { size: "md", variant: "default" },
});2. Add the group context
Radio reads its size and variant from an enclosing Radio Group through this small file. It works fine without a group's defaults.
src/types/radio-group-context.tsShow code
import { createContext, useContext } from "react";
import type { VariantProps } from "tailwind-variants";
import type { radioStyles } from "./radio-styles";
/** Defaults a RadioGroup passes down to the radios inside it. */
export type RadioGroupContextValue = Pick<
VariantProps<typeof radioStyles>,
"size" | "variant"
>;
const RadioGroupContext = createContext<RadioGroupContextValue | null>(null);
export const RadioGroupProvider = RadioGroupContext.Provider;
/** The enclosing RadioGroup's defaults, or null outside of one. */
export function useRadioGroup() {
return useContext(RadioGroupContext);
}3. Add the Radio file
src/components/ui/radio/radio.tsxShow code
"use client";
import { type ReactNode, useId } from "react";
import {
Radio as AriaRadio,
type RadioProps as AriaRadioProps,
} from "react-aria-components";
import { cn } from "@/lib/cn";
import { useRadioGroup } from "@/types/radio-group-context";
import { radioStyles } from "@/types/radio-styles";
export interface RadioProps
extends Omit<AriaRadioProps, "children" | "className"> {
/** The text next to the circle. */
children?: ReactNode;
/** A smaller line under the label. */
description?: ReactNode;
/** The circle only, or the whole option as a bordered card. */
variant?: "default" | "card";
size?: "sm" | "md" | "lg";
className?: string;
}
/**
* One choice of a RadioGroup: a circle that is filled when it is the chosen
* one. It only works inside a `RadioGroup`, which holds the selection, so at
* most one radio of a group is on. Built on react-aria's Radio, so keyboard
* support (arrow keys move and select, Tab enters and leaves the group) and
* screen reader support come from there.
*
* Its `size` and `variant` default to the group's.
*
* @example
* <RadioGroup label="Plan" defaultValue="pro">
* <Radio value="free">Free</Radio>
* <Radio value="pro" description="For individuals">Pro</Radio>
* </RadioGroup>
*/
export function Radio({
children,
description,
variant,
size,
className,
...props
}: RadioProps) {
// Same channel RadioGroup uses, so a radio reads its defaults from it.
const group = useRadioGroup();
const styles = radioStyles({
size: size ?? group?.size,
variant: variant ?? group?.variant,
});
const descriptionId = useId();
return (
<AriaRadio
aria-describedby={description ? descriptionId : undefined}
{...props}
className={cn(styles.root(), className)}
>
{({ isSelected }) => (
<>
<span aria-hidden="true" className={styles.box()}>
{isSelected ? <span className={styles.dot()} /> : null}
</span>
{children || description ? (
<span className="flex min-w-0 flex-col gap-0.5">
{children ? (
<span className={styles.label()}>{children}</span>
) : null}
{description ? (
<span id={descriptionId} className={styles.description()}>
{description}
</span>
) : null}
</span>
) : null}
</>
)}
</AriaRadio>
);
}4. Add a Radio Group
A radio needs a group around it. Follow the Manual installation on the Radio Group page, then use both.
import { Radio } from "@/components/ui/radio/radio";
import { RadioGroup } from "@/components/ui/radio-group/radio-group";Anatomy
A radio is a circle with a label, inside a group.
<RadioGroup aria-label="Plan" defaultValue="pro">
<Radio value="free">Free</Radio>
<Radio value="pro" description="For individuals">Pro</Radio>
</RadioGroup>| Part | Rendered as | Notes |
|---|---|---|
| Circle | span | The round box. Filled, with a dot, when this is the chosen option. |
| Label | span | The text next to the circle. Clicking it chooses the option. |
| Description | span | A smaller line under the label. |
The group gets the label or aria-label. A radio without text needs its own aria-label.
Examples
Every radio sits in a group. See the Radio Group page for the group's own options.
Usage
Description
A smaller line under the label.
Card
The whole option becomes a bordered box that highlights when chosen, which suits plans and other bigger choices.
Sizes
Disabled
Disable one option with isDisabled. It cannot be chosen and is skipped by the arrow keys.
Component API
Radio takes the props below, plus everything the react-aria Radio accepts.
| 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 |
| aria-label | string | - (when there is no text) |
Selection, orientation, the invalid state and the form name belong to the Radio Group.