material-web/packages/switch
2019-11-20 11:49:02 -08:00
..
images Add README for form field, and examples to checkbox/radio/switch 2019-11-08 17:44:39 -08:00
src Include mdc-ripple-common macro in element styles 2019-10-17 17:14:15 -07:00
.npmignore Set up new unit test locations for Karma and update package configs. 2019-09-25 13:55:33 -07:00
package.json Auto bump MDC Web deps to 5.0.0-canary.5729943ba.0 (#649) 2019-11-20 11:49:02 -08:00
README.md Add README for form field, and examples to checkbox/radio/switch 2019-11-08 17:44:39 -08:00
tsconfig.json Set up new unit test locations for Karma and update package configs. 2019-09-25 13:55:33 -07:00

<mwc-switch> Published on npm

IMPORTANT: The Material Web Components are a work in progress and subject to major changes until 1.0 release.

Switches toggle the state of a single setting on or off. They are the preferred way to adjust settings on mobile.

Material Design Guidelines: Switch

Installation

npm install @material/mwc-switch

NOTE: The Material Web Components are distributed as ES2017 JavaScript Modules, and use the Custom Elements API. They are compatible with all modern browsers including Chrome, Firefox, Safari, Edge, and IE11, but an additional tooling step is required to resolve bare module specifiers, as well as transpilation and polyfills for Edge and IE11. See here for detailed instructions.

Example Usage

Standard

<mwc-switch></mwc-switch>

Checked by default

<mwc-switch checked></mwc-switch>

Disabled

<mwc-switch disabled></mwc-switch>
<mwc-switch checked disabled></mwc-switch>

Styled

<style>
  mwc-switch {
    --mdc-theme-secondary: red;
  }
</style>
<mwc-switch checked></mwc-switch>

With Form Field

Most applications should use <mwc-formfield> to associate an interactive label with the switch.

<mwc-formfield label="Airplane mode">
  <mwc-switch checked></mwc-switch>
</mwc-formfield>

<script type="module">
  import '@material/mwc-switch';
  import '@material/mwc-formfield';
</script>

API

Slots

None

Properties/Attributes

Name Type Default Description
checked boolean false Whether or not the switch should be checked / activated.
disabled boolean false Disables the input and sets the disabled styles.

Methods

None

Events

Event Name Target Detail Description
change mwc-switch {} Fired when the user modifies the switch checked state from an input device interaction. Note that, like native <input>, the change event is not fired when the checked property is set from JavaScript.

CSS Custom Properties

Name Default Description
--mdc-theme-secondary #018786 Fill color of the thumb head and a lightened fill of the track.

Also inherits styles from ripple

Additional references