Skip to content

Radio Group

Lets people choose exactly one option from a short list that is all visible at once.

Installation

pnpm dlx shadcn@latest add @silkware/radio-group

To copy the component by hand, install its dependencies:

pnpm add react-aria-components cn

Then copy the source into components/ui/radio-group.tsx:

components/ui/radio-group.tsx
"use client"
import { cn } from "cn"import {  composeRenderProps,  RadioGroup as RadioGroupPrimitive,  Radio as RadioPrimitive,  type RadioGroupProps,  type RadioProps,} from "@animate-ui/react/primitives/react-aria"
function RadioGroup({ className, ...props }: RadioGroupProps) {  return (    <RadioGroupPrimitive      data-slot="radio-group"      className={cn("grid w-full gap-3", className)}      {...props}    />  )}
function RadioGroupItem({ className, children, ...props }: RadioProps) {  return (    <RadioPrimitive      data-slot="radio-group-item"      className={cn(        "group/radio-group-item peer relative flex aspect-square size-4 shrink-0 rounded-full border border-input outline-none group-has-[:focus-visible]/field-label:ring-0 group-has-[:focus-visible]/field-label:not-data-checked:border-input after:absolute after:-inset-x-3 after:-inset-y-2 focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/50 aria-invalid:border-destructive aria-invalid:ring-3 aria-invalid:ring-destructive/20 aria-invalid:aria-checked:border-primary data-focus-visible:border-ring data-focus-visible:ring-3 data-focus-visible:ring-ring/50 data-invalid:border-destructive data-invalid:ring-3 data-invalid:ring-destructive/20 data-[disabled]:cursor-not-allowed data-[disabled]:opacity-50 dark:bg-input/30 dark:aria-invalid:border-destructive/50 dark:aria-invalid:ring-destructive/40 dark:data-invalid:border-destructive/50 dark:data-invalid:ring-destructive/40 data-checked:border-primary data-checked:bg-primary data-checked:text-primary-foreground group-has-[:focus-visible]/field-label:data-checked:border-primary dark:data-checked:bg-primary data-selected:border-primary data-selected:bg-primary data-selected:text-primary-foreground data-invalid:data-selected:border-primary dark:data-selected:bg-primary",        className      )}      {...props}    >      {composeRenderProps(children, (children, { isSelected }) => (        <>          <span            data-slot="radio-group-indicator"            className="flex size-4 items-center justify-center"          >            {isSelected && (              <span className="absolute top-1/2 left-1/2 size-2 -translate-x-1/2 -translate-y-1/2 rounded-full bg-primary-foreground" />            )}          </span>          {children}        </>      ))}    </RadioPrimitive>  )}
export { RadioGroup, RadioGroupItem }

Usage

import { Field, FieldLabel } from "@/components/ui/field"import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"
<RadioGroup aria-label="Order priority" defaultValue="normal">  <Field orientation="horizontal">    <RadioGroupItem value="normal" id="normal" />    <FieldLabel htmlFor="normal">Normal</FieldLabel>  </Field>  <Field orientation="horizontal">    <RadioGroupItem value="urgent" id="urgent" />    <FieldLabel htmlFor="urgent">Urgent</FieldLabel>  </Field></RadioGroup>

The group needs a name: an aria-label, or an aria-labelledby that points to a FieldLegend. Each item needs a value, an id and a FieldLabel with a matching htmlFor. The group is uncontrolled with defaultValue and controlled with value and onChange, which receives the value as a string.

Examples

Description

Wrap the label and a description in FieldContent when an option needs explaining.

Two to four working days by road.

Next working day, at a higher rate.

With a legend

Give the group a visible name with FieldSet and FieldLegend, and point the group at it with aria-labelledby.

Payment terms

Agreed in the supplier contract.

Choice card

Wrap a Field in a FieldLabel to make each option a card. Put the radio last, and use FieldTitle for the title.

Horizontal

The group is a vertical grid. Pass a className to lay the items out in a row. The orientation prop sets the direction React Aria reports, and it does not change the layout.

Controlled

Hold the value in state with value and onChange.

Selected: torino

Disabled

isDisabled on RadioGroupItem disables one option. Put it on RadioGroup to disable them all. Add data-disabled to the Field so the label dims. Say why an option is unavailable in the label.

Invalid

isInvalid on the group marks every item as invalid. Add data-invalid to each Field, and show one FieldError for the group, linked with aria-describedby.

Reason for the delay

In a form with validation

With no default value, nothing is selected, and the person has to choose. Check on submit, and clear the error when they choose.

Delivery outcome

Accessibility

KeyAction
TabMoves focus into the group, to the selected option or to the first one if none is selected, and then out of it.
ShiftTabMoves focus out of the group to the previous control.
DownMoves focus to the next option and selects it.
RightMoves focus to the next option and selects it.
UpMoves focus to the previous option and selects it.
LeftMoves focus to the previous option and selects it.
SpaceSelects the focused option.

React Aria provides:

  • A radiogroup role around the items and a native radio input inside each one.
  • One tab stop for the whole group, with arrow keys inside it.
  • isDisabled, isReadOnly, isRequired and isInvalid states, and the data attributes the styles read.
  • Right-to-left support for the arrow keys.

You provide:

  • A name for the group: aria-label, or aria-labelledby pointing to the legend.
  • A visible label for every item, with an htmlFor that matches its id.
  • A default selection when one option is the common answer. A group with no selection cannot be cleared again by the person.
  • An error message in words, linked with aria-describedby.

Guidelines

When to use

  • For one choice from two to five options that people should compare side by side.
  • For a choice that applies when the form is saved.

When not to use

  • For choices that are not exclusive. Use Checkbox.
  • For a setting that applies at once. Use Switch, or Toggle Group for a view mode.
  • For more than about six options. Use Select or Native Select.
  • For a yes or no. Use a checkbox or a switch.

Content

  • Give the group a legend that asks the question: "Payment terms".
  • Keep options parallel, short and mutually exclusive. Start each in the same way.
  • Order options in a logical order: low to high, or the common one first.
  • Preselect the safe default when there is one.

Number of options

Do

Use radios for a few options, so each one is visible.

Don’t

Don’t list many options as radios. The group is long and hard to scan. Use a select.

API reference

RadioGroup

Renders the React Aria RadioGroup as a grid with a gap. It accepts every React Aria RadioGroup prop.

PropTypeDefaultDescription
value?string | nullNoneThe selected value, for a controlled group.
defaultValue?stringNoneThe value selected at the start, for an uncontrolled group.
onChange?(value: string) => voidNoneCalled when the selection changes.
name?stringNoneThe name used when the form is submitted.
orientation?"horizontal" | "vertical""vertical"The direction reported to assistive technology. It does not change the layout.
isDisabled?booleanfalseDisables every item.
isReadOnly?booleanfalseKeeps the items focusable but stops the selection from changing.
isRequired?booleanfalseAsks for a selection before the form is submitted.
isInvalid?booleanNoneMarks the group as invalid.
aria-label?stringNoneThe name of the group when there is no visible legend.
aria-labelledby?stringNoneThe id of the element that names the group, such as a legend.
className?stringNoneClasses merged after the base classes.

RadioGroupItem

Renders the React Aria Radio as a round button with a dot when selected. It accepts every React Aria Radio prop. It does not render its label, so pair it with a FieldLabel.

PropTypeDefaultDescription
valuestringNoneThe value of the option.
id?stringNoneThe id that the label points to with htmlFor.
isDisabled?booleanfalseDisables this option.
className?stringNoneClasses merged after the base classes.