Skip to content

Material 3

Components

Switch

Switches toggle the selection of an item on and off.

9:30

Network

Wi-FiHome network
BluetoothOff
Mobile dataOn
Airplane modeOff
HotspotOff

Switches toggle the selection of an item on and off. They are best used to adjust settings and other standalone options, and their effect should start immediately, without a save. Use them to:

  • Toggle a single item on or off
  • Immediately activate or deactivate something

Compared with the other selection controls:

  • Switch or checkbox: A switch suits a setting or another standalone option that takes effect immediately. When several related options can be selected from a list, use checkboxes, which take up less space.
  • Switch or radio button: A switch controls a single option that is either on or off, not a choice between two opposing options. Use switches, and not radio buttons, when the items of a list can be controlled independently.
import { Switch } from '@material-web/react'

Basic switch

Use a Switch to toggle a single option on or off. Set defaultSelected on a switch that starts selected.

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

export default function Basic() {
  return (
    <div className="flex flex-col items-start gap-space-200 text-body-lg">
      <Switch defaultSelected>
        <span className="ps-space-200">Wi-Fi</span>
      </Switch>
      <Switch>
        <span className="ps-space-200">Bluetooth</span>
      </Switch>
    </div>
  )
}

Set isDisabled to disable the switch: it cannot be toggled or focused. Set isReadOnly so that pressing the switch does not change its selection.

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

export default function Disabled() {
  return (
    <div className="flex flex-col items-start gap-space-200 text-body-lg">
      <Switch isDisabled>
        <span className="ps-space-200">Wi-Fi</span>
      </Switch>
      <Switch isDisabled defaultSelected icons="both">
        <span className="ps-space-200">Bluetooth</span>
      </Switch>
    </div>
  )
}

Label

Write the label as the switch's children. Pressing the label toggles the switch. With no label text, set an aria-label. Keep the label short and direct: it describes what the switch does when it is on.

The switch adds no space before its label, so add it yourself, as ps-space-200 does in this example.

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

export default function WithLabel() {
  return (
    <>
      <Switch defaultSelected className="text-body-lg">
        <span className="ps-space-200">Dark mode</span>
      </Switch>
      <Switch aria-label="Airplane mode" />
    </>
  )
}

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

Icons

Set icons to choose the icons in the handle:

  • none (the default) shows no icon.
  • selected shows a check in the selected handle.
  • both also shows an X in the unselected handle, which is then 24dp instead of 16dp.

The icons are decorative and are not part of the switch's name.

noneselectedboth
import { Switch } from '@material-web/react'

export default function Icons() {
  return (
    <div className="grid grid-cols-3 items-center justify-items-center gap-space-200">
      <Switch aria-label="No icons, off" />
      <Switch icons="selected" aria-label="Selected icon, off" />
      <Switch icons="both" aria-label="Both icons, off" />
      <Switch defaultSelected aria-label="No icons, on" />
      <Switch icons="selected" defaultSelected aria-label="Selected icon, on" />
      <Switch icons="both" defaultSelected aria-label="Both icons, on" />
      <span className="text-label-md text-on-surface-variant">none</span>
      <span className="text-label-md text-on-surface-variant">selected</span>
      <span className="text-label-md text-on-surface-variant">both</span>
    </div>
  )
}

Controlled

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

In this example, the switch is controlled, and its children are a function of its state that renders the supporting text for that state.

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

export default function Controlled() {
  const [isOn, setOn] = useState(true)
  return (
    <Switch icons="selected" isSelected={isOn} onChange={setOn} className="w-72">
      {({ isSelected }) => (
        <span className="flex flex-col ps-space-200">
          <span className="text-body-lg">Wi-Fi</span>
          <span className="text-body-md text-on-surface-variant">
            {isSelected ? 'Public Campus Wi-Fi' : 'Off'}
          </span>
        </span>
      )}
    </Switch>
  )
}

Forms

Set a name to include a switch in a form. It is submitted with the form and reset with it. A selected switch submits its value, or on with no value, and an unselected switch submits nothing.

Nothing sent yet
import { Button, Switch } from '@material-web/react'
import { useState } from 'react'

export default function Form() {
  const [sent, setSent] = useState('Nothing sent yet')
  return (
    <form
      className="flex flex-col items-start gap-space-200 text-body-lg"
      onSubmit={(event) => {
        event.preventDefault()
        setSent(JSON.stringify(Object.fromEntries(new FormData(event.currentTarget))))
      }}
    >
      <Switch name="newsletter" value="weekly" defaultSelected>
        <span className="ps-space-200">Weekly newsletter</span>
      </Switch>
      <div className="flex gap-space-100">
        <Button type="reset" variant="text">
          Reset
        </Button>
        <Button type="submit">Save</Button>
      </div>
      <output className="text-body-sm text-on-surface-variant">{sent}</output>
    </form>
  )
}

Set isRequired to require the switch to be selected. A required switch blocks its form from being submitted until then. See React Aria's Forms guide.

Note: isInvalid does not change how the switch looks.

In a list

In a list, a Switch in an item's slot does not select the item, as a Checkbox does. It is a separate control, so give it its own name and state.

Wi-Fi
Bluetooth
Airplane mode
import { List, ListItem, Switch } from '@material-web/react'
import { Text } from 'react-aria-components'

const settings = ['Wi-Fi', 'Bluetooth', 'Airplane mode']

export default function InList() {
  return (
    <List aria-label="Settings" className="w-90">
      {settings.map((name) => (
        <ListItem key={name} id={name} textValue={name}>
          <Text slot="label">{name}</Text>
          <Switch slot="trailing" aria-label={name} defaultSelected={name === 'Wi-Fi'} />
        </ListItem>
      ))}
    </List>
  )
}

Customization

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

export default function Custom() {
  return (
    <div className="flex flex-col items-start gap-space-100 text-body-lg">
      <Switch className="rounded-shape-full p-space-100 pe-space-300 has-hover:bg-surface-container">
        <span className="ps-space-200">Highlighted on hover</span>
      </Switch>
      <Switch
        defaultSelected
        className="rounded-shape-full p-space-100 pe-space-300 selected:bg-secondary-container"
      >
        <span className="ps-space-200">Tinted when selected</span>
      </Switch>
    </div>
  )
}
  • Color: className does not reach the track and the handle. To change their colors, set the color roles they use. See Theme.
  • States: On a switch, 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, isRequired and state. To read hover, press and focus, pass a function as children.
  • Every switch: To restyle all the switches at once, change the theme.

Children as a function

Pass a function as children to render the label from the switch's state: isSelected, isHovered, isPressed, isFocused, isFocusVisible, isDisabled, isReadOnly, isInvalid, isRequired and state. isFocused is any focus, and isFocusVisible is keyboard focus only. The state's type is exported as SwitchRenderProps. The controlled example uses one. See Children as a function.