Radio

Accessible React radio groups built with the RadioGroup, RadioField, and RadioCard components, from option lists to plan and color cards. Based on the WAI-ARIA Radio Group Pattern.

Examples

Radio

Descriptions

Card Badges

Times in São Paulo time (GMT−3)

Trail Jacket

$180

Step by Step

Before I create the project, I have two quick questions.

Question 1 of 2

Overview

These examples build radio groups with the Ariakit UI Radio components. A radio group shows a set of options, and a person picks exactly one of them. A visible label names the group, and the checked radio shows the current pick.

The gallery starts with a plain group that sorts comments. Then it shows options with descriptions, a text field that shows while Other is checked, and a poll that disables its group after the vote. The examples with cards show ratings without marks, theme pictures with a floating check, plans with a badge, times that depend on the picked day, feedback on a quiz answer, color swatches with a sold-out size, and a shipping option that is not available, with a right-to-left version. The next example asks the questions of an assistant in steps. The last examples are segmented controls: a row of options on one surface, with icons only, separators, a badge, and different selected surfaces.

Components

RadioGroup groups the radios and keeps the selected value. Set defaultValue to start with a checked radio, or set value and setValue to control the value. Set store to use a store from useRadioStore. The group makes its own store, so a RadioProvider around it does not reach its radios.

The label prop shows a label first in the group, and the label names the group. It takes text, a RadioGroupLabel element, or RadioGroupLabel props. The Color Swatches example puts the name of the picked color in the label. The label names the group after hydration. To name a group without a visible label, use aria-label or aria-labelledby.

The description prop shows a RadioGroupDescription right after the label, close under it. The group takes it as its accessible description, like the time zone in the Dependent Groups example. A group takes one description.

The group stacks its label, its description, its radios, and an error with the group gap. The label has the same text style as an InputLabel. By default, the gap is larger when the group has cards or radios with descriptions, because they take more space. Set $gap on the group to change it. Do not set a gap class on the group, because the description and the gaps of the items do not follow it.

RadioList stacks the radios of a group, and its gap spaces only the radios. By default, its gap is two thirds of the group gap, so the label stays further from the radios than they are from each other. Set $gap on the list to change it, such as $gap="md" for the small swatches in the Color Swatches example. Layout classes replace its column, such as the row of swatches that wraps. RadioList adds no role.

RadioField is a row with a native radio before its text. Put a RadioLabel and a RadioDescription in it to show a second line, as in the Descriptions example. The description is the accessible description of the radio, so it is not part of its name.

RadioCard is a card with a hidden native radio, and its content names the radio. RadioCardContent stacks a RadioCardLabel and a RadioCardDescription. RadioCardSlot holds other content, such as a price, a badge, or a color. A Badge in a slot with $kind="box" and $floating sits on the top-end corner of the card, as in the Dependent Groups example. A slot beside RadioCardContent takes a column of the card, so the description wraps before it. To let the description use the full width, put the label and the badge in a RadioCardLabelRow inside RadioCardContent, as in the Step by Step example. The badge wraps below the label when the row runs out of room.

RadioCardCheck draws the mark of a card. With $floating, a check shows on the top-end corner of the checked card only, as in the Floating Check example. A card without a mark shows its checked state with the brand fill and a second edge pixel, as in the Cards Without Marks example.

RadioCardGrid puts the cards of a group in columns of equal width, with rows of equal height. Like RadioList, it is only a layout and adds no role, and its gap is two thirds of the group gap by default. Put it inside a RadioGroup, and set the label, the value, and disabled on the group. $minItemSize sets the narrowest width of a card before the grid drops a column. A floating badge hangs over the top of its card, so the Dependent Groups example sets $gap="xl" on its grids to give the rows more space. When you raise the gap of the items, also raise the gap of the group, so the label stays further from the items than they are from each other. The Dependent Groups example sets $gap="lg" on its groups.

Set $kind="segmented" on a RadioGroup and on its RadioCard elements to show the options as a segmented control. Put the text of each card in a RadioCardLabel, so the label of the checked segment turns heavier without moving the other segments. A label then goes above the segments, and className and style go on the element that holds the label and the segments. The other variant props of the group style the segments’ surface: set $size for the text size, $rounded="full" for round corners, $layer="transparent" for an edge instead of the surface, $p="none" and $gap="none" for joined cells, with $edgeWeight="normal" and $borderType="border" on the cards, and $layout="stretch" to fill the container and share its width equally, as in the Segmented Control example. A group with padding that does not fit its container scrolls horizontally. Its segments fade at each end while more segments lie that way, and with a fine pointer, “Scroll backward” and “Scroll forward” buttons show beside them, outside the element that holds the segments. buttons={false} removes the buttons, and previousButton and nextButton take props. A stretched group truncates its labels instead. Set $kind="segmented-flat" on a card to replace the bevel with a shadow, $invert for the inverted thumb, and $layer for a colored thumb. Set separator on a RadioCard to draw a ButtonSeparator line before it, and a RadioCardSlot for an icon. For a badge, put a Badge in a slot with $kind="box", as in the Segmented Control with a Flat Thumb and a Badge example. A Tooltip around a segmented RadioCard anchors to the whole segment, as in the Icon-Only Segmented Control with Tooltips example.

Set disabled on a radio or a card to disable it, for example when an option is not available. Set disabled on the group to disable all of its radios, as in the Disabled Group example after the vote.

A checked radio does not change on hover, because a click on it does nothing. A click also cannot clear the pick. To let a person remove an answer, add a button that sets the value to null, like Clear in the Step by Step example.

Accessibility

A radio group is one Tab stop. Tab moves to the checked radio, or to the first radio when none was ever checked. After a pick is cleared, as with Clear in the Step by Step example, Tab moves to the radio that had focus last. The arrow keys move to the next or previous radio and check it, and they skip disabled radios. This is the radio group pattern of the ARIA Authoring Practices Guide.

The label of the group tells what the options are for. A screen reader reads it when focus moves into the group. A placeholder, a title attribute, or a tooltip does not do this for every person, as the Answer Feedback example tells.

Color alone does not show the checked radio (Use of Color). A radio shows a dot, and a floating mark shows a check. A card without a mark gets a second edge pixel, and in forced colors mode, such as a Windows contrast theme, its border gets wider. Words tell the other states: the badges of the Answer Feedback example tell the correct answer and the wrong pick, and a sold-out size in the Color Swatches example has a line through it and a description with the same text as its tooltip.

A screen reader reads a description after the name of its radio. The description of a disabled card can tell why the option is not available, as in the Disabled Option example. A badge in a card is part of the name, so the Card Badges example reads “Pro Popular.” The faces of the Cards Without Marks example and the pictures of the Floating Check example are hidden from screen readers, because each option has a word that names it. The color swatches of the Color Swatches example have aria-label.

A segment with only an icon needs aria-label, and its tooltip shows the same word on hover and on keyboard focus, as in the Icon-Only Segmented Control with Tooltips example. A selected segment has its own surface, and in high contrast mode it also has an edge. In forced colors mode, only the selected segment has an edge. Joined cells all have edges, so the selected cell has a second edge pixel, which turns wider and takes the text color in high contrast mode, and an inset outline in forced colors mode.

In the Conditional Field example, the text field comes right after the group, not inside it. The field is not one of the options, and the arrow keys move only between the radios of a group, so they would skip a field inside it (radiogroup role). When Other is checked, focus stays on the radio, because a change of focus on input can confuse people (On Input). Other is the last option, so the field shows right below it, and Tab moves from the group into the field.

In the Step by Step example, Back and Next move focus into the new question, because the button that has focus replaces the question. A button press makes this change, so it is not a change on input.

The Disabled Group and Answer Feedback examples show their result in a live region with role="status", so screen readers read it without a change of focus. Their buttons stay focusable while disabled, so focus does not go back to the start of the page.