Skip to main content

Switch

Turns a setting on or off.

<mm-switch name="masking" label="Mask PII in query results" checked></mm-switch>

Guidance​

Switches turn a setting on or off with immediate effect: "Run this pipeline on a schedule", "Mask PII in query results". Use a checkbox instead when the choice only applies after a form is saved alongside other fields.

State never relies on colour alone: when on, the track fills with ink and the thumb moves to the end and carries a tick; when off, the track is open and the thumb is empty. Set state-label to also print "On" or "Off" beside the label. The track is a 4px-radius control, not a pill: the The guidelines keep full radius for avatars, status pills and filter pills.

Keyboard: Space and Enter toggle (https://www.w3.org/WAI/ARIA/apg/patterns/switch/). The control is a native button with role="switch" and aria-checked.

Form-associated: submits value (default on) under name only while on. required means it must be on. The checked attribute is the default the form resets to.

Properties​

PropertyAttributeTypeDefaultDescription
checkedcheckedbooleanfalse
valuevaluestring'on'Submitted under name while on. Default on.
descriptiondescriptionstring''A second line under the label.
stateLabelstate-labelbooleanfalseAlso prints "On" or "Off" beside the label.
labellabelstring''Field label. Rendered above the control unless hide-label, and always the accessible name.
helphelpstring''Help text under the control.
errorerrorstring''Error text: a sentence saying what to fix. Shows the crimson border, an icon and sets aria-invalid.
hideLabelhide-labelbooleanfalseHides the label visually; it stays the accessible name.
optionaloptionalbooleanfalseMarks the field as optional, in words.
namenamestring''Name submitted with the form.
disableddisabledbooleanfalseDisables the control and excludes it from submission.
requiredrequiredbooleanfalseRequires a value before the form submits.
formHTMLFormElement | null
validityValidityState
validationMessagestring
willValidateboolean
isDisabledbooleanTrue when disabled directly or by a disabled ancestor fieldset.

Methods​

MethodDescription
toggle()Flips the switch, as a click or Space does.
formResetCallback()Restore the initial value when the owning form resets.

Events​

EventDetailDescription
mm-inputCustomEvent<MmSwitchDetail>When the switch is toggled.
mm-changeCustomEvent<MmSwitchDetail>When the switch is toggled.
inputInputEventWhen the switch is toggled.
changeEventWhen the switch is toggled.

CSS parts​

PartDescription
controlThe switch button (track).
thumbThe moving thumb.
labelThe label beside the switch.
descriptionThe description line.
helpThe help line.
errorThe error line.

Examples​

Every state of mm-switch in Storybook, with keyboard and screen reader tests.