RadioGroup
A radio group allows a user to select a single item from a list of mutually exclusive options.
install | yarn add react-aria-components |
---|---|
version | 1.0.0-beta.2 |
usage | import {RadioGroup} from 'react-aria-components' |
Example#
import {RadioGroup, Radio, Label} from 'react-aria-components';
<RadioGroup>
<Label>Favorite pet</Label>
<Radio value="dogs">Dog</Radio>
<Radio value="cats">Cat</Radio>
<Radio value="dragon">Dragon</Radio>
</RadioGroup>
import {
Label,
Radio,
RadioGroup
} from 'react-aria-components';
<RadioGroup>
<Label>Favorite pet</Label>
<Radio value="dogs">Dog</Radio>
<Radio value="cats">Cat</Radio>
<Radio value="dragon">Dragon</Radio>
</RadioGroup>
import {
Label,
Radio,
RadioGroup
} from 'react-aria-components';
<RadioGroup>
<Label>
Favorite pet
</Label>
<Radio value="dogs">
Dog
</Radio>
<Radio value="cats">
Cat
</Radio>
<Radio value="dragon">
Dragon
</Radio>
</RadioGroup>
Show CSS
.react-aria-RadioGroup {
display: flex;
flex-direction: column;
gap: 8px;
&[data-orientation=horizontal] {
flex-direction: row;
align-items: center;
}
& [slot=description] {
font-size: 12px;
}
& [slot=errorMessage] {
font-size: 12px;
color: var(--spectrum-global-color-red-600);
}
}
.react-aria-Radio {
--label-color: var(--spectrum-alias-text-color);
--deselected-color: gray;
--deselected-color-pressed: dimgray;
--background-color: var(--spectrum-global-color-gray-50);
--selected-color: slateblue;
--selected-color-pressed: lch(from slateblue calc(l - 10%) c h);
--invalid-color: var(--spectrum-global-color-red-600);
--invalid-color-pressed: var(--spectrum-global-color-red-700);
display: flex;
align-items: center;
gap: 0.571rem;
font-size: 1.143rem;
color: var(--label-color);
&:before {
content: '';
display: block;
width: 1.286rem;
height: 1.286rem;
box-sizing: border-box;
border: 0.143rem solid var(--deselected-color);
background: var(--background-color);
border-radius: 1.286rem;
transition: all 200ms;
}
&[data-pressed]:before {
border-color: var(--deselected-color-pressed);
}
&[data-selected] {
&:before {
border-color: var(--selected-color);
border-width: 0.429rem;
}
&[data-pressed]:before {
border-color: var(--selected-color-pressed);
}
}
&[data-invalid] {
&:before {
border-color: var(--invalid-color);
}
&[data-pressed]:before {
border-color: var(--invalid-color-pressed);
}
}
&[data-focus-visible]:before {
box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px var(--selected-color);
}
&[data-disabled] {
opacity: 0.4;
}
}
@media (forced-colors: active) {
.react-aria-Radio {
forced-color-adjust: none;
--label-color: ButtonText;
--deselected-color: ButtonBorder;
--deselected-color-pressed: ButtonBorder;
--selected-color: Highlight;
--selected-color-pressed: Highlight;
--background-color: HighlightText;
--invalid-color: LinkText;
--invalid-color-pressed: LinkText;
--spectrum-alias-background-color-default: Canvas;
&[data-disabled] {
opacity: 1;
--deselected-color: GrayText;
--selected-color: GrayText;
--label-color: GrayText;
}
}
}
.react-aria-RadioGroup {
display: flex;
flex-direction: column;
gap: 8px;
&[data-orientation=horizontal] {
flex-direction: row;
align-items: center;
}
& [slot=description] {
font-size: 12px;
}
& [slot=errorMessage] {
font-size: 12px;
color: var(--spectrum-global-color-red-600);
}
}
.react-aria-Radio {
--label-color: var(--spectrum-alias-text-color);
--deselected-color: gray;
--deselected-color-pressed: dimgray;
--background-color: var(--spectrum-global-color-gray-50);
--selected-color: slateblue;
--selected-color-pressed: lch(from slateblue calc(l - 10%) c h);
--invalid-color: var(--spectrum-global-color-red-600);
--invalid-color-pressed: var(--spectrum-global-color-red-700);
display: flex;
align-items: center;
gap: 0.571rem;
font-size: 1.143rem;
color: var(--label-color);
&:before {
content: '';
display: block;
width: 1.286rem;
height: 1.286rem;
box-sizing: border-box;
border: 0.143rem solid var(--deselected-color);
background: var(--background-color);
border-radius: 1.286rem;
transition: all 200ms;
}
&[data-pressed]:before {
border-color: var(--deselected-color-pressed);
}
&[data-selected] {
&:before {
border-color: var(--selected-color);
border-width: 0.429rem;
}
&[data-pressed]:before {
border-color: var(--selected-color-pressed);
}
}
&[data-invalid] {
&:before {
border-color: var(--invalid-color);
}
&[data-pressed]:before {
border-color: var(--invalid-color-pressed);
}
}
&[data-focus-visible]:before {
box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px var(--selected-color);
}
&[data-disabled] {
opacity: 0.4;
}
}
@media (forced-colors: active) {
.react-aria-Radio {
forced-color-adjust: none;
--label-color: ButtonText;
--deselected-color: ButtonBorder;
--deselected-color-pressed: ButtonBorder;
--selected-color: Highlight;
--selected-color-pressed: Highlight;
--background-color: HighlightText;
--invalid-color: LinkText;
--invalid-color-pressed: LinkText;
--spectrum-alias-background-color-default: Canvas;
&[data-disabled] {
opacity: 1;
--deselected-color: GrayText;
--selected-color: GrayText;
--label-color: GrayText;
}
}
}
.react-aria-RadioGroup {
display: flex;
flex-direction: column;
gap: 8px;
&[data-orientation=horizontal] {
flex-direction: row;
align-items: center;
}
& [slot=description] {
font-size: 12px;
}
& [slot=errorMessage] {
font-size: 12px;
color: var(--spectrum-global-color-red-600);
}
}
.react-aria-Radio {
--label-color: var(--spectrum-alias-text-color);
--deselected-color: gray;
--deselected-color-pressed: dimgray;
--background-color: var(--spectrum-global-color-gray-50);
--selected-color: slateblue;
--selected-color-pressed: lch(from slateblue calc(l - 10%) c h);
--invalid-color: var(--spectrum-global-color-red-600);
--invalid-color-pressed: var(--spectrum-global-color-red-700);
display: flex;
align-items: center;
gap: 0.571rem;
font-size: 1.143rem;
color: var(--label-color);
&:before {
content: '';
display: block;
width: 1.286rem;
height: 1.286rem;
box-sizing: border-box;
border: 0.143rem solid var(--deselected-color);
background: var(--background-color);
border-radius: 1.286rem;
transition: all 200ms;
}
&[data-pressed]:before {
border-color: var(--deselected-color-pressed);
}
&[data-selected] {
&:before {
border-color: var(--selected-color);
border-width: 0.429rem;
}
&[data-pressed]:before {
border-color: var(--selected-color-pressed);
}
}
&[data-invalid] {
&:before {
border-color: var(--invalid-color);
}
&[data-pressed]:before {
border-color: var(--invalid-color-pressed);
}
}
&[data-focus-visible]:before {
box-shadow: 0 0 0 2px var(--spectrum-alias-background-color-default), 0 0 0 4px var(--selected-color);
}
&[data-disabled] {
opacity: 0.4;
}
}
@media (forced-colors: active) {
.react-aria-Radio {
forced-color-adjust: none;
--label-color: ButtonText;
--deselected-color: ButtonBorder;
--deselected-color-pressed: ButtonBorder;
--selected-color: Highlight;
--selected-color-pressed: Highlight;
--background-color: HighlightText;
--invalid-color: LinkText;
--invalid-color-pressed: LinkText;
--spectrum-alias-background-color-default: Canvas;
&[data-disabled] {
opacity: 1;
--deselected-color: GrayText;
--selected-color: GrayText;
--label-color: GrayText;
}
}
}
Features#
Radio groups can be built in HTML with the
<fieldset>
and <input> elements,
however these can be difficult to style. RadioGroup
and Radio
help achieve accessible
radio groups that can be styled as needed.
- Accessible – Radio groups are exposed to assistive technology via ARIA, and automatically associate a nested
<Label>
. Description and error message help text slots are supported as well. - HTML form integration – Each individual radio uses a visually hidden
<input>
element under the hood, which enables HTML form integration and autofill. - Cross-browser – Mouse, touch, keyboard, and focus interactions are normalized to ensure consistency across browsers and devices.
Anatomy#
A radio group consists of a set of radio buttons, and a label. Each radio includes a label and a visual selection indicator. A single radio button within the group can be selected at a time. Users may click or touch a radio button to select it, or use the Tab key to navigate to the group, the arrow keys to navigate within the group, and the Space key to select an option.
RadioGroup
also supports optional description and error message elements, which can be used
to provide more context about the field, and any validation messages. These are linked with the
inputs via the aria-describedby
attribute.
import {RadioGroup, Radio, Label, Text} from 'react-aria-components';
<RadioGroup>
<Label />
<Radio />
<Text slot="description" />
<Text slot="errorMessage" />
</RadioGroup>
import {
Label,
Radio,
RadioGroup,
Text
} from 'react-aria-components';
<RadioGroup>
<Label />
<Radio />
<Text slot="description" />
<Text slot="errorMessage" />
</RadioGroup>
import {
Label,
Radio,
RadioGroup,
Text
} from 'react-aria-components';
<RadioGroup>
<Label />
<Radio />
<Text slot="description" />
<Text slot="errorMessage" />
</RadioGroup>
Individual radio buttons must have a visual label. If the radio group does not have a visible label,
an aria-label
or aria-labelledby
prop must be passed instead to identify the element to assistive
technology.
Composed components#
A RadioGroup
uses the following components, which may also be used standalone or reused in other components.
Examples#
Reusable wrappers#
If you will use a RadioGroup in multiple places in your app, you can wrap all of the pieces into a reusable component. This way, the DOM structure, styling code, and other logic are defined in a single place and reused everywhere to ensure consistency.
This example wraps RadioGroup
and all of its children together into a single component which accepts a label
prop, which is passed to the right place. It also shows how to use the description
and errorMessage
slots to render help text (see below for details).
import type {RadioGroupProps} from 'react-aria-components';
import {Text} from 'react-aria-components';
interface MyRadioGroupProps extends Omit<RadioGroupProps, 'children'> {
children?: React.ReactNode,
label?: string,
description?: string,
errorMessage?: string
}
function MyRadioGroup({
label,
description,
errorMessage,
children,
...props
}: MyRadioGroupProps) {
return (
<RadioGroup {...props}>
<Label>{label}</Label>
{children}
{description && <Text slot="description">{description}</Text>}
{errorMessage && <Text slot="errorMessage">{errorMessage}</Text>}
</RadioGroup>
);
}
<MyRadioGroup label="Favorite sport">
<Radio value="soccer">Soccer</Radio>
<Radio value="baseball">Baseball</Radio>
<Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
import type {RadioGroupProps} from 'react-aria-components';
import {Text} from 'react-aria-components';
interface MyRadioGroupProps
extends Omit<RadioGroupProps, 'children'> {
children?: React.ReactNode;
label?: string;
description?: string;
errorMessage?: string;
}
function MyRadioGroup({
label,
description,
errorMessage,
children,
...props
}: MyRadioGroupProps) {
return (
<RadioGroup {...props}>
<Label>{label}</Label>
{children}
{description && (
<Text slot="description">{description}</Text>
)}
{errorMessage && (
<Text slot="errorMessage">{errorMessage}</Text>
)}
</RadioGroup>
);
}
<MyRadioGroup label="Favorite sport">
<Radio value="soccer">Soccer</Radio>
<Radio value="baseball">Baseball</Radio>
<Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
import type {RadioGroupProps} from 'react-aria-components';
import {Text} from 'react-aria-components';
interface MyRadioGroupProps
extends
Omit<
RadioGroupProps,
'children'
> {
children?:
React.ReactNode;
label?: string;
description?: string;
errorMessage?: string;
}
function MyRadioGroup({
label,
description,
errorMessage,
children,
...props
}: MyRadioGroupProps) {
return (
<RadioGroup
{...props}
>
<Label>
{label}
</Label>
{children}
{description && (
<Text slot="description">
{description}
</Text>
)}
{errorMessage && (
<Text slot="errorMessage">
{errorMessage}
</Text>
)}
</RadioGroup>
);
}
<MyRadioGroup label="Favorite sport">
<Radio value="soccer">
Soccer
</Radio>
<Radio value="baseball">
Baseball
</Radio>
<Radio value="basketball">
Basketball
</Radio>
</MyRadioGroup>
Value#
Default value#
An initial, uncontrolled value can be provided to the RadioGroup using the defaultValue
prop, which accepts a value corresponding with the value
prop of each Radio.
<MyRadioGroup label="Are you a wizard?" defaultValue="yes">
<Radio value="yes">Yes</Radio>
<Radio value="no">No</Radio>
</MyRadioGroup>
<MyRadioGroup label="Are you a wizard?" defaultValue="yes">
<Radio value="yes">Yes</Radio>
<Radio value="no">No</Radio>
</MyRadioGroup>
<MyRadioGroup
label="Are you a wizard?"
defaultValue="yes"
>
<Radio value="yes">
Yes
</Radio>
<Radio value="no">
No
</Radio>
</MyRadioGroup>
Controlled value#
A controlled value can be provided using the value
prop, which accepts a value corresponding with the value
prop of each Radio.
The onChange
event is fired when the user selects a radio.
function Example() {
let [selected, setSelected] = React.useState('');
return (
<>
<MyRadioGroup
label="Favorite avatar"
value={selected}
onChange={setSelected}
>
<Radio value="wizard">Wizard</Radio>
<Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<p>You have selected: {selected}</p>
</>
);
}
function Example() {
let [selected, setSelected] = React.useState('');
return (
<>
<MyRadioGroup
label="Favorite avatar"
value={selected}
onChange={setSelected}
>
<Radio value="wizard">Wizard</Radio>
<Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<p>You have selected: {selected}</p>
</>
);
}
function Example() {
let [
selected,
setSelected
] = React.useState('');
return (
<>
<MyRadioGroup
label="Favorite avatar"
value={selected}
onChange={setSelected}
>
<Radio value="wizard">
Wizard
</Radio>
<Radio value="dragon">
Dragon
</Radio>
</MyRadioGroup>
<p>
You have
selected:{' '}
{selected}
</p>
</>
);
}
HTML forms#
RadioGroup supports the name
prop, paired with the Radio value
prop, for integration with HTML forms.
<MyRadioGroup label="Favorite pet" name="pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</MyRadioGroup>
<MyRadioGroup label="Favorite pet" name="pet">
<Radio value="dogs">Dogs</Radio>
<Radio value="cats">Cats</Radio>
</MyRadioGroup>
<MyRadioGroup
label="Favorite pet"
name="pet"
>
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
</MyRadioGroup>
Validation#
RadioGroups can display a validation state to communicate to the user if the current value is invalid.
Implement your own validation logic in your app and set the isInvalid
prop accordingly.
<MyRadioGroup label="Favorite avatar" isInvalid>
<Radio value="wizard">Wizard</Radio>
<Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup label="Favorite avatar" isInvalid>
<Radio value="wizard">Wizard</Radio>
<Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup
label="Favorite avatar"
isInvalid
>
<Radio value="wizard">
Wizard
</Radio>
<Radio value="dragon">
Dragon
</Radio>
</MyRadioGroup>
Help text#
The description
slot can be used to associate additional help text with a radio group. Additionally, the errorMessage
slot can be used to help the user fix a validation error. It should be combined with the isInvalid
prop to semantically mark the radio group as invalid for assistive technologies.
function Example() {
let [selected, setSelected] = React.useState('dogs');
let isValid = selected === 'dogs';
return (
<RadioGroup
value={selected}
onChange={setSelected}
isInvalid={!isValid} >
<Label>Favorite pet</Label>
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
<Radio value="dragons">
Dragons
</Radio>
{isValid && <Text slot="description">Please select a pet.</Text>}
{!isValid && (
<Text slot="errorMessage">
{selected === 'cats' ? 'No cats allowed.' : 'Please select dogs.'}
</Text>
)} </RadioGroup>
);
}
function Example() {
let [selected, setSelected] = React.useState('dogs');
let isValid = selected === 'dogs';
return (
<RadioGroup
value={selected}
onChange={setSelected}
isInvalid={!isValid} >
<Label>Favorite pet</Label>
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
<Radio value="dragons">
Dragons
</Radio>
{isValid && (
<Text slot="description">Please select a pet.</Text>
)}
{!isValid && (
<Text slot="errorMessage">
{selected === 'cats'
? 'No cats allowed.'
: 'Please select dogs.'}
</Text>
)} </RadioGroup>
);
}
function Example() {
let [
selected,
setSelected
] = React.useState(
'dogs'
);
let isValid =
selected === 'dogs';
return (
<RadioGroup
value={selected}
onChange={setSelected}
isInvalid={!isValid} >
<Label>
Favorite pet
</Label>
<Radio value="dogs">
Dogs
</Radio>
<Radio value="cats">
Cats
</Radio>
<Radio value="dragons">
Dragons
</Radio>
{isValid && (
<Text slot="description">
Please select a
pet.
</Text>
)}
{!isValid && (
<Text slot="errorMessage">
{selected ===
'cats'
? 'No cats allowed.'
: 'Please select dogs.'}
</Text>
)} </RadioGroup>
);
}
Orientation#
RadioGroups are vertically oriented by default.
The orientation
prop can be used to change the orientation to horizontal.
<MyRadioGroup label="Favorite avatar" orientation="horizontal">
<Radio value="wizard">Wizard</Radio>
<Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup
label="Favorite avatar"
orientation="horizontal"
>
<Radio value="wizard">Wizard</Radio>
<Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup
label="Favorite avatar"
orientation="horizontal"
>
<Radio value="wizard">
Wizard
</Radio>
<Radio value="dragon">
Dragon
</Radio>
</MyRadioGroup>
Disabled#
The entire RadioGroup can be disabled with the isDisabled
prop.
<MyRadioGroup label="Favorite sport" isDisabled>
<Radio value="soccer">Soccer</Radio>
<Radio value="baseball">Baseball</Radio>
<Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
<MyRadioGroup label="Favorite sport" isDisabled>
<Radio value="soccer">Soccer</Radio>
<Radio value="baseball">Baseball</Radio>
<Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
<MyRadioGroup
label="Favorite sport"
isDisabled
>
<Radio value="soccer">
Soccer
</Radio>
<Radio value="baseball">
Baseball
</Radio>
<Radio value="basketball">
Basketball
</Radio>
</MyRadioGroup>
To disable an individual radio, pass isDisabled
to the Radio
instead.
<MyRadioGroup label="Favorite sport">
<Radio value="soccer">Soccer</Radio>
<Radio value="baseball" isDisabled>Baseball</Radio>
<Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
<MyRadioGroup label="Favorite sport">
<Radio value="soccer">Soccer</Radio>
<Radio value="baseball" isDisabled>Baseball</Radio>
<Radio value="basketball">Basketball</Radio>
</MyRadioGroup>
<MyRadioGroup label="Favorite sport">
<Radio value="soccer">
Soccer
</Radio>
<Radio
value="baseball"
isDisabled
>
Baseball
</Radio>
<Radio value="basketball">
Basketball
</Radio>
</MyRadioGroup>
Read only#
The isReadOnly
prop makes the selection immutable. Unlike isDisabled
, the RadioGroup remains focusable.
See the MDN docs for more information.
<MyRadioGroup label="Favorite avatar" defaultValue="wizard" isReadOnly>
<Radio value="wizard">Wizard</Radio>
<Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup
label="Favorite avatar"
defaultValue="wizard"
isReadOnly
>
<Radio value="wizard">Wizard</Radio>
<Radio value="dragon">Dragon</Radio>
</MyRadioGroup>
<MyRadioGroup
label="Favorite avatar"
defaultValue="wizard"
isReadOnly
>
<Radio value="wizard">
Wizard
</Radio>
<Radio value="dragon">
Dragon
</Radio>
</MyRadioGroup>
Props#
RadioGroup#
Name | Type | Default | Description |
orientation | Orientation | 'vertical' | The axis the Radio Button(s) should align with. |
value | string | — | The current value (controlled). |
defaultValue | string | — | The default value (uncontrolled). |
isDisabled | boolean | — | Whether the input is disabled. |
isReadOnly | boolean | — | Whether the input can be selected but not changed by the user. |
name | string | — | The name of the input element, used when submitting an HTML form. See MDN. |
isRequired | boolean | — | Whether user input is required on the input before form submission. |
isInvalid | boolean | — | Whether the input value is invalid. |
children | ReactNode | (
(values: RadioGroupRenderProps
)) => ReactNode | — | The children of the component. A function may be provided to alter the children based on component state. |
className | string | (
(values: RadioGroupRenderProps
)) => string | — | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | CSSProperties | (
(values: RadioGroupRenderProps
)) => CSSProperties | — | The inline style for the element. A function may be provided to compute the style based on component state. |
Events
Name | Type | Description |
onChange | (
(value: T
)) => void | Handler that is called when the value changes. |
Layout
Name | Type | Description |
slot | string | null | A slot name for the component. Slots allow the component to receive props from a parent component.
An explicit |
Accessibility
Name | Type | Description |
id | string | The element's unique identifier. See MDN. |
aria-label | string | Defines a string value that labels the current element. |
aria-labelledby | string | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | Identifies the element (or elements) that describes the object. |
aria-details | string | Identifies the element (or elements) that provide a detailed, extended description for the object. |
aria-errormessage | string | Identifies the element that provides an error message for the object. |
Radio#
Name | Type | Description |
value | string | The value of the radio button, used when submitting an HTML form. See MDN. |
isDisabled | boolean | Whether the radio button is disabled or not. Shows that a selection exists, but is not available in that circumstance. |
autoFocus | boolean | Whether the element should receive focus on render. |
children | ReactNode | (
(values: RadioRenderProps
)) => ReactNode | The children of the component. A function may be provided to alter the children based on component state. |
className | string | (
(values: RadioRenderProps
)) => string | The CSS className for the element. A function may be provided to compute the class based on component state. |
style | CSSProperties | (
(values: RadioRenderProps
)) => CSSProperties | The inline style for the element. A function may be provided to compute the style based on component state. |
Events
Name | Type | Description |
onFocus | (
(e: FocusEvent<Target>
)) => void | Handler that is called when the element receives focus. |
onBlur | (
(e: FocusEvent<Target>
)) => void | Handler that is called when the element loses focus. |
onFocusChange | (
(isFocused: boolean
)) => void | Handler that is called when the element's focus status changes. |
onKeyDown | (
(e: KeyboardEvent
)) => void | Handler that is called when a key is pressed. |
onKeyUp | (
(e: KeyboardEvent
)) => void | Handler that is called when a key is released. |
Layout
Name | Type | Description |
slot | string | null | A slot name for the component. Slots allow the component to receive props from a parent component.
An explicit |
Accessibility
Name | Type | Description |
id | string | The element's unique identifier. See MDN. |
aria-label | string | Defines a string value that labels the current element. |
aria-labelledby | string | Identifies the element (or elements) that labels the current element. |
aria-describedby | string | Identifies the element (or elements) that describes the object. |
aria-details | string | Identifies the element (or elements) that provide a detailed, extended description for the object. |
Styling#
React Aria components can be styled in many ways, including using CSS classes, inline styles, utility classes (e.g. Tailwind), CSS-in-JS (e.g. Styled Components), etc. By default, all components include a builtin className
attribute which can be targeted using CSS selectors. These follow the react-aria-ComponentName
naming convention.
.react-aria-Radio {
/* ... */
}
.react-aria-Radio {
/* ... */
}
.react-aria-Radio {
/* ... */
}
A custom className
can also be specified on any component. This overrides the default className
provided by React Aria with your own.
<Radio className="my-radio">
{/* ... */}
</Radio>
<Radio className="my-radio">
{/* ... */}
</Radio>
<Radio className="my-radio">
{/* ... */}
</Radio>
In addition, some components support multiple UI states (e.g. focused, placeholder, readonly, etc.). React Aria components expose states using data attributes, which you can target in CSS selectors. For example:
.react-aria-Radio[data-selected] {
/* ... */
}
.react-aria-Radio[data-selected] {
/* ... */
}
.react-aria-Radio[data-selected] {
/* ... */
}
The className
and style
props also accept functions which receive states for styling. This lets you dynamically determine the classes or styles to apply, which is useful when using utility CSS libraries like Tailwind.
<Radio
className={({ isPressed }) => isPressed ? 'bg-gray-700' : 'bg-gray-600'}
/>
<Radio
className={({ isPressed }) =>
isPressed ? 'bg-gray-700' : 'bg-gray-600'}
/>
<Radio
className={(
{ isPressed }
) =>
isPressed
? 'bg-gray-700'
: 'bg-gray-600'}
/>
Render props may also be used as children to alter what elements are rendered based on the current state. For example, you could render an extra element when the radio is selected.
<Radio>
{({isSelected}) => (
<>
{isSelected && <SelectedIcon />}
Option
</>
)}
</Radio>
<Radio>
{({isSelected}) => (
<>
{isSelected && <SelectedIcon />}
Option
</>
)}
</Radio>
<Radio>
{(
{ isSelected }
) => (
<>
{isSelected && (
<SelectedIcon />
)}
Option
</>
)}
</Radio>
The states and selectors for each component used in a RadioGroup
are documented below.
RadioGroup#
A RadioGroup
can be targeted with the .react-aria-RadioGroup
CSS selector, or by overriding with a custom className
. It supports the following states and render props:
Name | CSS Selector | Description |
orientation | [data-orientation="horizontal | vertical"] | The orientation of the radio group. |
isDisabled | [data-disabled] | Whether the radio group is disabled. |
isReadOnly | [data-readonly] | Whether the radio group is read only. |
isRequired | [data-required] | Whether the radio group is required. |
isInvalid | [data-invalid] | Whether the radio group is invalid. |
state | — | State of the radio group. |
Radio#
A Radio
can be targeted with the .react-aria-Radio
CSS selector, or by overriding with a custom className
. It supports the following states:
Name | CSS Selector | Description |
isSelected | [data-selected] | Whether the radio is selected. |
isHovered | [data-hovered] | Whether the radio is currently hovered with a mouse. |
isPressed | [data-pressed] | Whether the radio is currently in a pressed state. |
isFocused | [data-focused] | Whether the radio is focused, either via a mouse or keyboard. |
isFocusVisible | [data-focus-visible] | Whether the radio is keyboard focused. |
isDisabled | [data-disabled] | Whether the radio is disabled. |
isReadOnly | [data-readonly] | Whether the radio is read only. |
isInvalid | [data-invalid] | Whether the radio is invalid. |
isRequired | [data-required] | Whether the checkbox is required. |
Text#
The help text elements within a RadioGroup
can be targeted with the [slot=description]
and [slot=errorMessage]
CSS selectors, or by adding a custom className
.
Advanced customization#
Contexts#
All React Aria Components export a corresponding context that can be used to send props to them from a parent element. This enables you to build your own compositional APIs similar to those found in React Aria Components itself. You can send any prop or ref via context that you could pass to the corresponding component. The local props and ref on the component are merged with the ones passed via context, with the local props taking precedence (following the rules documented in mergeProps).
Component | Context | Props | Ref |
RadioGroup | RadioGroupContext | RadioGroupProps | HTMLDivElement |
Radio | RadioContext | RadioProps | HTMLInputElement |
This example shows a RadioDescription
component that accepts a radio in its children and renders a description element below it. It uses the useId hook to generate a unique id for the description, and associates it with the radio via the aria-describedby
attribute passed to the RadioContext
provider.
import {RadioContext} from 'react-aria-components';
import {useId} from 'react-aria';
interface RadioDescriptionProps {
children?: React.ReactNode;
description?: string;
}
function RadioDescription({ children, description }: RadioDescriptionProps) {
let descriptionId = useId();
return (
<div>
<RadioContext.Provider value={{ 'aria-describedby': descriptionId }}> {children}
</RadioContext.Provider>
<small id={descriptionId}>{description}</small>
</div>
);
}
<MyRadioGroup label="Show scrollbars" defaultValue="automatic">
<RadioDescription description="Scrollbars will always be visible when using a mouse, and only while scrolling when using a trackpad.">
<Radio value="automatic">Automatic</Radio>
</RadioDescription>
<RadioDescription description="Scrollbars will appear only while you are scrolling.">
<Radio value="scrolling">While scrolling</Radio>
</RadioDescription>
<RadioDescription description="Scrollbars will always be visible.">
<Radio value="always">Always</Radio>
</RadioDescription>
</MyRadioGroup>
import {RadioContext} from 'react-aria-components';
import {useId} from 'react-aria';
interface RadioDescriptionProps {
children?: React.ReactNode;
description?: string;
}
function RadioDescription(
{ children, description }: RadioDescriptionProps
) {
let descriptionId = useId();
return (
<div>
<RadioContext.Provider
value={{ 'aria-describedby': descriptionId }}
> {children}
</RadioContext.Provider>
<small id={descriptionId}>{description}</small>
</div>
);
}
<MyRadioGroup
label="Show scrollbars"
defaultValue="automatic"
>
<RadioDescription description="Scrollbars will always be visible when using a mouse, and only while scrolling when using a trackpad.">
<Radio value="automatic">Automatic</Radio>
</RadioDescription>
<RadioDescription description="Scrollbars will appear only while you are scrolling.">
<Radio value="scrolling">While scrolling</Radio>
</RadioDescription>
<RadioDescription description="Scrollbars will always be visible.">
<Radio value="always">Always</Radio>
</RadioDescription>
</MyRadioGroup>
import {RadioContext} from 'react-aria-components';
import {useId} from 'react-aria';
interface RadioDescriptionProps {
children?:
React.ReactNode;
description?: string;
}
function RadioDescription(
{
children,
description
}: RadioDescriptionProps
) {
let descriptionId =
useId();
return (
<div>
<RadioContext.Provider
value={{
'aria-describedby':
descriptionId
}}
> {children}
</RadioContext.Provider>
<small
id={descriptionId}
>
{description}
</small>
</div>
);
}
<MyRadioGroup
label="Show scrollbars"
defaultValue="automatic"
>
<RadioDescription description="Scrollbars will always be visible when using a mouse, and only while scrolling when using a trackpad.">
<Radio value="automatic">
Automatic
</Radio>
</RadioDescription>
<RadioDescription description="Scrollbars will appear only while you are scrolling.">
<Radio value="scrolling">
While scrolling
</Radio>
</RadioDescription>
<RadioDescription description="Scrollbars will always be visible.">
<Radio value="always">
Always
</Radio>
</RadioDescription>
</MyRadioGroup>
Custom children#
RadioGroup passes props to its child components, such as the label, via their associated contexts. These contexts are exported so you can also consume them in your own custom components. This enables you to reuse existing components from your app or component library together with React Aria Components.
Component | Context | Props | Ref |
Label | LabelContext | LabelProps | HTMLLabelElement |
Text | TextContext | TextProps | HTMLElement |
This example consumes from LabelContext
in an existing styled label component to make it compatible with React Aria Components. The useContextProps
hook merges the local props and ref with the ones provided via context by RadioGroup.
import type {LabelProps} from 'react-aria-components';
import {LabelContext, useContextProps} from 'react-aria-components';
const MyCustomLabel = React.forwardRef(
(props: LabelProps, ref: React.ForwardedRef<HTMLLabelElement>) => {
// Merge the local props and ref with the ones provided via context.
[props, ref] = useContextProps(props, ref, LabelContext);
// ... your existing Label component
return <label {...props} ref={ref} />;
}
);
import type {LabelProps} from 'react-aria-components';
import {
LabelContext,
useContextProps
} from 'react-aria-components';
const MyCustomLabel = React.forwardRef(
(
props: LabelProps,
ref: React.ForwardedRef<HTMLLabelElement>
) => {
// Merge the local props and ref with the ones provided via context.
[props, ref] = useContextProps(
props,
ref,
LabelContext
);
// ... your existing Label component
return <label {...props} ref={ref} />;
}
);
import type {LabelProps} from 'react-aria-components';
import {
LabelContext,
useContextProps
} from 'react-aria-components';
const MyCustomLabel =
React.forwardRef(
(
props: LabelProps,
ref:
React.ForwardedRef<
HTMLLabelElement
>
) => {
// Merge the local props and ref with the ones provided via context.
[props, ref] =
useContextProps(
props,
ref,
LabelContext
);
// ... your existing Label component
return (
<label
{...props}
ref={ref}
/>
);
}
);
Now you can use MyCustomLabel
within a RadioGroup
, in place of the builtin React Aria Components Label
.
<RadioGroup>
<MyCustomLabel>Favorite pet</MyCustomLabel> <Radio value="dogs">Dog</Radio>
<Radio value="cats">Cat</Radio>
<Radio value="dragon">Dragon</Radio>
</RadioGroup>
<RadioGroup>
<MyCustomLabel>Favorite pet</MyCustomLabel> <Radio value="dogs">Dog</Radio>
<Radio value="cats">Cat</Radio>
<Radio value="dragon">Dragon</Radio>
</RadioGroup>
<RadioGroup>
<MyCustomLabel>
Favorite pet
</MyCustomLabel> <Radio value="dogs">
Dog
</Radio>
<Radio value="cats">
Cat
</Radio>
<Radio value="dragon">
Dragon
</Radio>
</RadioGroup>
State#
RadioGroup provides a RadioGroupState
object to its children via RadioGroupStateContext
. This can be used to access and manipulate the radio group's state.
Hooks#
If you need to customize things further, such as accessing internal state or customizing DOM structure, you can drop down to the lower level Hook-based API. See useRadioGroup for more details.