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
import { createSwitch } from 'material';
const toggle = createSwitch({ label: 'Wi-Fi', checked: true });
toggle.on('change', ({ checked }) => setWifi(checked));
document.body.append(toggle.element);
<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>
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>
);
}
<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>
<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>
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.
import { createSwitch } from 'material';
const toggle = createSwitch({ label: 'Dark theme', icon: 'none' });
document.body.append(toggle.element);
<m-switch icon="none">Dark theme</m-switch>
import { Switch } from 'material/react';
export function Example() {
return (
<Switch icon="none">Dark theme</Switch>
);
}
<script setup lang="ts">
import { MSwitch } from 'material/vue';
</script>
<template>
<MSwitch icon="none">Dark theme</MSwitch>
</template>
<script lang="ts">
import { Switch } from 'material/svelte';
</script>
<Switch icon="none">Dark theme</Switch>
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.
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');
}
<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>
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>
);
}
<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>
<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>
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
labelThe label, which names the switch
string | NodeundefinedlabelPositionWhich side of the switch the label sits on, in the reading direction
'start' | 'end''start'checkedWhether the switch starts on
booleanfalsedisabledWhether the switch starts disabled
booleanfalseiconIcon in the selected handle, as SVG markup; 'none' for no icons
stringunselectedIconIcon in the unselected handle, which then grows to 24dp
stringundefinedsupportingTextText under the label, linked to the switch with aria-describedby
stringundefinederrorThe error state: an error outline and aria-invalid, with or without supporting text
booleanfalsenameThe input's name, for forms
stringundefinedvalueThe value submitted when on
string'on'requiredWhether the form requires it on
booleanfalseariaLabelAccessible name when there is no visible label
stringundefinedclassAdditional CSS classes
stringundefinedprefixPrefix for CSS class names
string'mtrl'Methods
check() / uncheck() / toggle()Changes the state, and emits change when the state changes
SwitchComponentisChecked()Whether the switch is on
booleangetValue()The on state
booleansetValue(value)Turns it on or off; the strings 'true' and '1' turn it on
value: boolean | stringSwitchComponentgetValueAttribute() / setValueAttribute(value)The input's value attribute
value: stringstring / SwitchComponentgetLabel() / setLabel(text)The label
text: stringstring / SwitchComponentsetSupportingText(text, isError?)The supporting text, and the error state with it
text: string, isError?: booleanSwitchComponentremoveSupportingText()Removes the supporting text
SwitchComponentenable() / disable()The disabled state
SwitchComponenton(event, handler) / off(event, handler)Adds or removes a listener
event: string, handler: FunctionSwitchComponentdestroy()Removes the switch
voidEvents
changeThe state changed
{ checked, value, valueAttribute, nativeEvent? }focus / blurThe switch gained or lost focus
FocusEventvalue 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 |