| OLD | NEW |
| 1 <!-- | 1 <!-- |
| 2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | 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.g
ithub.io/LICENSE.txt | 3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.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/CONTRI
BUTORS.txt | 5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 6 Code distributed by Google as part of the polymer project is also | 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/PATEN
TS.txt | 7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 8 --> | 8 --> |
| 9 | 9 |
| 10 <!-- | 10 <!-- |
| 11 `paper-toggle-button` provides a ON/OFF switch that user can toggle the state | 11 `paper-toggle-button` provides a ON/OFF switch that user can toggle the state |
| 12 by tapping or by dragging the swtich. | 12 by tapping or by dragging the swtich. |
| 13 | 13 |
| 14 Example: | 14 Example: |
| 15 | 15 |
| 16 <paper-toggle-button></paper-toggle-button> | 16 <paper-toggle-button></paper-toggle-button> |
| 17 | 17 |
| 18 Styling toggle button: | 18 Styling toggle button: |
| 19 | 19 |
| 20 To change the ink color for checked state: | 20 To change the toggle color: |
| 21 | 21 |
| 22 paper-toggle-button::shadow paper-radio-button::shadow #ink[checked] { | 22 paper-toggle-button::shadow .toggle { |
| 23 background-color: #9c27b0; |
| 24 } |
| 25 |
| 26 To change the ink color: |
| 27 |
| 28 paper-toggle-button::shadow .toggle-ink { |
| 29 color: #009688; |
| 30 } |
| 31 |
| 32 To change the checked toggle color: |
| 33 |
| 34 paper-toggle-button::shadow [checked] .toggle { |
| 35 background-color: #4285f4; |
| 36 } |
| 37 |
| 38 To change the checked ink color: |
| 39 |
| 40 paper-toggle-button::shadow [checked] .toggle-ink { |
| 23 color: #4285f4; | 41 color: #4285f4; |
| 24 } | 42 } |
| 25 | |
| 26 To change the radio checked color: | |
| 27 | |
| 28 paper-toggle-button::shadow paper-radio-button::shadow #onRadio { | |
| 29 background-color: #4285f4; | |
| 30 } | |
| 31 | |
| 32 To change the bar color for checked state: | |
| 33 | 43 |
| 34 paper-toggle-button::shadow #toggleBar[checked] { | 44 To change the toggle bar and toggle button colors separately: |
| 35 background-color: #4285f4; | |
| 36 } | |
| 37 | |
| 38 To change the ink color for unchecked state: | |
| 39 | 45 |
| 40 paper-toggle-button::shadow paper-radio-button::shadow #ink { | 46 paper-toggle-button::shadow .toggle-bar { |
| 41 color: #b5b5b5; | 47 background-color: #5677fc; |
| 42 } | |
| 43 | |
| 44 To change the radio unchecked color: | |
| 45 | |
| 46 paper-toggle-button::shadow paper-radio-button::shadow #offRadio { | |
| 47 border-color: #b5b5b5; | |
| 48 } | 48 } |
| 49 | 49 |
| 50 To change the bar color for unchecked state: | 50 paper-toggle-button::shadow .toggle-button { |
| 51 | 51 background-color: #9c27b0; |
| 52 paper-toggle-button::shadow #toggleBar { | |
| 53 background-color: red; | |
| 54 } | 52 } |
| 55 | 53 |
| 56 @group Paper Elements | 54 @group Paper Elements |
| 57 @element paper-toggle-button | 55 @element paper-toggle-button |
| 58 @homepage github.io | 56 @homepage github.io |
| 59 --> | 57 --> |
| 60 | 58 |
| 61 <link rel="import" href="../paper-radio-button/paper-radio-button.html"> | 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 | 61 |
| 63 <polymer-element name="paper-toggle-button" attributes="checked disabled" role="
button" aria-pressed="false" tabindex="0"> | 62 <polymer-element name="paper-toggle-button" attributes="checked disabled" role="
button" aria-pressed="false" tabindex="0"> |
| 64 <template> | 63 <template> |
| 65 | 64 |
| 66 <link rel="stylesheet" href="paper-toggle-button.css"> | 65 <link rel="stylesheet" href="paper-toggle-button.css"> |
| 67 | 66 |
| 68 <div id="toggleContainer" disabled?="{{disabled}}"> | 67 <core-a11y-keys target="{{}}" keys="space" on-keys-pressed="{{tap}}"></core-a1
1y-keys> |
| 68 |
| 69 <div id="toggleContainer" checked?="{{checked}}" disabled?="{{disabled}}"> |
| 69 | 70 |
| 70 <div id="toggleBar" checked?="{{checked}}"></div> | 71 <div id="toggleBar" class="toggle toggle-bar"></div> |
| 71 | 72 |
| 72 <paper-radio-button id="toggleRadio" toggles checked="{{checked}}" on-change
="{{changeAction}}" on-core-change="{{stopPropagation}}" | 73 <div id="toggleButton" class="toggle toggle-button"> |
| 73 on-trackstart="{{trackStart}}" on-trackx="{{trackx}}" on-trackend="{{tra
ckEnd}}"></paper-radio-button> | 74 <paper-ripple id="ink" class="toggle-ink circle"></paper-ripple> |
| 74 | 75 </div> |
| 76 |
| 75 </div> | 77 </div> |
| 76 | 78 |
| 77 </template> | 79 </template> |
| 78 <script> | 80 <script> |
| 79 | 81 |
| 80 Polymer('paper-toggle-button', { | 82 Polymer('paper-toggle-button', { |
| 81 | 83 |
| 82 /** | 84 /** |
| 83 * Fired when the checked state changes due to user interaction. | 85 * Fired when the checked state changes due to user interaction. |
| 84 * | 86 * |
| 85 * @event change | 87 * @event change |
| 86 */ | 88 */ |
| 87 | 89 |
| 88 /** | 90 /** |
| 89 * Fired when the checked state changes. | 91 * Fired when the checked state changes. |
| 90 * | 92 * |
| 91 * @event core-change | 93 * @event core-change |
| 92 */ | 94 */ |
| 93 | 95 |
| 94 /** | 96 /** |
| 95 * Gets or sets the state, `true` is checked and `false` is unchecked. | 97 * Gets or sets the state, `true` is checked and `false` is unchecked. |
| 96 * | 98 * |
| 97 * @attribute checked | 99 * @attribute checked |
| 98 * @type boolean | 100 * @type boolean |
| 99 * @default false | 101 * @default false |
| 100 */ | 102 */ |
| 101 checked: false, | 103 checked: false, |
| 102 | 104 |
| 103 /** | 105 /** |
| 104 * If true, the toggle button is disabled. A disabled toggle button cannot | 106 * If true, the toggle button is disabled. A disabled toggle button cannot |
| 105 * be tapped or dragged to change the checked state. | 107 * be tapped or dragged to change the checked state. |
| 106 * | 108 * |
| 107 * @attribute disabled | 109 * @attribute disabled |
| 108 * @type boolean | 110 * @type boolean |
| 109 * @default false | 111 * @default false |
| 110 */ | 112 */ |
| 111 disabled: false, | 113 disabled: false, |
| 112 | 114 |
| 115 eventDelegates: { |
| 116 down: 'downAction', |
| 117 up: 'upAction', |
| 118 tap: 'tap', |
| 119 trackstart: 'trackStart', |
| 120 trackx: 'trackx', |
| 121 trackend: 'trackEnd' |
| 122 }, |
| 123 |
| 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 |
| 129 }); |
| 130 }, |
| 131 |
| 132 upAction: function(e) { |
| 133 this.$.ink.upAction(); |
| 134 }, |
| 135 |
| 136 tap: function() { |
| 137 if (this.disabled) { |
| 138 return; |
| 139 } |
| 140 this.checked = !this.checked; |
| 141 this.fire('change'); |
| 142 }, |
| 143 |
| 113 trackStart: function(e) { | 144 trackStart: function(e) { |
| 114 this._w = this.$.toggleBar.offsetLeft + this.$.toggleBar.offsetWidth; | 145 if (this.disabled) { |
| 146 return; |
| 147 } |
| 148 this._w = this.$.toggleBar.offsetWidth / 2; |
| 115 e.preventTap(); | 149 e.preventTap(); |
| 116 }, | 150 }, |
| 117 | 151 |
| 118 trackx: function(e) { | 152 trackx: function(e) { |
| 119 this._x = Math.min(this._w, | 153 this._x = Math.min(this._w, |
| 120 Math.max(0, this.checked ? this._w + e.dx : e.dx)); | 154 Math.max(0, this.checked ? this._w + e.dx : e.dx)); |
| 121 this.$.toggleRadio.classList.add('dragging'); | 155 this.$.toggleButton.classList.add('dragging'); |
| 122 var s = this.$.toggleRadio.style; | 156 var s = this.$.toggleButton.style; |
| 123 s.webkitTransform = s.transform = 'translate3d(' + this._x + 'px,0,0)'; | 157 s.webkitTransform = s.transform = 'translate3d(' + this._x + 'px,0,0)'; |
| 124 }, | 158 }, |
| 125 | 159 |
| 126 trackEnd: function() { | 160 trackEnd: function() { |
| 127 var s = this.$.toggleRadio.style; | 161 var s = this.$.toggleButton.style; |
| 128 s.transform = s.webkitTransform = ''; | 162 s.transform = s.webkitTransform = ''; |
| 129 this.$.toggleRadio.classList.remove('dragging'); | 163 this.$.toggleButton.classList.remove('dragging'); |
| 130 var old = this.checked; | 164 var old = this.checked; |
| 131 this.checked = Math.abs(this._x) > this._w / 2; | 165 this.checked = Math.abs(this._x) > this._w / 2; |
| 132 if (this.checked !== old) { | 166 if (this.checked !== old) { |
| 133 this.fire('change'); | 167 this.fire('change'); |
| 134 } | 168 } |
| 135 }, | 169 }, |
| 136 | 170 |
| 137 checkedChanged: function() { | 171 checkedChanged: function() { |
| 138 this.setAttribute('aria-pressed', Boolean(this.checked)); | 172 this.setAttribute('aria-pressed', Boolean(this.checked)); |
| 139 this.fire('core-change'); | 173 this.fire('core-change'); |
| 140 }, | |
| 141 | |
| 142 changeAction: function(e) { | |
| 143 e.stopPropagation(); | |
| 144 this.fire('change'); | |
| 145 }, | |
| 146 | |
| 147 stopPropagation: function(e) { | |
| 148 e.stopPropagation(); | |
| 149 } | 174 } |
| 150 | 175 |
| 151 }); | 176 }); |
| 152 | 177 |
| 153 </script> | 178 </script> |
| 154 </polymer-element> | 179 </polymer-element> |
| OLD | NEW |