Skip to content

Switch

Switches toggle the state of a single setting on or off.

Switches are the preferred way to adjust settings on mobile. The option that the switch controls, as well as the state it's in, should be made clear from the corresponding inline label.

Basic switches

<Switch defaultChecked inputProps={{ 'aria-label': 'checked' }} />
<Switch inputProps={{ 'aria-label': 'unchecked' }} />
<Switch
  disabled
  defaultChecked
  inputProps={{ 'aria-label': 'disabled checked' }}
/>
<Switch disabled inputProps={{ 'aria-label': 'disabled unchecked' }} />

Label

You can provide a label to the Switch thanks to the FormControlLabel component.

<FormGroup>
  <FormControlLabel control={<Switch defaultChecked />} label="Label" />
  <FormControlLabel disabled control={<Switch />} label="Disabled" />
</FormGroup>

Size

Use the size prop to change the size of the switch.

<Switch defaultChecked size="small" inputProps={{ 'aria-label': 'small' }} />
<Switch defaultChecked inputProps={{ 'aria-label': 'normal' }} />

Color

<Switch defaultChecked inputProps={{ 'aria-label': 'secondary' }} />
<Switch
  defaultChecked
  color="primary"
  inputProps={{ 'aria-label': 'primary' }}
/>
<Switch
  defaultChecked
  color="default"
  inputProps={{ 'aria-label': 'default' }}
/>
<GreenSwitch defaultChecked inputProps={{ 'aria-label': 'custom' }} />

Controlled

You can control the switch with the value and onChange props:

<Switch
  checked={checked}
  onChange={handleChange}
  inputProps={{ 'aria-label': 'controlled' }}
/>

Switches with FormGroup

FormGroup is a helpful wrapper used to group selection controls components that provides an easier API. However, you are encouraged to use Checkboxes instead if multiple related controls are required. (See: When to use).

Assign responsibility

Be careful

Customized switches

Here are some examples of customizing the component. You can learn more about this in the overrides documentation page.

Off

On

<FormGroup>
  <FormControlLabel control={<IOSSwitch defaultChecked />} label="iOS style" />
  <Stack direction="row" spacing={1} alignItems="center">
    <Typography>Off</Typography>
    <AntSwitch defaultChecked inputProps={{ 'aria-label': 'ant design' }} />
    <Typography>On</Typography>
  </Stack>
</FormGroup>

🎨 If you are looking for inspiration, you can check MUI Treasury's customization examples.

Label placement

You can change the placement of the label:

Label placement

When to use

Accessibility

  • It will render an element with the checkbox role not switch role since this role isn't widely supported yet. Please test first if assistive technology of your target audience supports this role properly. Then you can change the role with <Switch inputProps={{ role: 'switch' }}>
  • All form controls should have labels, and this includes radio buttons, checkboxes, and switches. In most cases, this is done by using the <label> element (FormControlLabel).
  • When a label can't be used, it's necessary to add an attribute directly to the input component. In this case, you can apply the additional attribute (e.g. aria-label, aria-labelledby, title) via the inputProps prop.
<Switch value="checkedA" inputProps={{ 'aria-label': 'Switch A' }} />