-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathpaper-chip.html
711 lines (556 loc) · 21.9 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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
<link rel="import" href="../polymer/polymer-element.html">
<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
<link rel="import" href="../iron-behaviors/iron-button-state.html">
<link rel="import" href="../iron-behaviors/iron-control-state.html">
<link rel="import" href="./paper-chip-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">
<link rel="import" href="../paper-styles/typography.html">
<!--
Material design: [Chips](https://material.io/guidelines/components/chips.html#).
> A chip web component made with Polymer 2 following Material Design guidelines
Polymer 2.x `<paper-chip>` may contain a icon or a photo, some line of text or a contact information with Material Design styling.
## Usage
Basic paper-chip.
```html
<paper-chip single-line>
<div slot="label" class="label">Single-line Chip</div>
</paper-chip>
```
It can show **single-line** or **multi-line** text. You can open a multi-line chip tapping on it.
```html
<paper-chip>
<iron-icon slot="icon" class="icon" icon="avatars:avatar-1"></iron-icon>
<div slot="label" class="label">John Foo</div>
<div slot="caption" class="caption">[email protected]</div>
</paper-chip>
```
Configure some basic behavior. It may be removable or not.
```html
<paper-chip removable>
<div slot="label" class="label">Removable Chip</div>
</paper-chip>
```
Choose to set animation in openable chip.
```html
<paper-chip removable animated>
<div slot="icon" class="icon">P</div>
<div slot="label" class="label">John Foo</div>
<div slot="caption" class="caption">[email protected]</div>
</paper-chip>
```
Use `<paper-chip-input>` component to collect a set of `chip`.
```html
<paper-chip-input></paper-chip-input>
```
Do you want to change the **keys** that automatically submit chips?
No worries, we support it!
Configure them with the `enter-keys` property adding the keys.
We support the [Polymer.IronA11yKeysBehavior](https://github.com/PolymerElements/iron-a11y-keys-behavior) format.
Following example should clarify the usage of this feature.
```html
<paper-chip-input enter-keys='["enter", "space"]'></paper-chip-input>
```
Do you need **autocomplete** ?
When you have a predefined and finite set of input values you can set the predefined values with `datasource` property.
```html
<paper-chip-input label="Weekdays" datasource='[{"value": "1", "label": "monday" },{"value": "2", "label": "tuesday"}, {"value": "3", "label": "wednesday"}, {"value": "4", "label": "thursday"}, {"value": "5", "label": "friday"}, {"value": "6", "label": "saturday"}, {"value": "7", "label": "sunday"}]' placeholder="Add day"></paper-chip-input>
```
The default properties for datasource are `label` and `value`, but you can configure it from outside by the properties `display-property` and `value-property`.
```html
<paper-chip-input display-property="value" value-property="key">
</paper-chip-input>
```
```json
{ "value": "apple", "key": 0 },
{ "value": "apricot", "key": 1 }
```
Autocomplete uses a **startWith** filter as default logic.
Do you need to implement a **custom filter logic**? You can do it!
Set `filter` property with your custom *function*. The following lines of code will show you how to do it.
```html
<paper-chip-input id="weekdays" label="Weekdays" datasource='[{"value": "1", "label": "monday" },{"value": "2", "label": "tuesday"}, {"value": "3", "label": "wednesday"}, {"value": "4", "label": "thursday"}, {"value": "5", "label": "friday"}, {"value": "6", "label": "saturday"}, {"value": "7", "label": "sunday"}]' placeholder="Add day"></paper-chip-input>
<script>
let weekdays = document.querySelector("#weekdays")
weekdays.filter = (data, value) => {
return data.filter((item) => item[weekdays.displayProperty].toLowerCase().includes(value.toLowerCase()))
}
</script>
```
## Styling
The following custom properties and mixins are available for styling:
Custom property | Description | Default
----------------|-------------|----------
`--paper-chip-secondary-text-color` | The paper-chip label-color |
`--paper-chip-background-color` | The paper-chip background-color |
`--paper-chip-icon-background-color` | The paper-chip avatar background-color |
`--paper-chip-icon-text-color` | The paper-chip icon color |
`--paper-chip-closed-label-max-width` | The paper-chip label max-width when is closed | 80px
## Testing
If you are using **polyserve** navigate to the `test/` directory of your element to run its tests. You can view results in browser console.
### web-component-tester
The tests are compatible with [web-component-tester](https://github.com/Polymer/web-component-tester).
Install it via:
```sh
npm install web-component-tester
```
The `wct` tool will run your tests in all the browsers you have installed. Just
run it:
```sh
wct
```
By default, any tests under `test/` will be run. You can override this by
specifying particular files (or globs of files) via `wct path/to/files`.
Otherwise you can run the test suite via npm script
```sh
npm test
```
## License
MIT © fabbricadigitale
@element paper-chip
@demo demo/index.html
-->
<dom-module id="paper-chip">
<template>
<style>
:host {
display: inline-block;
vertical-align: top;
position: relative;
outline: none;
cursor: default;
margin: 8px 0;
height: 32px;
overflow: visible;
@apply --paper-font-body1;
@apply --paper-chip;
}
:host([animated]) * {
transition: 200ms ease-in;
}
#main,
#chip {
border-radius: 16px;
}
#main {
background-color: var(--paper-chip-background-color, var(--paper-grey-200));
position: relative;
color: var(--paper-chip-secondary-text-color, var(--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, var(--paper-grey-500));
color: var(--paper-chip-icon-text-color, var(--text-primary-color));
@apply --layout-flex;
}
#icon ::slotted([class="icon"]) {
/* FIXME: only top-level selectors allowed */
width: 32px;
height: 32px;
vertical-align: bottom;
}
#label {
padding: 0 4px 0 12px;
@apply --layout-flex-auto;
@apply --layout-self-center;
}
:host(:not([removable])) #label {
padding-right: 12px;
}
:host([single-line]) #label {
padding-right: 12px;
}
:host([single-line][removable]) #label {
padding-right: 0px;
}
#label .labelFallback,
#label ::slotted([slot="label"]) {
@apply --layout-block;
margin: 0;
@apply --paper-font-body2;
color: var(--paper-chip-secondary-text-color, var(--secondary-text-color));
}
:host(:not([opened])) #label .labelFallback,
:host(:not([opened])) #label ::slotted([slot="label"]) {
@apply --paper-font-common-nowrap;
max-width: var(--paper-chip-closed-label-max-width, 80px);
}
#label ::slotted([slot="caption"]) {
display: none;
white-space: nowrap;
margin: 0;
@apply --paper-font-body1;
color: var(--paper-chip-secondary-text-color, var(--secondary-text-color));
}
:host([opened]) #label ::slotted([slot="caption"]) {
display: block;
}
:host([opened]) {
z-index: 1;
}
#label .labelFallback,
#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;
cursor: pointer;
@apply --paper-chip-removebtn;
}
#removeBtn iron-icon {
display: block;
@apply --paper-chip-removebtn-icon;
}
#removeBtn {
display: none;
}
:host([removable]) #removeBtn {
display: block;
}
/* pressed state */
:host([pressed]) #main {
background-color: var(--paper-chip-pressed-background-color, var(--paper-grey-300));
}
/* opened state */
:host([opened]) #main,
:host([opened]) #chip {
border-radius: 0;
}
#content {
width: 0;
height: 0;
min-width: 100%;
overflow: hidden;
}
: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]) #label ::slotted([slot="content"]) {
padding: 16px 12px;
background-color: var(--paper-chip-opened-background-color, var(--paper-grey-50));
}
:host([opened]) #label .labelFallback,
:host([opened]) #label ::slotted([slot="label"]) {
color: var(--paper-chip-primary-text-color, var(--primary-text-color));
font-size: 16px;
@apply --paper-chip-label-opened;
}
:host([opened]) paper-material {
border-radius: 0;
}
:host([opened]) #icon ::slotted([slot="icon"]) {
font-size: 20px;
width: 40px;
height: 40px;
line-height: 40px;
}
:host([opened]) #removeBtn {
padding: 4px;
margin-left: 16px;
}
/* open + active state */
:host([opened][active]) #chip,
:host([opened][active]) * ::slotted {
background-color: var(--paper-chip-active-color, var(--accent-color));
color: var(--paper-chip-active-text-color, var(--text-primary-color));
}
:host([opened][active]) #removeBtn {
color: var(--paper-chip-active-color, var(--accent-color));
background-color: var(--paper-chip-active-text-color, var(--text-primary-color));
}
:host([opened][removable]) iron-icon {
padding-top: 2px;
padding-left: 2px;
}
:host([opened][active]) #label ::slotted([slot="label"]) {
color: var(--paper-chip-active-text-color, var(--text-primary-color));
}
:host([opened][active]) * ::slotted(h2) {
color: var(--paper-chip-active-secondary-text-color, var(--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 id="labelSlot" name="label">
<!-- fallback -->
<div class="labelFallback">
<slot id="fallbackLabelSlot"></slot>
</div>
</slot>
<slot name="caption"></slot>
</div>
<div id="removeBtn" on-tap="_selfRemove" aria-label="remove button">
<iron-icon icon="paper-chip-icons:cancel" class="icon"></iron-icon>
</div>
</div>
<div id="content">
<slot name="content"></slot>
</div>
</div>
</paper-material>
</template>
<script>
class PaperChip extends
Polymer.mixinBehaviors(Polymer.IronControlState,
Polymer.mixinBehaviors(Polymer.IronButtonState,
Polymer.mixinBehaviors(Polymer.IronA11yKeysBehavior, Polymer.Element))) {
static get is() {
return "paper-chip"
}
static get properties() {
return {
value: {
type: String
},
index: {
type: Number
},
/**
* Whether or not the chip is removable.
* When `true`, a remove button will be shown.
*
* @attribute removable
* @type boolean
* @default false
*/
removable: {
type: Boolean,
value: false
},
/**
* Whether or not the chip contains additional content.
* Single-line chips do not open.
*/
singleLine: {
type: Boolean,
value: false,
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
},
/**
* 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"
},
_elevation: {
type: Number,
computed: "_computeElevation(opened, focused, disabled, active, pressed)"
},
hasLabel: {
type: Boolean,
readOnly: true
}
}
}
static get observers() {
return [
"_handleVisibleLabel(hasLabel, value)"
]
}
connectedCallback() {
super.connectedCallback()
this._boundOnTap = (evt) => this._onTap(evt)
this._boundOnBlur = (evt) => this._onBlur(evt)
this._boundOnSlotChange = (evt) => this._onSlotChange(evt)
this.addEventListener("tap", this._boundOnTap)
this.addEventListener("blur", this._boundOnBlur, true)
this._ensureAttribute("tabindex", 0)
// Detect whether we have a label somehow (at boot and also at runtime)
this._onSlotChange()
this.$.fallbackLabelSlot.addEventListener("slotchange", this._boundOnSlotChange)
this.$.labelSlot.addEventListener("slotchange", this._boundOnSlotChange)
}
disconnectedCallback() {
super.disconnectedCallback()
this.removeEventListener("tap", this._boundOnTap)
this.removeEventListener("blur", this._boundOnBlur, true)
}
_onSlotChange(event) {
if (event) {
if (event.target == event.currentTarget) {
switch (event.target) {
case this.$.labelSlot:
this._setHasLabel(this.$.labelSlot.assignedNodes().length > 0)
break
case this.$.fallbackLabelSlot:
this._setHasLabel(this.$.fallbackLabelSlot.assignedNodes().length > 0)
break
}
}
return
}
this._setHasLabel(this.$.labelSlot.assignedNodes().length > 0 || this.$.fallbackLabelSlot.assignedNodes().length > 0)
}
_handleVisibleLabel(hasLabel, value) {
if (!hasLabel && !!value && !this.textContent) {
// Restarts (fallback) distribution
this.textContent = value
}
}
_computeElevation(opened, focused, pressed) {
const elevationMax = 4
const elevationMin = 1
const noElevation = 0
if (focused || pressed) {
return elevationMin
}
if (opened) {
return elevationMax
}
return noElevation
}
_onTap() {
// Toggle opened status only when this element is focused
if (this.opened && document.activeElement === this) {
this.opened = false
} else {
this.opened = true
}
}
_onBlur(event) {
let relatedTarget = event.relatedTarget
if (!relatedTarget || !this.isLightDescendant(relatedTarget)) {
this.opened = false
}
}
_selfRemove(evt) {
evt.stopPropagation()
this.remove()
}
remove() {
if (!this.removable || !this.parentNode) {
return // ignore
}
const evt = new CustomEvent("remove", { cancelable: true })
this.dispatchEvent(evt)
// Remove from DOM when the event default action has not been prevented
if (!evt.defaultPrevented) {
return this.parentNode.removeChild(this)
}
}
_singleLineChanged(singleLine) {
if (singleLine && this.opened) {
this.opened = false
}
}
_openedChanged(opened) {
if (opened && this.singleLine) {
this.opened = false
return
}
if (this.animated) {
let width = ""
let height = ""
const content = this.$.content
if (this.opened) {
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() {
return this.offsetHeight
}
get behaviors() {
return []
}
get keyBindings() {
return {
"delete": "_selfRemove",
"backspace": "_selfRemove"
}
}
get label() {
var nodes = this.$.labelSlot.assignedNodes()
if (nodes.length <= 0) {
nodes = this.$.fallbackLabelSlot.assignedNodes()
}
var label = ""
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i]
if (node instanceof Text) {
label += node.nodeValue
} else if (node instanceof Element) {
label += node.innerText
}
}
return label.trim()
}
}
customElements.define(PaperChip.is, PaperChip)
</script>
</dom-module>