Search
MaterialComponentsDocs
Components overviewApp barBadgeButtonsButton groupButtonExtended FABFAB menuFABIcon buttonSplit buttonCardCarouselCheckboxChipsDate & time pickersDate pickerTime pickerDialogDividerListLoading & progressLoading indicatorProgressMenuNavigationNavigation barNavigation drawerNavigation railRadio buttonsSearchSelectSheetsBottom sheetSide sheetSliderSnackbarSwitchTabsText fieldToolbarTooltip

Switch

Turn a setting on or off. Try labels, supporting text, and interactive states.

Notifications
FactoryWeb ComponentsReactVueSvelteSolidJS
PublishedUpdated

A switch turns one setting on or off, with an immediate effect: Wi-Fi, notifications, dark theme. When the choice is saved later with a form, use a checkbox. Pair every switch with a short label that says what it controls when on.

Usage

Vanilla
import { createSwitch } from 'material';

const toggle = createSwitch({ label: 'Wi-Fi', checked: true });
toggle.on('change', ({ checked }) => setWifi(checked));
document.body.append(toggle.element);
Web Components
<m-switch checked>Wi-Fi</m-switch>

<script type="module">
  const switchElement = document.querySelector('m-switch');
  switchElement.addEventListener('change', (event) => setWifi(event.detail.checked));
</script>
React
import { useState } from 'react';
import { Switch } from 'material/react';
import { setWifi } from './app';

export function Example() {
  const [checked, setChecked] = useState(true);
  return (
    <Switch checked={checked} onChange={(event) => { setChecked(event.detail.checked); setWifi(event.detail.checked); }}>Wi-Fi</Switch>
  );
}
Vue
<script setup lang="ts">
import { ref } from 'vue';
import { MSwitch } from 'material/vue';
import { setWifi } from './app';

const checked = ref(true);
</script>

<template>
  <MSwitch v-model="checked" @change="setWifi($event.detail.checked)">Wi-Fi</MSwitch>
</template>
Svelte
<script lang="ts">
  import { Switch } from 'material/svelte';
  import { setWifi } from './app';

  let checked = $state(true);
</script>

<Switch bind:checked onchange={(event) => setWifi(event.detail.checked)}>Wi-Fi</Switch>
SolidJS
import { createSignal } from 'solid-js';
import { Switch } from 'material/solid';
import { setWifi } from './app';

export function Example() {
  const [checked, setChecked] = createSignal(true);
  return (
    <Switch checked={checked()} onChange={(event) => { setChecked(event.detail.checked); setWifi(event.detail.checked); }}>Wi-Fi</Switch>
  );
}

Examples

Icons

M3 has three configurations: a check in the handle when on (the default), no icons (icon: 'none'), and icons in both states, where unselectedIcon also grows the off handle to 24dp. The web component takes icon, not unselectedIcon yet.

Vanilla
import { createSwitch } from 'material';

const toggle = createSwitch({ label: 'Dark theme', icon: 'none' });
document.body.append(toggle.element);
Web Components
<m-switch icon="none">Dark theme</m-switch>
React
import { Switch } from 'material/react';

export function Example() {
  return (
    <Switch icon="none">Dark theme</Switch>
  );
}
Vue
<script setup lang="ts">
import { MSwitch } from 'material/vue';
</script>

<template>
  <MSwitch icon="none">Dark theme</MSwitch>
</template>
Svelte
<script lang="ts">
  import { Switch } from 'material/svelte';
</script>

<Switch icon="none">Dark theme</Switch>
SolidJS
import { Switch } from 'material/solid';

export function Example() {
  return (
    <Switch icon="none">Dark theme</Switch>
  );
}

Supporting text

Supporting text sits under the label and describes the switch. setSupportingText(text, true) shows it as an error, and error sets the error state with or without text.

Vanilla
import { createSwitch } from 'material';

const toggle = createSwitch({ label: 'Back up photos', supportingText: 'Uses about 2 GB' });
document.body.append(toggle.element);

function offline() {
  toggle.setSupportingText('Connect to back up');
}
Web Components
<m-switch supporting-text="Uses about 2 GB">Back up photos</m-switch>

<script type="module">
  const switchElement = document.querySelector('m-switch');

  function offline() {
    switchElement.setAttribute('supporting-text', "Connect to back up");
  }
</script>
React
import { useState } from 'react';
import { Switch } from 'material/react';

export function Example() {
  const [supportingText, setSupportingText] = useState("Uses about 2 GB");
  const offline = () => setSupportingText("Connect to back up");

  return (
    <Switch supportingText={supportingText}>Back up photos</Switch>
  );
}
Vue
<script setup lang="ts">
import { ref } from 'vue';
import { MSwitch } from 'material/vue';

const supportingText = ref("Uses about 2 GB");

function offline() {
  supportingText.value = "Connect to back up";
}
</script>

<template>
  <MSwitch :supporting-text="supportingText">Back up photos</MSwitch>
</template>
Svelte
<script lang="ts">
  import { Switch } from 'material/svelte';

  let supportingText = $state("Uses about 2 GB");

  function offline() {
    supportingText = "Connect to back up";
  }
</script>

<Switch supportingText={supportingText}>Back up photos</Switch>
SolidJS
import { createSignal } from 'solid-js';
import { Switch } from 'material/solid';

export function Example() {
  const [supportingText, setSupportingText] = createSignal("Uses about 2 GB");
  const offline = () => setSupportingText("Connect to back up");

  return (
    <Switch supportingText={supportingText()}>Back up photos</Switch>
  );
}

labelPosition: 'end' puts the label after the switch.

API

Options

label

The label, which names the switch

Typestring | Node
Defaultundefined
labelPosition

Which side of the switch the label sits on, in the reading direction

Type'start' | 'end'
Default'start'
checked

Whether the switch starts on

Typeboolean
Defaultfalse
disabled

Whether the switch starts disabled

Typeboolean
Defaultfalse
icon

Icon in the selected handle, as SVG markup; 'none' for no icons

Typestring
Defaulta check
unselectedIcon

Icon in the unselected handle, which then grows to 24dp

Typestring
Defaultundefined
supportingText

Text under the label, linked to the switch with aria-describedby

Typestring
Defaultundefined
error

The error state: an error outline and aria-invalid, with or without supporting text

Typeboolean
Defaultfalse
name

The input's name, for forms

Typestring
Defaultundefined
value

The value submitted when on

Typestring
Default'on'
required

Whether the form requires it on

Typeboolean
Defaultfalse
ariaLabel

Accessible name when there is no visible label

Typestring
Defaultundefined
class

Additional CSS classes

Typestring
Defaultundefined
prefix

Prefix for CSS class names

Typestring
Default'mtrl'

Methods

check() / uncheck() / toggle()

Changes the state, and emits change when the state changes

Parametersnone
ReturnsSwitchComponent
isChecked()

Whether the switch is on

Parametersnone
Returnsboolean
getValue()

The on state

Parametersnone
Returnsboolean
setValue(value)

Turns it on or off; the strings 'true' and '1' turn it on

Parametersvalue: boolean | string
ReturnsSwitchComponent
getValueAttribute() / setValueAttribute(value)

The input's value attribute

Parametersvalue: string
Returnsstring / SwitchComponent
getLabel() / setLabel(text)

The label

Parameterstext: string
Returnsstring / SwitchComponent
setSupportingText(text, isError?)

The supporting text, and the error state with it

Parameterstext: string, isError?: boolean
ReturnsSwitchComponent
removeSupportingText()

Removes the supporting text

Parametersnone
ReturnsSwitchComponent
enable() / disable()

The disabled state

Parametersnone
ReturnsSwitchComponent
on(event, handler) / off(event, handler)

Adds or removes a listener

Parametersevent: string, handler: Function
ReturnsSwitchComponent
destroy()

Removes the switch

Parametersnone
Returnsvoid

Events

change

The state changed

Payload{ checked, value, valueAttribute, nativeEvent? }
focus / blur

The switch gained or lost focus

PayloadFocusEvent

value is the boolean, the same as getValue(). valueAttribute is the input's value attribute, submitted with a form. nativeEvent is there when the user toggled it, not for the methods. The web component's change carries { checked, value, valueAttribute }.

Styling

.mtrl-switch { }                  /* the root */
.mtrl-switch--checked, .mtrl-switch--disabled, .mtrl-switch--error { }
.mtrl-switch--label-end, .mtrl-switch--icons { }
.mtrl-switch__container, .mtrl-switch__content, .mtrl-switch__label, .mtrl-switch__helper { }
.mtrl-switch__input { }           /* the native input, over the whole switch */
.mtrl-switch__track, .mtrl-switch__thumb { }
.mtrl-switch__thumb-icon, .mtrl-switch__thumb-icon--unselected { }

Measurements

From the m3.material.io switch specs, then Compose's SwitchTokens.

Attribute Value
Track 52 × 32dp, fully rounded, a 2dp outline
Handle 16dp off, 24dp on or with an icon, 28dp pressed
Icons 16dp; on-primary-container when on, surface-container-highest when off
Off surface-container-highest track, outline outline and handle
On primary track, on-primary handle
Interaction The handle turns on-surface-variant (off) or primary-container (on) on hover, focus and press, over a 40dp state layer
Disabled Track on-surface 12%, handle on-surface 38%; when on, a surface handle
Label on-surface
Motion The handle moves and resizes on the fast spatial spring