Skip to content

Material 3

Components

Radio button

Radio buttons let people select one option from a set of options.

9:30

Delivery

Ship to
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:

PartUse
RadioGroupThe group, which holds the other parts
LabelThe group's name. Without one, name the group with an aria-label.
RadioAn option, with its value
Text with slot="description"The group's description
FieldErrorThe 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.

Ship to
Weekdays only
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

  1. description
SlotChildUse
descriptionA TextDescribes 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.

Ship to: home
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.

Delivery
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.

Delivery
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.

Delivery
Nothing sent yet
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: className does 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: and required: style those states. Hover, press and focus are reported on an element inside it, so use their has-* form, as in has-hover:bg-surface-container. See States.
  • Functions: className and style can be functions of isSelected, isInvalid, isDisabled, isReadOnly and isRequired. 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.