-
Notifications
You must be signed in to change notification settings - Fork 7
/
toggle-icon.js
291 lines (241 loc) · 8.08 KB
/
toggle-icon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
Copyright (c) 2016-17 Felix Edelmann.
The project is distributed under the MIT License, so you can use or modify the code as you like, you only need to name the author (e. g. by adding a link to the GitHub repository).
Source code: https://github.com/fxedel/toggle-icon
Component page / demo: http://fxedel.github.io/toggle-icon/
*/
/**
An extremly powerful and customizable switch that looks like a paper-icon-button. Use `toggleIcon.checked` to get the toggle-icon's status in JavaScript.
The very basic usage without user feedback to show whether the switch is checked or not only needs the `icon` (or `icon-src`) property:
<toggle-icon icon="thumb-up"></toggle-icon>
Set `animation` to a space-seperated list of supported animations, which currently are `flip-vertical`, `flip-horizontal` and `rotate` (default rotation angle is 180deg, use `rotation` for other values).
<toggle-icon icon="thumb-up" animation="flip-vertical"></toggle-icon>
<toggle-icon icon="arrow-forward" animation="rotate" rotation="90"></toggle-icon>
Use `icon-checked` (or `icon-src-checked`) to change the icon while switching (there will be a smooth fading animation).
<toggle-icon icon="favorite-border" icon-checked="favorite"></toggle-icon>
### Styling
You can style the paper-icon-buttons like usual by adding (custom) properties or mixins to the following mixins:
Custom property | Description | Default
----------------|-------------|----------
`--toggle-icon` | Mixin for the toggle | `{}`
`--toggle-icon-checked` | Mixin for the toggle when checked | `{}`
`--toggle-icon-active` | _Used for backwards compatibility._ Same as --toggle-icon-checked | `{}`
`--toggle-icon-buttons` | Mixin for the used paper-icon-buttons | `{}`
Custom colors and backgrounds do animate if they change in checked state by using the CSS transition attribute.
***Example:** This will add a green phone that rotates and turns red on click.
<style is="custom-style">
.green-red {
--toggle-icon: {
color: var(--paper-green-500);
};
--toggle-icon-checked: {
color: var(--paper-red-500);
};
}
</style>
<toggle-icon
icon="communication:call"
animation="rotate"
rotation="135"
class="green-red">
</toggle-icon>
@element toggle-icon
@demo demo/index.html
*/
import '@webcomponents/shadycss/entrypoints/apply-shim.js';
import { PolymerElement } from '@polymer/polymer/polymer-element.js';
import {mixinBehaviors} from '@polymer/polymer/lib/legacy/class.js';
import { html } from '@polymer/polymer/lib/utils/html-tag.js';
import { IronCheckedElementBehavior } from '@polymer/iron-checked-element-behavior/iron-checked-element-behavior.js';
import '@polymer/paper-icon-button/paper-icon-button.js';
class ToggleIcon extends mixinBehaviors(IronCheckedElementBehavior, PolymerElement) {
static get is() {
return 'toggle-icon';
}
static get template() {
return html`
<style>
:host {
display: inline-block;
position: relative;
transition: all .3s;
@apply --toggle-icon;
}
:host([checked]) {
@apply --toggle-icon-checked;
@apply --toggle-icon-active;
}
#unchecked, #checked {
transition: opacity .3s;
@apply --toggle-icon-buttons;
}
#checked {
position: absolute;
left: 0;
top: 0;
}
:host(:not([checked])) #checked,
:host([checked]) #unchecked {
opacity: 0;
pointer-events: none;
}
</style>
<paper-icon-button
id="unchecked"
icon="{{icon}}"
src="{{iconSrc}}"
disabled\$="{{disabled}}">
</paper-icon-button>
<paper-icon-button
id="checked"
icon="{{_computeIconChecked(icon, iconChecked, iconActive)}}"
src="{{_computeIconSrcChecked(iconSrc, iconSrcChecked, iconSrcActive)}}"
disabled\$="{{disabled}}">
</paper-icon-button>`;
}
static get properties() {
return {
/**
* Changes on click, like a checkbox. Doesn't change when disabled.
*/
checked: {
type: Boolean,
value: false,
},
/**
* _Used for backwards compatibility._
* Alias for `checked`
*/
active: {
type: Boolean,
observer: '_activeChanged',
},
/**
* Specifies how to transform the content.
* Allowed modes: `flip-vertical`, `flip-horizontal`, `rotate`
*/
animation: {
type: String,
},
/**
* Specifies the degrees when `animation` contains `rotate`.
*/
rotation: {
type: Number,
value: 180,
},
/**
* The name of the icon to use. The name should be of the form: `iconset_name:icon_name` (omit the iconset name when using the default set). See [iron-icon](https://elements.polymer-project.org/elements/iron-icon).
*/
icon: {
type: String,
},
/**
* Like `icon`, but only used when checked.
*/
iconChecked: {
type: String,
value: '',
},
/**
* _Used for backwards compatibility._
* Alias for `iconChecked`
*/
iconActive: {
type: String,
value: null,
},
/**
* If using iron-icon without an iconset, you can set the src to be the URL of an individual icon image file. Note that this will take precedence over a given icon attribute. See [iron-icon](https://elements.polymer-project.org/elements/iron-icon).
*/
iconSrc: {
type: String,
},
/**
* Like `iconSrc`, but only used when checked.
*/
iconSrcChecked: {
type: String,
value: '',
},
/**
* _Used for backwards compatibility._
* Alias for `iconSrcChecked`
*/
iconSrcActive: {
type: String,
value: null,
},
/**
* Set to `true` to disable the toggle.
*/
disabled: {
type: Boolean,
value: false,
}
}
}
ready() {
super.ready();
this._addListeners()
}
_addListeners() {
this.addEventListener('click', this.toggleChecked)
this.addEventListener('iron-change', this._updateTransform);
}
/**
* Toggles `checked` if the element is not disabled. Usually called when clicked.
*/
toggleChecked() {
if (!this.disabled) {
this.checked = !this.checked;
}
}
/**
* Compute and return the checked paper-icon-button's `icon` property.
* Use `iconChecked` if present, else `iconActive` if present (_used for backwards compatibility_), else `icon`.
*/
_computeIconChecked(icon, iconChecked, iconActive) {
return iconChecked ? iconChecked : (iconActive ? iconActive : icon);
}
/**
* Compute and return the checked paper-icon-button's `src` property.
* Use `iconSrcChecked` if present, else `iconSrcActive` if present (_used for backwards compatibility_), else `iconSrc`.
*/
_computeIconSrcChecked(iconSrc, iconSrcChecked, iconSrcActive) {
return iconSrcChecked ? iconSrcChecked : (iconSrcActive ? iconSrcActive : iconSrc);
}
/**
* Compute the transform property depending on the specified animations in `animation`.
*/
_computeTransform() {
if (this.animation === undefined || this.animation == null) {
return '';
}
var transform = '';
if (this.animation.indexOf('flip-vertical') > -1) {
transform += ' scale(1, -1)';
}
if (this.animation.indexOf('flip-horizontal') > -1) {
transform += ' scale(-1, 1)';
}
if (this.animation.indexOf('rotate') > -1) {
transform += ' rotate(' + this.rotation + 'deg)';
}
return transform;
}
/**
* If element is checked, apply CSS transforms (specified in `animation`).
*/
_updateTransform() {
this.transform(this.checked ? this._computeTransform() : '');
}
/**
* _Used for backwards compatibility._
* When `active` changes, set `checked` to this value
*/
_activeChanged() {
this.checked = this.active;
}
}
window.customElements.define(ToggleIcon.is, ToggleIcon);