| OLD | NEW |
| (Empty) | |
| 1 <!-- |
| 2 @license |
| 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 |
| 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 |
| 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 |
| 9 --> |
| 10 |
| 11 <link rel="import" href="../polymer/polymer.html"> |
| 12 <link rel="import" href="../paper-ripple/paper-ripple.html"> |
| 13 <link rel="import" href="../paper-behaviors/paper-inky-focus-behavior.html"> |
| 14 |
| 15 <!-- |
| 16 `paper-toggle-button` provides a ON/OFF switch that user can toggle the state |
| 17 by tapping or by dragging the switch. |
| 18 |
| 19 Example: |
| 20 |
| 21 <paper-toggle-button></paper-toggle-button> |
| 22 |
| 23 ### Styling |
| 24 |
| 25 The following custom properties and mixins are available for styling: |
| 26 |
| 27 Custom property | Description | Default |
| 28 ----------------|-------------|---------- |
| 29 `--paper-toggle-button-unchecked-bar-color` | Slider color when the input is not
checked | `#000000` |
| 30 `--paper-toggle-button-unchecked-button-color` | Button color when the input is
not checked | `--paper-grey-50` |
| 31 `--paper-toggle-button-unchecked-ink-color` | Selected/focus ripple color when t
he input is not checked | `--dark-primary-color` |
| 32 `--paper-toggle-button-checked-bar-color` | Slider button color when the input i
s checked | `--google-green-500` |
| 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` |
| 35 |
| 36 @group Paper Elements |
| 37 @element paper-toggle-button |
| 38 @hero hero.svg |
| 39 @demo demo/index.html |
| 40 --> |
| 41 |
| 42 <dom-module id="paper-toggle-button"> |
| 43 |
| 44 <link rel="import" type="css" href="paper-toggle-button.css"> |
| 45 |
| 46 <template> |
| 47 |
| 48 <div id="toggleContainer"> |
| 49 <div id="toggleBar" class="toggle-bar"></div> |
| 50 <div id="toggleButton" class="toggle-button"> |
| 51 <paper-ripple id="ink" class="toggle-ink circle" recenters></paper-rippl
e> |
| 52 </div> |
| 53 </div> |
| 54 |
| 55 </template> |
| 56 |
| 57 <script> |
| 58 Polymer({ |
| 59 is: 'paper-toggle-button', |
| 60 |
| 61 behaviors: [ |
| 62 Polymer.PaperInkyFocusBehavior |
| 63 ], |
| 64 |
| 65 hostAttributes: { |
| 66 role: 'button', |
| 67 'aria-pressed': 'false', |
| 68 tabindex: 0 |
| 69 }, |
| 70 |
| 71 properties: { |
| 72 /** |
| 73 * Fired when the checked state changes due to user interaction. |
| 74 * |
| 75 * @event change |
| 76 */ |
| 77 /** |
| 78 * Fired when the checked state changes. |
| 79 * |
| 80 * @event iron-change |
| 81 */ |
| 82 /** |
| 83 * Gets or sets the state, `true` is checked and `false` is unchecked. |
| 84 * |
| 85 * @attribute checked |
| 86 * @type boolean |
| 87 * @default false |
| 88 */ |
| 89 checked: { |
| 90 type: Boolean, |
| 91 value: false, |
| 92 reflectToAttribute: true, |
| 93 notify: true, |
| 94 observer: '_checkedChanged' |
| 95 }, |
| 96 |
| 97 /** |
| 98 * If true, the button toggles the active state with each tap or press |
| 99 * of the spacebar. |
| 100 * |
| 101 * @attribute toggles |
| 102 * @type boolean |
| 103 * @default true |
| 104 */ |
| 105 toggles: { |
| 106 type: Boolean, |
| 107 value: true, |
| 108 reflectToAttribute: true |
| 109 } |
| 110 }, |
| 111 |
| 112 listeners: { |
| 113 track: '_ontrack' |
| 114 }, |
| 115 |
| 116 ready: function() { |
| 117 this._isReady = true; |
| 118 }, |
| 119 |
| 120 // button-behavior hook |
| 121 _buttonStateChanged: function() { |
| 122 if (this.disabled) { |
| 123 return; |
| 124 } |
| 125 if (this._isReady) { |
| 126 this.checked = this.active; |
| 127 } |
| 128 }, |
| 129 |
| 130 _checkedChanged: function(checked) { |
| 131 this.active = this.checked; |
| 132 this.fire('iron-change'); |
| 133 }, |
| 134 |
| 135 _ontrack: function(event) { |
| 136 var track = event.detail; |
| 137 if (track.state === 'start') { |
| 138 this._trackStart(track); |
| 139 } else if (track.state === 'track') { |
| 140 this._trackMove(track); |
| 141 } else if (track.state === 'end') { |
| 142 this._trackEnd(track); |
| 143 } |
| 144 }, |
| 145 |
| 146 _trackStart: function(track) { |
| 147 this._width = this.$.toggleBar.offsetWidth / 2; |
| 148 /* |
| 149 * keep an track-only check state to keep the dragging behavior smooth |
| 150 * while toggling activations |
| 151 */ |
| 152 this._trackChecked = this.checked; |
| 153 this.$.toggleButton.classList.add('dragging'); |
| 154 }, |
| 155 |
| 156 _trackMove: function(track) { |
| 157 var dx = track.dx; |
| 158 this._x = Math.min(this._width, |
| 159 Math.max(0, this._trackChecked ? this._width + dx : dx)); |
| 160 this.translate3d(this._x + 'px', 0, 0, this.$.toggleButton); |
| 161 this._userActivate(this._x > (this._width / 2)); |
| 162 }, |
| 163 |
| 164 _trackEnd: function(track) { |
| 165 this.$.toggleButton.classList.remove('dragging'); |
| 166 this.transform('', this.$.toggleButton); |
| 167 } |
| 168 |
| 169 }); |
| 170 </script> |
| 171 |
| 172 </dom-module> |
| OLD | NEW |