Switch

Switches turn a single setting on or off.

Two switches for Wi-Fi and Bluetooth; the first is on.

View interactive demo inline.

Open interactive demo in new tab.

Switches are similar to checkboxes, and can be unselected or selected.

<petal-switch></petal-switch>
<petal-switch selected></petal-switch>

Icons can be used to visually emphasize the switch's selected state. Switches can choose to display both icons or only selected icons.

<petal-switch icons></petal-switch>
<petal-switch icons selected></petal-switch>

<petal-switch icons show-only-selected-icon></petal-switch>
<petal-switch icons show-only-selected-icon selected></petal-switch>

Associate a label with a switch using the <label> element.

<label>
  Wi-Fi
  <petal-switch selected></petal-switch>
</label>

<label for="switch">Bluetooth</label>
<petal-switch id="switch"></petal-switch>

Add an aria-label attribute to switches without labels or switches whose labels need to be more descriptive.

<petal-switch aria-label="Lights"></petal-switch>

<label>
  All
  <petal-switch aria-label="All notifications"></petal-switch>
</label>
bookmark

switches are not automatically labelled by <label> elements and always need an aria-label. See b/294081528.

Switches supports Material theming and can be customized in terms of color and shape.

TokenDefault value
--md-switch-handle-color--md-sys-color-outline
--md-switch-handle-shape--md-sys-shape-corner-full
--md-switch-track-color--md-sys-color-surface-container-highest
--md-switch-track-shape--md-sys-shape-corner-full
--md-switch-selected-handle-color--md-sys-color-on-primary
--md-switch-selected-track-color--md-sys-color-primary
<style>
  :root {
    /* System tokens */
    --md-sys-color-primary: #006a6a;
    --md-sys-color-on-primary: #ffffff;
    --md-sys-color-outline: #6f7979;
    --md-sys-color-surface-container-highest: #dde4e3;

    /* Component tokens */
    --md-switch-handle-shape: 0px;
    --md-switch-track-shape: 0px;
  }
</style>

<petal-switch></petal-switch>
<petal-switch selected></petal-switch>
PropertyAttributeTypeDefaultDescription
selectedselectedbooleanfalsePuts the switch in the selected state and sets the form submission value to the value property.
iconsiconsbooleanfalseShows both the selected and deselected icons.
showOnlySelectedIconshow-only-selected-iconbooleanfalseShows only the selected icon, and not the deselected icon. If true, overrides the behavior of the icons property.
requiredrequiredbooleanfalseWhen true, require the switch to be selected when participating in form submission.
https://developer.mozilla.org/en-US/docs/Web/HTML/Element/input/checkbox#validation
valuevaluestring'on'The value associated with this switch on form submission. null is submitted when selected is false.
namenamestringundefinedThe HTML name to use in form submission.
disableddisabledbooleanundefinedWhether or not the element is disabled.
formHTMLFormElementundefinedThe associated form element with which this element's value will submit.
labelsNodeListundefinedThe labels this element is associated with.
validityValidityStateundefinedReturns a ValidityState object that represents the validity states of the element.
https://developer.mozilla.org/en-US/docs/Web/API/ValidityState
validationMessagestringundefinedReturns a validation error message or an empty string if the element is valid.
https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/validationMessage
willValidatebooleanundefinedReturns whether an element will successfully validate based on forms validation rules and constraints.
Disabled and readonly elements will not validate.
https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/willValidate
MethodParametersReturnsDescription
formResetCallbackNonevoid
formStateRestoreCallbackstatevoid
checkValidityNonebooleanChecks the element's constraint validation and returns true if the element is valid or false if not.
If invalid, this method will dispatch an invalid event.
https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/checkValidity
reportValidityNonebooleanChecks the element's constraint validation and returns true if the element is valid or false if not.
If invalid, this method will dispatch a cancelable invalid event. If not canceled, a the current validationMessage will be reported to the user.
https://developer.mozilla.org/en-US/docs/Web/API/ElementInternals/reportValidity
setCustomValidityerrorvoidSets the element's constraint validation error message. When set to a non-empty string, validity.customError will be true and validationMessage will display the provided error.
Use this method to customize error messages reported.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLInputElement/setCustomValidity
EventTypeBubblesComposedDescription
inputInputEventNoNoFired whenever selected changes due to user interaction (bubbles and composed).
changeEventNoNoFired whenever selected changes due to user interaction (bubbles).
About
IntroductionQuick StartMaterial 3 ExpressiveRoadmapSupportBundle SizesDesign tokensToken update v34LicensingVisual testingReleasing
Theming
Material ThemingColorTypographyShape
Components
Button groupButtonsCheckboxChipsDialogsFloating action button (FAB)Icon ButtonsListsMenusProgress indicatorsRadioRippleSelectSlidersSwitchTabsText field