Delivery
Delivery speed
Radio buttons let people select one option from a set of options. Only one radio button of a set can be selected at a time. Use them to:
- Select a single option from a set
- Expose all available options
Compared with the other selection controls:
- Radio button or checkbox: Radio buttons are single-select. To let people select several options from a list, use checkboxes.
- Radio button or switch: To let people turn the items of a list on or off independently, use switches.
A radio group is composed of these parts:
| Part | Use |
|---|---|
RadioGroup | The group, which holds the other parts |
Label | The group's name. Without one, name the group with an aria-label. |
Radio | An option, with its value |
Text with slot="description" | The group's description |
FieldError | The group's error, displayed once a form submission is blocked. See Validation. |
import { Radio, RadioGroup } from '@material-web/react'
import { FieldError, Label, Text } from 'react-aria-components'Basic radio button
Put radio buttons in a RadioGroup and give each a value. Selecting one deselects the others. Set the initial selection with defaultValue on the group: the group's value is the value of the selected Radio.
import { Radio, RadioGroup } from '@material-web/react'
export default function Basic() {
return (
<RadioGroup
aria-label="Ship to"
defaultValue="home"
className="flex flex-col items-start text-body-lg"
>
<Radio value="home">Home</Radio>
<Radio value="office">Office</Radio>
<Radio value="other">Other</Radio>
</RadioGroup>
)
}The group has no layout of its own, so give it one, such as a column.
import { Radio, RadioGroup } from '@material-web/react'
import { Label, Text } from 'react-aria-components'
export default function Group() {
return (
<RadioGroup defaultValue="home" className="flex flex-col items-start text-body-lg">
<Label className="ps-space-125 text-label-lg text-on-surface-variant">Ship to</Label>
<Radio value="home">Home</Radio>
<Radio value="office">Office</Radio>
<Radio value="other">Other</Radio>
<Text slot="description" className="ps-space-125 text-body-sm text-on-surface-variant">
Weekdays only
</Text>
</RadioGroup>
)
}Note: A Radio outside a RadioGroup throws an error.
Slots
- description
| Slot | Child | Use |
|---|---|---|
description | A Text | Describes the group and each radio button in it |
Label
Write the label as the radio button's children. Pressing the label selects the radio button. With no label text, set an aria-label.
import { Radio, RadioGroup } from '@material-web/react'
export default function WithLabel() {
return (
<>
<RadioGroup aria-label="Size" defaultValue="m" className="flex gap-space-100 text-body-lg">
<Radio value="s">Small</Radio>
<Radio value="m">Medium</Radio>
<Radio value="l">Large</Radio>
</RadioGroup>
<RadioGroup aria-label="Rating" defaultValue="2" className="flex">
<Radio value="1" aria-label="1 star" />
<Radio value="2" aria-label="2 stars" />
<Radio value="3" aria-label="3 stars" />
</RadioGroup>
</>
)
}The label takes the type style of the text around it. To set one, add its class to the radio button, to the group or to an element around them, as in text-body-lg.
Controlled
Control the selection with value and onChange on the group, as with React Aria's RadioGroup. onChange is called with the value of the newly selected Radio.
Pressing a selected radio button does not deselect it. To clear the selection, set the group's value to null, as the button in this example does.
import { Button, Radio, RadioGroup } from '@material-web/react'
import { useState } from 'react'
import { Label } from 'react-aria-components'
export default function Controlled() {
const [address, setAddress] = useState<string | null>('home')
return (
<RadioGroup
value={address}
onChange={setAddress}
className="flex flex-col items-start text-body-lg"
>
<Label className="ps-space-125 text-label-lg text-on-surface-variant">
Ship to: {address ?? 'nowhere yet'}
</Label>
<Radio value="home">Home</Radio>
<Radio value="office">Office</Radio>
<Radio value="other">Other</Radio>
<Button variant="text" isDisabled={address === null} onPress={() => setAddress(null)}>
Clear selection
</Button>
</RadioGroup>
)
}Disabled
Set isDisabled on the group to disable every radio button in it, or on a Radio to disable that one alone. A disabled radio button cannot be selected or focused, and Tab and the arrow keys skip it. Set isReadOnly on the group to keep its selection as it is while it can still be focused.
import { Radio, RadioGroup } from '@material-web/react'
export default function Disabled() {
return (
<>
<RadioGroup
aria-label="Ship to"
defaultValue="office"
isDisabled
className="flex flex-col items-start text-body-lg"
>
<Radio value="home">Home</Radio>
<Radio value="office">Office</Radio>
</RadioGroup>
<RadioGroup
aria-label="Delivery"
defaultValue="standard"
className="flex flex-col items-start text-body-lg"
>
<Radio value="standard">Standard</Radio>
<Radio value="express" isDisabled>
Express
</Radio>
</RadioGroup>
</>
)
}Orientation
Set orientation to vertical (the default) or horizontal. It tells assistive technology which way the group runs, and in a right-to-left locale it makes the left and right arrow keys of a horizontal group follow the row. It does not set the layout, so give a horizontal group a row.
import { Radio, RadioGroup } from '@material-web/react'
import { Label } from 'react-aria-components'
export default function Orientation() {
return (
<RadioGroup
orientation="horizontal"
defaultValue="standard"
className="flex flex-wrap items-center gap-x-space-100 text-body-lg"
>
<Label className="pe-space-100 text-label-lg text-on-surface-variant">Delivery</Label>
<Radio value="standard">Standard</Radio>
<Radio value="express">Express</Radio>
<Radio value="pickup">Pickup</Radio>
</RadioGroup>
)
}Validation
Set isRequired on the group to require a selection. A required group blocks its form from being submitted while no radio button is selected. Once a submission is blocked, a FieldError in the group is displayed until one is selected. See React Aria's Forms guide.
import { Button, Radio, RadioGroup } from '@material-web/react'
import { FieldError, Label } from 'react-aria-components'
export default function Required() {
return (
<form
onSubmit={(event) => event.preventDefault()}
className="flex flex-col items-start gap-space-100 text-body-lg"
>
<RadioGroup name="delivery" isRequired className="flex flex-col items-start">
<Label className="ps-space-125 text-label-lg text-on-surface-variant">Delivery</Label>
<Radio value="standard">Standard</Radio>
<Radio value="express">Express</Radio>
<Radio value="pickup">Pickup</Radio>
<FieldError className="ps-space-125 text-body-sm text-error">Choose a delivery</FieldError>
</RadioGroup>
<Button type="submit" className="ms-space-125">
Order
</Button>
</form>
)
}To mark a group as invalid yourself, set isInvalid on it. The group's isRequired and isInvalid apply to every radio button in it. An invalid radio button keeps its colors, so show the error in a FieldError.
Forms
Set a name on the group to include it in a form. The selected value is submitted with the form, and the selection is reset with it. A group with no selection submits nothing.
import { Button, Radio, RadioGroup } from '@material-web/react'
import { useState } from 'react'
import { Label } from 'react-aria-components'
export default function Form() {
const [sent, setSent] = useState('Nothing sent yet')
return (
<form
className="flex flex-col items-start gap-space-100 text-body-lg"
onSubmit={(event) => {
event.preventDefault()
setSent(JSON.stringify(Object.fromEntries(new FormData(event.currentTarget))))
}}
>
<RadioGroup name="delivery" defaultValue="standard" className="flex flex-col items-start">
<Label className="ps-space-125 text-label-lg text-on-surface-variant">Delivery</Label>
<Radio value="standard">Standard</Radio>
<Radio value="express">Express</Radio>
<Radio value="pickup">Pickup</Radio>
</RadioGroup>
<div className="flex gap-space-100">
<Button type="reset" variant="text">
Reset
</Button>
<Button type="submit">Send</Button>
</div>
<output className="ps-space-125 text-body-sm text-on-surface-variant">{sent}</output>
</form>
)
}In a list
In a selection list, a Radio in an item's slot does not select the item, as a Checkbox does. It is a separate control, and still needs a RadioGroup around it.
Customization
Pass classes in className to restyle one radio button. They go on its outermost element, an inline flex box aligned to the middle of its line, as do style and ref. inputRef is the ref of its input. See Styling for the classes, the states and the theme.
import { Radio, RadioGroup } from '@material-web/react'
const sizes = ['A4', 'A5', 'Letter']
export default function Custom() {
return (
<RadioGroup
aria-label="Paper size"
defaultValue="A4"
className="flex flex-col items-start gap-space-100 text-body-lg"
>
{sizes.map((size) => (
<Radio
key={size}
value={size}
className="rounded-shape-full pe-space-200 has-hover:bg-surface-container selected:bg-secondary-container"
>
{size}
</Radio>
))}
</RadioGroup>
)
}- Color:
classNamedoes not reach the circles. To change their colors, set the color roles they use. See Theme. - States: On a radio button, the variants
selected:,invalid:,disabled:,read-only:andrequired:style those states. Hover, press and focus are reported on an element inside it, so use theirhas-*form, as inhas-hover:bg-surface-container. See States. - Functions:
classNameandstylecan be functions ofisSelected,isInvalid,isDisabled,isReadOnlyandisRequired. To read hover, press and focus, pass a function as children. - Every radio button: To restyle all the radio buttons at once, change the theme.
Children as a function
Pass a function as children to render the label from the radio button's state: isSelected, isHovered, isPressed, isFocused, isFocusVisible, isDisabled, isReadOnly, isInvalid and isRequired. isFocused is any focus, and isFocusVisible is keyboard focus only. The state's type is exported as RadioRenderProps. See Children as a function.
import { Radio, RadioGroup } from '@material-web/react'
const plans = [
{ value: 'monthly', name: 'Monthly', price: '$12 a month' },
{ value: 'yearly', name: 'Yearly', price: '$120 a year' },
]
export default function RenderProps() {
return (
<RadioGroup aria-label="Plan" defaultValue="monthly" className="flex flex-col items-start">
{plans.map(({ value, name, price }) => (
<Radio key={value} value={value}>
{({ isSelected }) => (
<span className="flex flex-col py-space-100">
<span className="text-body-lg">{name}</span>
<span
className={
isSelected ? 'text-body-md text-primary' : 'text-body-md text-on-surface-variant'
}
>
{price}
</span>
</span>
)}
</Radio>
))}
</RadioGroup>
)
}On a RadioGroup, className, style and children can be functions of isDisabled, isReadOnly, isRequired, isInvalid, orientation and state.
Use cases
Material lists what people should be able to do with assistive technology:
- Navigate to a radio button
- Select a radio button
- Get appropriate feedback based on input type
Interaction and style
- States: Hovering shows a state layer, and a press shows a ripple. Keyboard focus shows a focus ring, and a press with a mouse or touch does not.
- Touch target: A radio button has a touch target of 48dp around its circle. Pressing its label selects it too.
- Color contrast: In the light and dark schemes, at every contrast level, the circle and its dot keep 3:1 contrast with the surface. Keep that contrast when you change the color roles they use.
- Density: Material advises against density that brings a radio button's target under 48dp. A radio button does not follow the density classes: it keeps its size and its 48dp target in a denser layout.
- Motion: With reduced motion, the circle changes color and the dot appears instantly.
- Forced colors: In forced colors, the circle and its dot are drawn in the system's text color, and a disabled radio button and its label are grayed out.
- Deselecting: Pressing a selected radio button does not deselect it. Material advises giving people a way to opt out: an option such as Not applicable, or a separate control such as Clear selection, which sets the group's
valuetonull, as in the Controlled example.
Focus
- The group is one tab stop. Tab moves the focus to the selected radio button, as Material advises. With none selected, Tab moves it to the first radio button, and Shift+Tab to the last.
- A disabled radio button cannot be focused: Tab and the arrow keys skip it. A read-only group can still be focused.
- Set
autoFocuson aRadioto move the focus to it when it is first rendered.
Keyboard navigation
| Keys | Actions |
|---|---|
| Tab | Move the focus into the group, to the selected radio button, or to the first with none selected |
| Shift+Tab | Move the focus into the group, to the selected radio button, or to the last with none selected |
| Arrow keys | Move the focus and the selection to the previous or next radio button, wrapping around the group |
| Space or Enter | Select the focused radio button |
- In a right-to-left locale, the Left and Right arrow keys of a
horizontalgroup follow the row. See Orientation.
Labeling elements
- The group has the radiogroup role. Name it with a
Labelor anaria-label, which Material advises be the same as the title of the options. ATextin itsdescriptionslot describes the group and each radio button in it. - Each radio button has the radio role, and its label names it. With no label, set an
aria-label. - The group reports its
orientation, andisDisabled,isReadOnly,isRequiredandisInvalid, to assistive technology.
Radio
Props of Radio (26)
classNameClassNameOrFunction<RadioFieldRenderProps>Default'react-aria-RadioField'The CSS className for the element. A function may be provided to compute the class based on component state.
inputRefRef<HTMLInputElement | null>A ref for the HTML input element.
idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
onClick(e: MouseEvent<FocusableElement>) => voidNot recommended – use
onPressinstead.onClickis an alias foronPressprovided for compatibility with other libraries.onPressprovides additional event details for non-mouse interactions.isDisabledbooleanWhether the radio button is disabled or not. Shows that a selection exists, but is not available in that circumstance.
onPress(e: PressEvent) => voidHandler that is called when the press is released over the target.
onPressStart(e: PressEvent) => voidHandler that is called when a press interaction starts.
onPressEnd(e: PressEvent) => voidHandler that is called when a press interaction ends, either over the target or when the pointer leaves the target.
onPressChange(isPressed: boolean) => voidHandler that is called when the press state changes.
onPressUp(e: PressEvent) => voidHandler that is called when a press is released over the target, regardless of whether it started on the target or not.
autoFocusbooleanWhether the element should receive focus on render.
onFocus(e: FocusEvent<Target>) => voidHandler that is called when the element receives focus.
onBlur(e: FocusEvent<Target>) => voidHandler that is called when the element loses focus.
onFocusChange(isFocused: boolean) => voidHandler that is called when the element's focus status changes.
onKeyDown(e: KeyboardEvent) => voidHandler that is called when a key is pressed.
onKeyUp(e: KeyboardEvent) => voidHandler that is called when a key is released.
valuerequiredstringThe value of the radio button, used when submitting an HTML form. See MDN.
styleCSSProperties | (values: RadioFieldRenderProps & { defaultStyle: CSSProperties; }) => CSSProperties | undefinedThe inline style for the element. A function may be provided to compute the style based on component state.
renderDOMRenderFunction<'div', RadioFieldRenderProps>Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.
Requirements:
- You must render the expected element type (e.g. if
<button>is expected, you cannot render an<a>). - Only a single root DOM element can be rendered (no fragments).
- You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
- You must render the expected element type (e.g. if
slotstring | nullA slot name for the component. Slots allow the component to receive props from a parent component. An explicit
nullvalue indicates that the local props completely override all props received from a parent.childrenChildrenOrFunction<T>The children of the component. A function may be provided to alter the children based on component state.
refRef<HTMLDivElement>
Radio also takes the 69 attributes and events of the element it renders.
RadioGroup
Props of RadioGroup (27)
classNameClassNameOrFunction<RadioGroupRenderProps>Default'react-aria-RadioGroup'The CSS className for the element. A function may be provided to compute the class based on component state.
formstringThe
<form>element to associate the input with. The value of this attribute must be the id of a<form>in the same document. See MDN.idstringThe element's unique identifier. See MDN.
aria-labelstringDefines a string value that labels the current element.
aria-labelledbystringIdentifies the element (or elements) that labels the current element.
aria-describedbystringIdentifies the element (or elements) that describes the object.
aria-detailsstringIdentifies the element (or elements) that provide a detailed, extended description for the object.
isDisabledbooleanWhether the input is disabled.
onFocus(e: FocusEvent<Target>) => voidHandler that is called when the element receives focus.
onBlur(e: FocusEvent<Target>) => voidHandler that is called when the element loses focus.
onFocusChange(isFocused: boolean) => voidHandler that is called when the element's focus status changes.
namestringThe name of the input element, used when submitting an HTML form. See MDN.
valuenull | stringThe current value (controlled).
onChange(value: string) => voidHandler that is called when the value changes.
isReadOnlybooleanWhether the input can be selected but not changed by the user.
isInvalidbooleanWhether the input value is invalid.
defaultValuenull | stringThe default value (uncontrolled).
isRequiredbooleanWhether user input is required on the input before form submission.
validate(value: string) => true | ValidationError | null | undefinedA function that returns an error message if a given value is invalid. Validation errors are displayed to the user when the form is submitted if
validationBehavior="native". For realtime validation, use theisInvalidprop instead.aria-errormessagestringIdentifies the element that provides an error message for the object.
orientation'horizontal' | 'vertical'Default'vertical'The axis the Radio Button(s) should align with.
validationBehavior'native' | 'aria'Default'native'Whether to use native HTML form validation to prevent form submission when the value is missing or invalid, or mark the field as required or invalid via ARIA.
childrenChildrenOrFunction<T>The children of the component. A function may be provided to alter the children based on component state.
styleCSSProperties | (values: RadioGroupRenderProps & { defaultStyle: CSSProperties; }) => CSSProperties | undefinedThe inline style for the element. A function may be provided to compute the style based on component state.
renderDOMRenderFunction<'div', RadioGroupRenderProps>Overrides the default DOM element with a custom render function. This allows rendering existing components with built-in styles and behaviors such as router links, animation libraries, and pre-styled components.
Requirements:
- You must render the expected element type (e.g. if
<button>is expected, you cannot render an<a>). - Only a single root DOM element can be rendered (no fragments).
- You must pass through props and ref to the underlying DOM element, merging with your own prop as appropriate.
- You must render the expected element type (e.g. if
slotstring | nullA slot name for the component. Slots allow the component to receive props from a parent component. An explicit
nullvalue indicates that the local props completely override all props received from a parent.refRef<HTMLDivElement>
RadioGroup also takes the 70 attributes and events of the element it renders.