2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11 `paper-toggle-button` provides a ON/OFF switch that user can toggle the state
12 by tapping or by dragging the swtich.
16 <paper-toggle-button></paper-toggle-button>
18 Styling toggle button:
20 To change the toggle color:
22 paper-toggle-button::shadow .toggle {
23 background-color: #9c27b0;
26 To change the ink color:
28 paper-toggle-button::shadow .toggle-ink {
32 To change the checked toggle color:
34 paper-toggle-button::shadow [checked] .toggle {
35 background-color: #4285f4;
38 To change the checked ink color:
40 paper-toggle-button::shadow [checked] .toggle-ink {
44 To change the toggle bar and toggle button colors separately:
46 paper-toggle-button::shadow .toggle-bar {
47 background-color: #5677fc;
50 paper-toggle-button::shadow .toggle-button {
51 background-color: #9c27b0;
55 @element paper-toggle-button
59 <link rel=
"import" href=
"../paper-radio-button/paper-radio-button.html">
60 <link rel=
"import" href=
"../core-a11y-keys/core-a11y-keys.html">
62 <polymer-element name=
"paper-toggle-button" attributes=
"checked disabled" role=
"button" aria-pressed=
"false" tabindex=
"0">
65 <link rel=
"stylesheet" href=
"paper-toggle-button.css">
67 <core-a11y-keys target=
"{{}}" keys=
"space" on-keys-pressed=
"{{tap}}"></core-a11y-keys>
69 <div id=
"toggleContainer" checked?=
"{{checked}}" disabled?=
"{{disabled}}">
71 <div id=
"toggleBar" class=
"toggle toggle-bar"></div>
73 <div id=
"toggleButton" class=
"toggle toggle-button">
74 <paper-ripple id=
"ink" class=
"toggle-ink circle"></paper-ripple>
82 Polymer('paper-toggle-button', {
85 * Fired when the checked state changes due to user interaction.
91 * Fired when the checked state changes.
97 * Gets or sets the state, `true` is checked and `false` is unchecked.
106 * If true, the toggle button is disabled. A disabled toggle button cannot
107 * be tapped or dragged to change the checked state.
109 * @attribute disabled
119 trackstart
: 'trackStart',
124 downAction: function(e
) {
125 var rect
= this.$.ink
.getBoundingClientRect();
126 this.$.ink
.downAction({
127 x
: rect
.left
+ rect
.width
/ 2,
128 y
: rect
.top
+ rect
.height
/ 2
132 upAction: function(e
) {
133 this.$.ink
.upAction();
140 this.checked
= !this.checked
;
144 trackStart: function(e
) {
148 this._w
= this.$.toggleBar
.offsetWidth
/ 2;
152 trackx: function(e
) {
153 this._x
= Math
.min(this._w
,
154 Math
.max(0, this.checked
? this._w
+ e
.dx
: e
.dx
));
155 this.$.toggleButton
.classList
.add('dragging');
156 var s
= this.$.toggleButton
.style
;
157 s
.webkitTransform
= s
.transform
= 'translate3d(' + this._x
+ 'px,0,0)';
160 trackEnd: function() {
161 var s
= this.$.toggleButton
.style
;
162 s
.transform
= s
.webkitTransform
= '';
163 this.$.toggleButton
.classList
.remove('dragging');
164 var old
= this.checked
;
165 this.checked
= Math
.abs(this._x
) > this._w
/ 2;
166 if (this.checked
!== old
) {
171 checkedChanged: function() {
172 this.setAttribute('aria-pressed', Boolean(this.checked
));
173 this.fire('core-change');