2.23.0
Switches afford a choice between one of two opposing states or options.
<!-- Off -->
<div class="mds-switch">
<label class="mds-switch__label" for="switch--96661">
<input id="switch--96661" type="checkbox" role="switch" class="mds-switch__input">
<span class="mds-switch__text">
Logic Mode
</span>
</label>
</div>
<!-- Off, Disabled -->
<div class="mds-switch">
<label class="mds-switch__label" for="switch--44794">
<input id="switch--44794" type="checkbox" role="switch" class="mds-switch__input" disabled>
<span class="mds-switch__text">
Logic Mode
</span>
</label>
</div>
<!-- On -->
<div class="mds-switch">
<label class="mds-switch__label" for="switch--65811">
<input id="switch--65811" type="checkbox" role="switch" class="mds-switch__input" checked>
<span class="mds-switch__text">
Logic Mode
</span>
</label>
</div>
<!-- On, Disabled -->
<div class="mds-switch">
<label class="mds-switch__label" for="switch--73142">
<input id="switch--73142" type="checkbox" role="switch" class="mds-switch__input" disabled checked>
<span class="mds-switch__text">
Logic Mode
</span>
</label>
</div>
<!-- Off -->
<mds-switch name="default"> Logic Mode </mds-switch>
<!-- Off, Disabled -->
<mds-switch disabled name="default"> Logic Mode </mds-switch>
<!-- On -->
<mds-switch name="default" checked> Logic Mode </mds-switch>
<!-- On, Disabled -->
<mds-switch disabled checked name="default"> Logic Mode </mds-switch>
Use when the purpose of a switch is clearly implied by its proximity to other content and controls.
<!-- Off -->
<div class="mds-switch mds-switch--hide-label">
<label class="mds-switch__label" for="switch--28731">
<input id="switch--28731" type="checkbox" role="switch" class="mds-switch__input">
<span class="mds-switch__text">
Logic Mode
</span>
</label>
</div>
<!-- Off, Disabled -->
<div class="mds-switch mds-switch--hide-label">
<label class="mds-switch__label" for="switch--14435">
<input id="switch--14435" type="checkbox" role="switch" class="mds-switch__input" disabled>
<span class="mds-switch__text">
Logic Mode
</span>
</label>
</div>
<!-- On -->
<div class="mds-switch mds-switch--hide-label">
<label class="mds-switch__label" for="switch--56297">
<input id="switch--56297" type="checkbox" role="switch" class="mds-switch__input" checked>
<span class="mds-switch__text">
Logic Mode
</span>
</label>
</div>
<!-- On, Disabled -->
<div class="mds-switch mds-switch--hide-label">
<label class="mds-switch__label" for="switch--56453">
<input id="switch--56453" type="checkbox" role="switch" class="mds-switch__input" disabled checked>
<span class="mds-switch__text">
Logic Mode
</span>
</label>
</div>
<!-- Off -->
<mds-switch hidden-text name="without-label"> Logic Mode </mds-switch>
<!-- Off, Disabled -->
<mds-switch hidden-text disabled name="without-label"> Logic Mode </mds-switch>
<!-- On -->
<mds-switch hidden-text name="without-label" checked> Logic Mode </mds-switch>
<!-- On, Disabled -->
<mds-switch hidden-text disabled checked name="without-label"> Logic Mode </mds-switch>
<fieldset class="mds-switch__group" role="group">
<legend class="mds-switch__group-label">
Client Configuration Options
</legend>
<div class="mds-switch">
<label class="mds-switch__label" for="switch--37437">
<input id="switch--37437" type="checkbox" role="switch" class="mds-switch__input">
<span class="mds-switch__text">
Display Overview/Investment
</span>
</label>
</div>
<div class="mds-switch">
<label class="mds-switch__label" for="switch--37222">
<input id="switch--37222" type="checkbox" role="switch" class="mds-switch__input">
<span class="mds-switch__text">
Display Account Details
</span>
</label>
</div>
<div class="mds-switch">
<label class="mds-switch__label" for="switch--52063">
<input id="switch--52063" type="checkbox" role="switch" class="mds-switch__input">
<span class="mds-switch__text">
Display Benchmark Returns
</span>
</label>
</div>
<div class="mds-switch">
<label class="mds-switch__label" for="switch--9678">
<input id="switch--9678" type="checkbox" role="switch" class="mds-switch__input">
<span class="mds-switch__text">
Allow Clients to Generate Reports
</span>
</label>
</div>
<div class="mds-switch">
<label class="mds-switch__label" for="switch--98830">
<input id="switch--98830" type="checkbox" role="switch" class="mds-switch__input">
<span class="mds-switch__text">
Display Document Manager
</span>
</label>
</div>
</fieldset>
<mds-switch-group label="Client Configuration Options">
<mds-switch name="switch-grouped"> Display Overview/Investment </mds-switch>
<mds-switch name="switch-grouped"> Display Account Details </mds-switch>
<mds-switch name="switch-grouped"> Display Benchmark Returns </mds-switch>
<mds-switch name="switch-grouped"> Allow Clients to Generate Reports </mds-switch>
<mds-switch name="switch-grouped"> Display Document Manager </mds-switch>
</mds-switch-group>
Sizing affects the switch size, text size, and internal spacing. The default size is medium, and you can use modifier classes or props to make the switch smaller or larger.
<div class="mds-switch mds-switch--small">
<label class="mds-switch__label" for="switch--52486">
<input id="switch--52486" type="checkbox" role="switch" class="mds-switch__input" checked>
<span class="mds-switch__text">
Logic Mode
</span>
</label>
</div>
<div class="mds-switch mds-switch--small mds-switch--hide-label">
<label class="mds-switch__label" for="switch--99342">
<input id="switch--99342" type="checkbox" role="switch" class="mds-switch__input" checked>
<span class="mds-switch__text">
Logic Mode
</span>
</label>
</div>
<mds-switch size="small" name="switch-small" checked> Logic Mode </mds-switch>
<mds-switch size="small" name="switch-small" hidden-text checked> Logic Mode </mds-switch>
Class
|
Applies to
|
Outcome
|
---|---|---|
|
|
At parent level, this applies base-switch styles to an element. |
|
|
Adjusts styling to render a small switch. |
|
|
Adjusts styling to render a large switch. |
|
|
Adjusts styling to render a touch switch. |
|
|
Hides label text although text is still readable by screen readers. |
|
|
Semantically structures a group of switches. Always include |
|
|
Apply to a parent wrapper of a fieldset to convert switches layout from vertical to horizontal. |
Use this mixin to create media queries in your product styles to change switches to their touch variation.
@include mds-switch-touch-properties();
For example, switching to the touch variation on screens with a max-width
of 500px
:
// Your product SCSS
@media screen and (max-width: 500px) {
@include mds-switch-touch-properties();
}
max-width
, pointer: coarse
is another helpful method for targeting touch screen devices.Switches are comprised of two separate web components, each with a dedicated API:
<mds-switch>
elements to present them as a group.When setting props as attributes on a custom HTML element, use kebab-case
instead of camelCase
.
Prop
|
Type
|
Validation
|
Default
|
Description
|
---|---|---|---|---|
|
String |
— |
— |
A space-separated list of class names that will be appended to the default |
|
Boolean |
— |
|
Sets the |
|
Boolean |
— |
|
If |
|
Boolean |
— |
|
If |
|
String |
Required |
— |
Sets the |
|
String |
One of: |
|
Alters the size of the switch. |
|
String |
Required |
— |
The label text for the switch. Can also be passed via the default slot. |
|
String |
— |
— |
Sets the |
Any text passed in between the <mds-switch></mds-switch>
tags will be used as the text content.
Setting text
, checked
, and size
via props:
<mds-switch name="switch_name" text="Switch Label" size="large" checked></mds-switch>
When setting props as attributes on a custom HTML element, use kebab-case
instead of camelCase
.
Prop
|
Type
|
Validation
|
Default
|
Description
|
---|---|---|---|---|
|
String |
— |
— |
A space-separated list of class names that will be appended to the default |
|
String |
— |
— |
A space-separated list of element |
|
String |
Required |
— |
Label for the switch group. |
Pass two or more <mds-switch>
elements between the <mds-switch-group></mds-switch-group>
tags to set the content of the switch group.
Setting label
via a prop. Using the default slot for the switches in the group:
<mds-switch-group label="Choose your options">
<mds-switch name="switch_name">Switch Label 1</mds-switch>
<mds-switch name="switch_name">Switch Label 2</mds-switch>
</mds-switch-group>
role="switch"
ARIA attribute to ensure they are accurately announced via assistive technologies.role="group"
on the <fieldset>
wrapping grouped switches.spacebar
.