3
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7
Code distributed by Google as part of the polymer project is also
8
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11
<link rel="import" href="../polymer/polymer.html">
12
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
13
<link rel="import" href="../paper-styles/color.html">
14
<link rel="import" href="../paper-styles/default-theme.html">
15
<link rel="import" href="../paper-behaviors/paper-checked-element-behavior.html">
18
Material design: [Switch](https://www.google.com/design/spec/components/selection-controls.html#selection-controls-switch)
20
`paper-toggle-button` provides a ON/OFF switch that user can toggle the state
21
by tapping or by dragging the switch.
25
<paper-toggle-button></paper-toggle-button>
29
The following custom properties and mixins are available for styling:
31
Custom property | Description | Default
32
----------------|-------------|----------
33
`--paper-toggle-button-unchecked-bar-color` | Slider color when the input is not checked | `#000000`
34
`--paper-toggle-button-unchecked-button-color` | Button color when the input is not checked | `--paper-grey-50`
35
`--paper-toggle-button-unchecked-ink-color` | Selected/focus ripple color when the input is not checked | `--dark-primary-color`
36
`--paper-toggle-button-checked-bar-color` | Slider button color when the input is checked | `--primary-color`
37
`--paper-toggle-button-checked-button-color` | Button color when the input is checked | `--primary-color`
38
`--paper-toggle-button-checked-ink-color` | Selected/focus ripple color when the input is checked | `--primary-color`
39
`--paper-toggle-button-unchecked-bar` | Mixin applied to the slider when the input is not checked | `{}`
40
`--paper-toggle-button-unchecked-button` | Mixin applied to the slider button when the input is not checked | `{}`
41
`--paper-toggle-button-checked-bar` | Mixin applied to the slider when the input is checked | `{}`
42
`--paper-toggle-button-checked-button` | Mixin applied to the slider button when the input is checked | `{}`
43
`--paper-toggle-button-label-color` | Label color | `--primary-text-color`
44
`--paper-toggle-button-label-spacing` | Spacing between the label and the button | `8px`
46
This element applies the mixin `--paper-font-common-base` but does not import `paper-styles/typography.html`.
47
In order to apply the `Roboto` font to this element, make sure you've imported `paper-styles/typography.html`.
50
@element paper-toggle-button
55
<dom-module id="paper-toggle-button">
56
<template strip-whitespace>
60
display: inline-block;
61
@apply(--layout-horizontal);
62
@apply(--layout-center);
63
@apply(--paper-font-common-base);
81
transition: background-color linear .08s;
82
background-color: var(--paper-toggle-button-unchecked-bar-color, #000000);
84
@apply(--paper-toggle-button-unchecked-bar);
94
box-shadow: 0 1px 5px 0 rgba(0, 0, 0, 0.6);
95
transition: -webkit-transform linear .08s, background-color linear .08s;
96
transition: transform linear .08s, background-color linear .08s;
97
will-change: transform;
98
background-color: var(--paper-toggle-button-unchecked-button-color, --paper-grey-50);
100
@apply(--paper-toggle-button-unchecked-button);
103
.toggle-button.dragging {
104
-webkit-transition: none;
108
:host([checked]:not([disabled])) .toggle-bar {
110
background-color: var(--paper-toggle-button-checked-bar-color, --primary-color);
112
@apply(--paper-toggle-button-checked-bar);
115
:host([disabled]) .toggle-bar {
116
background-color: #000;
120
:host([checked]) .toggle-button {
121
-webkit-transform: translate(16px, 0);
122
transform: translate(16px, 0);
125
:host([checked]:not([disabled])) .toggle-button {
126
background-color: var(--paper-toggle-button-checked-button-color, --primary-color);
128
@apply(--paper-toggle-button-checked-button);
131
:host([disabled]) .toggle-button {
132
background-color: #bdbdbd;
145
pointer-events: none;
146
color: var(--paper-toggle-button-unchecked-ink-color, --primary-text-color);
149
:host([checked]) .toggle-ink {
150
color: var(--paper-toggle-button-checked-ink-color, --primary-color);
154
display: inline-block;
158
/* The toggle button has an absolute position of -3px; The extra 1px
159
/* accounts for the toggle button shadow box. */
165
display: inline-block;
166
vertical-align: middle;
167
padding-left: var(--paper-toggle-button-label-spacing, 8px);
168
pointer-events: none;
169
color: var(--paper-toggle-button-label-color, --primary-text-color);
173
<div class="toggle-container">
174
<div id="toggleBar" class="toggle-bar"></div>
175
<div id="toggleButton" class="toggle-button"></div>
178
<div class="toggle-label"><content></content></div>
184
is: 'paper-toggle-button',
187
Polymer.PaperCheckedElementBehavior
192
'aria-pressed': 'false',
198
* Fired when the checked state changes due to user interaction.
203
* Fired when the checked state changes.
213
_ontrack: function(event) {
214
var track = event.detail;
215
if (track.state === 'start') {
216
this._trackStart(track);
217
} else if (track.state === 'track') {
218
this._trackMove(track);
219
} else if (track.state === 'end') {
220
this._trackEnd(track);
224
_trackStart: function(track) {
225
this._width = this.$.toggleBar.offsetWidth / 2;
227
* keep an track-only check state to keep the dragging behavior smooth
228
* while toggling activations
230
this._trackChecked = this.checked;
231
this.$.toggleButton.classList.add('dragging');
234
_trackMove: function(track) {
236
this._x = Math.min(this._width,
237
Math.max(0, this._trackChecked ? this._width + dx : dx));
238
this.translate3d(this._x + 'px', 0, 0, this.$.toggleButton);
239
this._userActivate(this._x > (this._width / 2));
242
_trackEnd: function(track) {
243
this.$.toggleButton.classList.remove('dragging');
244
this.transform('', this.$.toggleButton);
247
// customize the element's ripple
248
_createRipple: function() {
249
this._rippleContainer = this.$.toggleButton;
250
var ripple = Polymer.PaperRippleBehavior._createRipple();
252
ripple.setAttribute('recenters', '');
253
ripple.classList.add('circle', 'toggle-ink');