Skip to content

Material 3

Components

Checkbox

Checkboxes let users select one or more items from a list, or turn an item on or off.

9:30

Maps

Allow access
Microphone
Location
Contacts
Camera
Notifications
Photos and videos
Nearby devices

Checkboxes let people select one or more items from a list, or turn an item on or off. Use them to:

  • Select one or more options from a list
  • Present a list that contains sub-selections
  • Turn an item on or off in a desktop environment
  • Group similar options together

Compared with the other selection controls:

  • Checkbox or switch: Use checkboxes in place of switches when several related options can be selected from a list. Checkboxes group similar items together and take up less space than switches.
  • Checkbox or radio button: Checkboxes are multi-select. To select a single option from a set, use radio buttons.

Use a Checkbox on its own, or several in a group, which is composed of these parts:

PartUse
CheckboxGroupThe group, which holds the other parts
LabelThe group's name. Without one, name the group with an aria-label.
CheckboxA checkbox. In a group, an option, with its value.
Text with slot="description"The group's description
FieldErrorThe group's error, displayed once a form submission is blocked. See Forms.
import { Checkbox, CheckboxGroup } from '@material-web/react'
import { FieldError, Label, Text } from 'react-aria-components'

Basic checkbox

Use a Checkbox on its own, or put several in a CheckboxGroup to select from a list. Set defaultSelected on a checkbox that starts selected.

import { Checkbox } from '@material-web/react'

export default function Basic() {
  return (
    <div className="flex flex-col items-start text-body-lg">
      <Checkbox defaultSelected>Pickles</Checkbox>
      <Checkbox>Tomato</Checkbox>
      <Checkbox>Lettuce</Checkbox>
    </div>
  )
}

Set isDisabled to disable the checkbox: it cannot be selected or focused. Set isReadOnly to keep its selection as it is while it can still be focused.

import { Checkbox } from '@material-web/react'

export default function Disabled() {
  return (
    <div className="flex flex-col items-start text-body-lg">
      <Checkbox isDisabled>Pickles</Checkbox>
      <Checkbox isDisabled defaultSelected>
        Tomato
      </Checkbox>
      <Checkbox isDisabled isIndeterminate>
        Lettuce
      </Checkbox>
    </div>
  )
}

Label

Write the label as the checkbox's children. Pressing the label toggles the checkbox. With no label text, set an aria-label.

import { Checkbox } from '@material-web/react'

export default function WithLabel() {
  return (
    <>
      <Checkbox defaultSelected className="text-body-lg">
        Remember me
      </Checkbox>
      <Checkbox aria-label="Select all messages" />
    </>
  )
}

The label takes the type style of the text around it. To set one, add its class to the checkbox or to an element around it, as in text-body-lg.

Checkbox group

Put checkboxes in a CheckboxGroup and give each a value. The group's value is the list of the selected values, as with React Aria's CheckboxGroup.

The group has no layout of its own, so give it one, such as a column.

Extras
Free of charge
import { Checkbox, CheckboxGroup } from '@material-web/react'
import { Label, Text } from 'react-aria-components'

export default function Group() {
  return (
    <CheckboxGroup
      name="extras"
      defaultValue={['pickles', 'cheese']}
      className="flex flex-col items-start text-body-lg"
    >
      <Label className="ps-space-150 text-label-lg text-on-surface-variant">Extras</Label>
      <Checkbox value="pickles">Pickles</Checkbox>
      <Checkbox value="tomato">Tomato</Checkbox>
      <Checkbox value="lettuce">Lettuce</Checkbox>
      <Checkbox value="cheese">Cheese</Checkbox>
      <Text slot="description" className="ps-space-150 text-body-sm text-on-surface-variant">
        Free of charge
      </Text>
    </CheckboxGroup>
  )
}

Set the initial selection with defaultValue, or control it with value and onChange. In a group, a checkbox's own isSelected and defaultSelected do not change the group's value.

With tomato
import { Checkbox, CheckboxGroup } from '@material-web/react'
import { useState } from 'react'

export default function GroupControlled() {
  const [extras, setExtras] = useState(['tomato'])
  return (
    <div className="flex flex-col items-start text-body-lg">
      <CheckboxGroup
        aria-label="Extras"
        value={extras}
        onChange={setExtras}
        className="flex flex-col items-start"
      >
        <Checkbox value="pickles">Pickles</Checkbox>
        <Checkbox value="tomato">Tomato</Checkbox>
        <Checkbox value="lettuce">Lettuce</Checkbox>
      </CheckboxGroup>
      <span className="ps-space-150 text-body-md text-on-surface-variant">
        {extras.length > 0 ? `With ${extras.join(', ')}` : 'No extras'}
      </span>
    </div>
  )
}

The group's other props apply to every checkbox in it:

  • isDisabled and isReadOnly disable each checkbox or make it read-only.
  • isInvalid shows each checkbox in the error color.
  • isRequired requires at least one checkbox to be selected.
  • name submits each selected value under that name.

Slots

  1. description
SlotChildUse
descriptionA TextDescribes the group and each checkbox in it

Controlled

Control the selection with isSelected and onChange, which is called with the new selection. Without them, the checkbox keeps its own selection, starting from defaultSelected.

A receipt will be sent
import { Checkbox } from '@material-web/react'
import { useState } from 'react'

export default function Controlled() {
  const [isSelected, setSelected] = useState(true)
  return (
    <div className="flex flex-col items-start text-body-lg">
      <Checkbox isSelected={isSelected} onChange={setSelected}>
        Email me the receipt
      </Checkbox>
      <span className="ps-space-500 text-body-md text-on-surface-variant">
        {isSelected ? 'A receipt will be sent' : 'No receipt will be sent'}
      </span>
    </div>
  )
}

Indeterminate

Set isIndeterminate to show a dash in place of the check. A press does not clear it, so clear it yourself in onChange.

In this example, a parent checkbox is indeterminate while only some of its children are selected. Selecting the parent selects every child, and unselecting it unselects them all.

import { Checkbox } from '@material-web/react'
import { useState } from 'react'

const extras = ['Pickles', 'Tomato', 'Lettuce']

export default function Indeterminate() {
  const [selected, setSelected] = useState(['Tomato'])
  const isAll = selected.length === extras.length
  return (
    <div className="flex flex-col items-start text-body-lg">
      <Checkbox
        isSelected={isAll}
        isIndeterminate={selected.length > 0 && !isAll}
        onChange={(isSelected) => setSelected(isSelected ? extras : [])}
      >
        Additions
      </Checkbox>
      {extras.map((name) => (
        <Checkbox
          key={name}
          className="ps-space-400"
          isSelected={selected.includes(name)}
          onChange={(isSelected) =>
            setSelected(isSelected ? [...selected, name] : selected.filter((it) => it !== name))
          }
        >
          {name}
        </Checkbox>
      ))}
    </div>
  )
}

Note: isIndeterminate changes how the checkbox is shown and announced. In a form, the checkbox is still submitted by its selection.

Validation

Set isInvalid to mark the checkbox as invalid and show it in the error color. To show the error of a field that a form requires, see Forms.

import { Checkbox } from '@material-web/react'

export default function Invalid() {
  return (
    <>
      <Checkbox isInvalid aria-label="Unselected" />
      <Checkbox isInvalid defaultSelected aria-label="Selected" />
      <Checkbox isInvalid isIndeterminate aria-label="Indeterminate" />
    </>
  )
}

Forms

Set a name to include a checkbox or a group in a form. It is submitted with the form and reset with it.

FieldSubmits
A Checkbox with a nameIts value when selected, or on with no value. Nothing when unselected.
A CheckboxGroup with a nameThe value of each selected checkbox, under the group's name.

Set isRequired to require a single checkbox to be selected, or a group to have at least one checkbox selected. A required field blocks its form from being submitted until then. Once a submission is blocked, each checkbox shows the error color, and a FieldError in the group is displayed, until one is selected. See React Aria's Forms guide.

Extras
import { Button, Checkbox, CheckboxGroup } 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"
    >
      <CheckboxGroup name="extras" isRequired className="flex flex-col items-start">
        <Label className="ps-space-150 text-label-lg text-on-surface-variant">Extras</Label>
        <Checkbox value="pickles">Pickles</Checkbox>
        <Checkbox value="tomato">Tomato</Checkbox>
        <Checkbox value="lettuce">Lettuce</Checkbox>
        <FieldError className="ps-space-150 text-body-sm text-error">Pick at least one</FieldError>
      </CheckboxGroup>
      <Button type="submit" className="ms-space-150">
        Order
      </Button>
    </form>
  )
}

Note: A reset from code, with form.reset(), that unselects several checkboxes of a group unselects only the last of them. A reset button resets them all.

In a list

In a selection list, put a Checkbox in an item's selection, leading or trailing slot to make it the item's selection checkbox. It needs no label.

Pickles
Tomato
Lettuce
import { Checkbox, List, ListItem } from '@material-web/react'
import { Text } from 'react-aria-components'

const extras = ['Pickles', 'Tomato', 'Lettuce']

export default function InList() {
  return (
    <List
      aria-label="Extras"
      selectionMode="multiple"
      defaultSelectedKeys={['Pickles']}
      className="w-90"
    >
      {extras.map((name) => (
        <ListItem key={name} id={name} textValue={name}>
          <Text slot="label">{name}</Text>
          <Checkbox slot="trailing" />
        </ListItem>
      ))}
    </List>
  )
}

Customization

Pass classes in className to restyle one checkbox. 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 { Checkbox } from '@material-web/react'

export default function Custom() {
  return (
    <div className="flex flex-col items-start gap-space-100 text-body-lg">
      <Checkbox className="rounded-shape-full pe-space-200 has-hover:bg-surface-container">
        Highlighted on hover
      </Checkbox>
      <Checkbox
        defaultSelected
        className="rounded-shape-full pe-space-200 selected:bg-secondary-container"
      >
        Tinted when selected
      </Checkbox>
    </div>
  )
}
  • Color: className does not reach the box. To change its colors, set the color roles it uses. See Theme.
  • States: On a checkbox, the variants selected:, indeterminate:, 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, isIndeterminate, isInvalid, isDisabled, isReadOnly and isRequired. To read hover, press and focus, pass a function as children.
  • Every checkbox: To restyle all the checkboxes at once, change the theme.

Children as a function

Pass a function as children to render the label from the checkbox's state: isSelected, isIndeterminate, 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 CheckboxRenderProps. See Children as a function.

import { Checkbox } from '@material-web/react'

export default function RenderProps() {
  return (
    <Checkbox className="text-body-lg">
      {({ isSelected }) => (isSelected ? 'Notifications are on' : 'Notifications are off')}
    </Checkbox>
  )
}

On a CheckboxGroup, className, style and children can be functions of isDisabled, isReadOnly, isRequired, isInvalid and state, typed as CheckboxGroupRenderProps.