-
Notifications
You must be signed in to change notification settings - Fork 18
/
paper-chip.html
459 lines (434 loc) · 13.3 KB
/
paper-chip.html
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-behaviors/iron-button-state.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../iron-icons/iron-icons.html">
<link rel="import" href="../iron-icon/iron-icon.html">
<link rel="import" href="../paper-material/paper-material.html">
<link rel="import" href="../paper-styles/paper-styles.html">
<!--
Material Design: [Chips](http://www.google.com/design/spec/components/chips.html)
`paper-chip` is a small element that represents a complex entity such as a
calendar event or a contact. The chip can be in an opened or closed state. In
its closed state, it contains an icon and a short title. In its open state, the
chip expands to show more detail about the represented entity, as well as
(optionally) a "remove" button that removes the chip element from the DOM.
The child elements with the following slots be placed within the paper-chip tag
to define its content:
* icon: The icon representing the chip. This may be any element, for
example, a `<iron-icon>` or a simple `<span>` with a single letter
* label: The chip label, shown in both closed and opened states
* caption: A secondary label, shown only in the opened state
* all other content will be shown in the opened chip
The `removable` attribute can be used to add a button which removes the chip
from the DOM.
## Examples:
Removable chip with iron-icon
<paper-chip removable>
<iron-icon slot="icon" icon="avatars:avatar-1"></iron-icon>
<div slot="label">John Doe</div>
<div slot="caption">[email protected]</div>
</paper-chip>
Basic chip with single letter instead of an icon
<paper-chip label="Jane Doe">
<div slot="icon">J</div>
<div slot="label">[email protected]</div>
</paper-chip>
@element paper-chip
@blurb A basic "chip" element representing an icon/image and a short piece of text.
@homepage http://bendavis78.github.io/paper-chip/
@demo demo/index.html
-->
<dom-module id="paper-chip">
<template>
<style>
:host {
display: inline-block;
vertical-align: top;
position: relative;
outline: none;
font-size: 14px;
cursor: default;
margin: 8px 0;
height: 32px;
overflow: visible;
@apply --paper-chip;
}
:host([animated]) *,
:host([animated]) ::slotted(*) {
transition: 200ms ease-in;
}
#main, #chip {
border-radius: 16px;
}
#main {
background-color: var(--paper-chip-background-color, --paper-grey-200);
position: relative;
color: var(--paper-chip-secondary-text-color, --secondary-text-color);
@apply --layout-vertical;
}
#chip {
box-sizing: border-box;
height: 32px;
@apply --layout-horizontal;
@apply --layout-center;
}
paper-material {
border-radius: 16px;
}
#icon {
@apply --layout-horizontal;
@apply --layout-center;
@apply --layout-center-justified;
}
#icon ::slotted([slot=icon]) {
margin-right: -4px;
width: 32px;
height: 32px;
line-height: 32px;
border-radius: 100%;
overflow: hidden;
text-align: center;
vertical-align: middle;
font-size: 16px;
font-weight: bold;
background-color: var(--paper-chip-icon-background-color, --paper-grey-500);
color: var(--paper-chip-icon-text-color, --text-primary-color);
@apply --layout-flex;
}
#icon ::slotted(iron-icon.icon),
#icon ::slotted(iron-icon.icon svg) { /* FIXME: only top-level selectors allowed */
width: 32px;
height: 32px;
}
#icon ::slotted(iron-icon.icon) {
vertical-align: bottom;
}
#label {
padding: 0 4px 0 12px;
@apply --layout-flex-auto;
@apply --layout-self-center;
}
:host([single-line]:not([removable])) #label {
padding-right: 12px;
}
#label ::slotted([slot=label]),
#label ::slotted([slot=caption]) {
display: block;
white-space: nowrap;
margin: 0;
font-weight: normal;
font-size: 14px;
}
#label ::slotted([slot=label]) {
@apply --paper-chip-label;
}
#label ::slotted([slot=caption]) {
@apply --paper-chip-caption;
}
.icon-btn-wrapper {
@apply --layout-self-center;
}
#removeBtn {
position: relative;
margin: 0 8px 0 4px;
padding: 2px;
width: 12px;
height: 12px;
border-radius: 100%;
background-color: var(--paper-chip-removebtn-background-color, --paper-grey-400);
color: var(--paper-chip-removebtn-icon-color, --text-primary-color);
cursor: pointer;
@apply --paper-chip-removebtn;
}
#removeBtn iron-icon {
width: 12px;
height: 12px;
display: block;
@apply --paper-chip-removebtn-icon;
}
:host(:not([removable])) #removeBtn {
display: none;
}
/* pressed state */
:host([pressed]) #main {
background-color: var(--paper-chip-pressed-background-color, --paper-grey-300);
}
/* initially hidden elements */
:host(:not([opened])) #label ::slotted([slot=caption]) {
color: var(--paper-chip-secondary-text-color, --secondary-text-color);
font-size: 0;
height: 0;
}
:host(:not([persist-remove-button]):not([single-line]):not([opened])) #removeBtn,
:host(:not([persist-remove-button]):not([single-line]):not([opened])) #removeBtn .icon {
width: 0;
height: 0;
margin: 0;
padding: 0;
}
:host(:not([persist-remove-button]):not([single-line]):not([opened])) #label {
padding-right: 12px;
}
:host(:not([opened])) #content {
width: 0;
height: 0;
min-width: 100%;
overflow: hidden;
}
:host(:not([opened])) #label ::slotted([slot=label]) {
color: var(--paper-chip-secondary-text-color, --secondary-text-color);
}
/* opened state */
:host([opened]) #main, :host([opened]) #chip {
border-radius: 0;
}
:host([opened]) #content {
height: auto;
width: auto;
min-width: 100%;
overflow: hidden;
@apply --paper-chip-content;
}
:host([opened]) #chip {
height: 72px;
}
:host([opened]) #chip,
:host([opened]) #content ::slotted(*) {
padding: 16px 12px;
background-color: var(--paper-chip-opened-background-color, --paper-grey-50);
}
:host([opened]) #label ::slotted([slot=label]) {
color: var(--paper-chip-primary-text-color, --primary-text-color);
font-size: 16px;
@apply --paper-chip-label-opened;
}
:host([opened]) paper-material {
border-radius: 0;
}
:host([opened]) #icon ::slotted([slot=icon]),
:host([opened]) #icon ::slotted(iron-icon.icon svg) { /* FIXME: only top-level selectors allowed)
font-size: 20px;
width: 40px;
height: 40px;
line-height: 40px;
}
:host([opened]) #removeBtn {
padding: 4px;
margin-left: 20px;
width: 16px;
height: 16px;
}
:host([opened]) #removeBtn iron-icon {
width: 16px;
height: 16px;
}
/* open + active state */
:host([opened][active]) #chip,
:host([opened][active]) ::slotted {
background-color: var(--paper-chip-active-color, --accent-color);
color: var(--paper-chip-active-text-color, --text-primary-color);
}
:host([opened][active]) #removeBtn {
color: var(--paper-chip-active-color, --accent-color);
background-color: var(--paper-chip-active-text-color, --text-primary-color);
}
:host([opened][active]) #chip #label ::slotted([slot=label]) {
color: var(--paper-chip-active-text-color, --text-primary-color);
}
:host([opened][active]) #chip #label ::slotted(h2) {
color: var(--paper-chip-active-secondary-text-color, --text-primary-color);
}
</style>
<paper-material id="shadow" elevation="{{_elevation}}" animated$="{{animated}}">
<div id="main">
<div id="chip">
<div id="icon">
<slot name="icon"></slot>
</div>
<div id="label">
<slot name="label"></slot>
<slot name="caption"></slot>
</div>
<div id="removeBtn" on-tap="remove" aria-label="remove button">
<iron-icon icon="close" class="icon"></iron-icon>
</div>
</div>
<div id="content">
<slot></slot>
</div>
</div>
</paper-material>
</template>
<script>
Polymer({
is: 'paper-chip',
behaviors: [
Polymer.IronButtonState,
Polymer.IronControlState,
],
properties: {
/**
* Whether or not the chip is removable. If `true`, a remove button will
* be shown.
*
* @attribute removable
* @type boolean
* @default false
*/
removable: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* Whether or not the chip contains additional content. Single-line chips do not open.
*/
singleLine: {
type: Boolean,
value: false,
reflectToAttribute: true,
observer: '_singleLineChanged',
},
/**
* Always show remove button
*/
persistRemoveButton: {
type: Boolean,
value: false,
reflectToAttribute: true,
observer: '_singleLineChanged'
},
/**
* Whether or not the chip uses an animated transition between opened and
* closed states
*
* @attribute animated
* @type boolean
* @default true
*/
animated: {
type: Boolean,
value: false,
reflectToAttribute: true
},
/**
* Whether or not the chip is in its opened state.
*
* @attribute opened
* @type boolean
* @default false
*/
opened: {
type: Boolean,
value: false,
reflectToAttribute: true,
notify: true,
observer: '_openedChanged'
},
/**
* Whether or not the chip state is "active". If `true`, the main chip
* area will be highlighted when in the opened state.
*
* @attribute active
* @type boolean
* @default false
*/
active: {
type: Boolean,
value: false,
notify: true,
reflectToAttribute: true
},
_elevation: {
type: Number,
computed: '_computeElevation(opened, focused, disabled, active, pressed)'
},
_keyTarget: {
type: Object,
value: function() {
return this;
}
},
},
listeners: {
tap: '_onTap',
blur: '_onBlur'
},
hostAttributes: {
tabindex: '0'
},
ready: function() {
if (this.$.removeBtn) {
// disable tabindex on remove button so that tabindex can be used for chips
this.$.removeBtn.removeAttribute('tabindex');
}
},
_computeElevation: function(opened, focused, pressed) {
if (focused || pressed) {
return 1;
}
if (opened) {
return 4;
}
return 0;
},
_onTap: function() {
this.toggleOpened();
},
_onBlur: function() {
this.opened = false;
},
/**
* Fired before the element is removed. This event is cancelable.
*
* @event remove
*/
remove: function(event) {
event.stopPropagation();
var e = this.fire('remove', {}, {bubbles: false, cancelable: true});
if (!e.defaultPrevented) {
this.parentNode.removeChild(this);
}
},
toggleOpened: function() {
if (!this.singleLine) {
this.opened = !this.opened;
}
},
_singleLineChanged: function(singleLine) {
if (singleLine && this.opened) {
this.opened = false;
}
},
_openedChanged: function(opened) {
if (opened && this.singleLine) {
// single-line chips don't open
this.opened = false;
return;
}
if (this.animated) {
var $content, width, height;
$content = this.$.content;
width = height = '';
if (this.opened) {
// temporarily disable transitions in order to take measurements of
// the content area, allowing for a proper css transision.
this.animated = false;
this.opened = true;
width = $content.offsetWidth + 'px';
height = $content.offsetHeight + 'px';
this.opened = false;
this._forceReflow();
this.opened = true;
this.animated = true;
}
$content.style.width = width;
$content.style.height = height;
}
},
_forceReflow: function() {
return this.offsetHeight;
}
});
</script>
</dom-module>