OLD | NEW |
1 <!-- | 1 <!-- |
2 @license | 2 @license |
3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. | 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.g
ithub.io/LICENSE.txt | 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.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/CONTRI
BUTORS.txt | 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
7 Code distributed by Google as part of the polymer project is also | 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/PATEN
TS.txt | 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
9 --> | 9 --> |
10 | 10 |
11 <link rel="import" href="../polymer/polymer.html"> | 11 <link rel="import" href="../polymer/polymer.html"> |
12 <link rel="import" href="../paper-ripple/paper-ripple.html"> | 12 <link rel="import" href="../paper-ripple/paper-ripple.html"> |
13 <link rel="import" href="../paper-behaviors/paper-radio-button-behavior.html"> | 13 <link rel="import" href="../paper-behaviors/paper-radio-button-behavior.html"> |
14 | 14 |
15 <!-- | 15 <!-- |
16 `paper-toggle-button` provides a ON/OFF switch that user can toggle the state | 16 `paper-toggle-button` provides a ON/OFF switch that user can toggle the state |
17 by tapping or by dragging the swtich. | 17 by tapping or by dragging the switch. |
18 | 18 |
19 Example: | 19 Example: |
20 | 20 |
21 <paper-toggle-button></paper-toggle-button> | 21 <paper-toggle-button></paper-toggle-button> |
22 | 22 |
23 Styling toggle-button: | 23 ### Styling |
24 <style is="x-style"> | 24 |
25 * { | 25 The following custom properties and mixins are available for styling: |
26 --paper-toggle-button-unchecked-bar-color: #FF4081; | 26 |
27 --paper-toggle-button-unchecked-button-color: #9c27b0; | 27 Custom property | Description | Default |
28 --paper-toggle-button-unchecked-ink-color: #009688; | 28 ----------------|-------------|---------- |
29 --paper-toggle-button-checked-bar-color: #5677fc; | 29 `--paper-toggle-button-unchecked-bar-color` | Slider color when the input is not
checked | `#000000` |
30 --paper-toggle-button-checked-button-color: #ff4081; | 30 `--paper-toggle-button-unchecked-button-color` | Button color when the input is
not checked | `--paper-grey-50` |
31 --paper-toggle-button-checked-ink-color: #ff4081; | 31 `--paper-toggle-button-unchecked-ink-color` | Selected/focus ripple color when t
he input is not checked | `--dark-primary-color` |
32 } | 32 `--paper-toggle-button-checked-bar-color` | Slider button color when the input i
s checked | `--google-green-500` |
33 </style> | 33 `--paper-toggle-button-checked-button-color` | Button color when the input is ch
ecked | `--google-green-500` |
| 34 `--paper-toggle-button-checked-ink-color` | Selected/focus ripple color when the
input is checked | `--google-green-500` |
34 | 35 |
35 @group Paper Elements | 36 @group Paper Elements |
36 @element paper-toggle-button | 37 @element paper-toggle-button |
| 38 @hero hero.svg |
| 39 @demo demo/index.html |
37 --> | 40 --> |
| 41 <style is="custom-style"> |
| 42 :root { |
| 43 --paper-toggle-button-unchecked-bar-color: #000000; |
| 44 --paper-toggle-button-unchecked-button-color: var(--paper-grey-50); |
| 45 --paper-toggle-button-unchecked-ink-color: var(--dark-primary-color); |
| 46 |
| 47 --paper-toggle-button-checked-bar-color: var(--google-green-500); |
| 48 --paper-toggle-button-checked-button-color: var(--google-green-500); |
| 49 --paper-toggle-button-checked-ink-color: var(--google-green-500); |
| 50 } |
| 51 </style> |
38 | 52 |
39 <dom-module id="paper-toggle-button"> | 53 <dom-module id="paper-toggle-button"> |
40 | 54 |
41 <style is="x-style"> | |
42 * { | |
43 --paper-toggle-button-unchecked-bar-color: #000000; | |
44 --paper-toggle-button-unchecked-button-color: #f1f1f1; | |
45 --paper-toggle-button-unchecked-ink-color: #bbb; | |
46 --paper-toggle-button-checked-bar-color: #0f9d58; | |
47 --paper-toggle-button-checked-button-color: #0f9d58; | |
48 --paper-toggle-button-checked-ink-color: #0f9d58; | |
49 } | |
50 </style> | |
51 | |
52 <link rel="import" type="css" href="paper-toggle-button.css"> | 55 <link rel="import" type="css" href="paper-toggle-button.css"> |
53 | 56 |
54 <template> | 57 <template> |
55 | 58 |
56 <div id="toggleContainer"> | 59 <div id="toggleContainer"> |
57 <div id="toggleBar" class="toggle-bar"></div> | 60 <div id="toggleBar" class="toggle-bar"></div> |
58 <div id="toggleButton" class="toggle-button"> | 61 <div id="toggleButton" class="toggle-button"> |
59 <paper-ripple id="ink" class="toggle-ink circle" recenters></paper-rippl
e> | 62 <paper-ripple id="ink" class="toggle-ink circle" recenters></paper-rippl
e> |
60 </div> | 63 </div> |
61 </div> | 64 </div> |
62 | 65 |
63 </template> | 66 </template> |
64 | 67 |
65 <script> | 68 <script> |
66 Polymer({ | 69 Polymer({ |
67 is: 'paper-toggle-button', | 70 is: 'paper-toggle-button', |
68 | 71 |
69 behaviors: [ | 72 behaviors: [ |
70 Polymer.PaperRadioButtonBehavior | 73 Polymer.PaperRadioButtonBehavior |
71 ], | 74 ], |
72 | 75 |
73 // The custom properties shim is currently an opt-in feature. | |
74 enableCustomStyleProperties: true, | |
75 | |
76 hostAttributes: { | 76 hostAttributes: { |
77 role: 'button', | 77 role: 'button', |
78 'aria-pressed': 'false', | 78 'aria-pressed': 'false', |
79 tabindex: 0 | 79 tabindex: 0 |
80 }, | 80 }, |
81 | 81 |
82 properties: { | 82 properties: { |
83 /** | 83 /** |
84 * Fired when the checked state changes due to user interaction. | 84 * Fired when the checked state changes due to user interaction. |
85 * | 85 * |
(...skipping 28 matching lines...) Expand all Loading... |
114 * @default true | 114 * @default true |
115 */ | 115 */ |
116 toggles: { | 116 toggles: { |
117 type: Boolean, | 117 type: Boolean, |
118 value: true, | 118 value: true, |
119 reflectToAttribute: true | 119 reflectToAttribute: true |
120 } | 120 } |
121 }, | 121 }, |
122 | 122 |
123 listeners: { | 123 listeners: { |
124 // TODO(sjmiles): tracking feature disabled until we can control | 124 track: '_ontrack' |
125 // track/tap interaction with confidence | 125 }, |
126 //xtrack: '_ontrack' | 126 |
| 127 ready: function() { |
| 128 this._isReady = true; |
127 }, | 129 }, |
128 | 130 |
129 // button-behavior hook | 131 // button-behavior hook |
130 _buttonStateChanged: function() { | 132 _buttonStateChanged: function() { |
131 this.checked = this.active; | 133 if (this.disabled) { |
| 134 return; |
| 135 } |
| 136 if (this._isReady) { |
| 137 this.checked = this.active; |
| 138 } |
132 }, | 139 }, |
133 | 140 |
134 _checkedChanged: function(checked) { | 141 _checkedChanged: function(checked) { |
135 this.active = this.checked; | 142 this.active = this.checked; |
136 this.fire('iron-change'); | 143 this.fire('iron-change'); |
137 }, | 144 }, |
138 | 145 |
139 _ontrack: function(event) { | 146 _ontrack: function(event) { |
140 var track = event.detail; | 147 var track = event.detail; |
141 if (track.state === 'start' ) { | 148 if (track.state === 'start') { |
142 //this._preventTap = true; | |
143 this._trackStart(track); | 149 this._trackStart(track); |
144 } else if (track.state === 'move' ) { | 150 } else if (track.state === 'track') { |
145 this._trackMove(track); | 151 this._trackMove(track); |
146 } else if (track.state === 'end' ) { | 152 } else if (track.state === 'end') { |
147 this._trackEnd(track); | 153 this._trackEnd(track); |
148 } | 154 } |
149 }, | 155 }, |
150 | 156 |
151 _trackStart: function(track) { | 157 _trackStart: function(track) { |
152 this._width = this.$.toggleBar.offsetWidth / 2; | 158 this._width = this.$.toggleBar.offsetWidth / 2; |
153 this._startx = track.x; | 159 /* |
| 160 * keep an track-only check state to keep the dragging behavior smooth |
| 161 * while toggling activations |
| 162 */ |
| 163 this._trackChecked = this.checked; |
| 164 this.$.toggleButton.classList.add('dragging'); |
154 }, | 165 }, |
155 | 166 |
156 _trackMove: function(track) { | 167 _trackMove: function(track) { |
157 var dx = track.x - this._startx; | 168 var dx = track.dx; |
158 this._x = Math.min(this._width, | 169 this._x = Math.min(this._width, |
159 Math.max(0, this.checked ? this._width + dx : dx)); | 170 Math.max(0, this._trackChecked ? this._width + dx : dx)); |
160 this.$.toggleButton.classList.add('dragging'); | 171 this.translate3d(this._x + 'px', 0, 0, this.$.toggleButton); |
161 this.translate3d(this, this._x + 'px', 0, 0); | 172 this._userActivate(this._x > (this._width / 2)); |
162 }, | 173 }, |
163 | 174 |
164 _trackEnd: function(track) { | 175 _trackEnd: function(track) { |
165 this.$.toggleButton.classList.remove('dragging'); | 176 this.$.toggleButton.classList.remove('dragging'); |
166 this.transform(this, ''); | 177 this.transform('', this.$.toggleButton); |
167 this._userActivate(Math.abs(this._x) > this._width / 2); | |
168 } | 178 } |
169 | 179 |
170 }); | 180 }); |
171 </script> | 181 </script> |
172 | 182 |
173 </dom-module> | 183 </dom-module> |
OLD | NEW |