-
Notifications
You must be signed in to change notification settings - Fork 17
/
vaadin-dropdown-menu.html
512 lines (444 loc) · 18.5 KB
/
vaadin-dropdown-menu.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
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<link rel="import" href="../../polymer/polymer-element.html">
<link rel="import" href="../../polymer/lib/elements/custom-style.html">
<link rel="import" href="../../vaadin-themable-mixin/vaadin-themable-mixin.html">
<link rel="import" href="../../vaadin-control-state-mixin/vaadin-control-state-mixin.html">
<link rel="import" href="../../iron-resizable-behavior/iron-resizable-behavior.html">
<link rel="import" href="../../iron-media-query/iron-media-query.html">
<link rel="import" href="../../vaadin-element-mixin/vaadin-element-mixin.html">
<link rel="import" href="vaadin-dropdown-menu-overlay.html">
<link rel="import" href="vaadin-dropdown-menu-text-field.html">
<custom-style>
<style>
@font-face {
font-family: "vaadin-dropdown-menu-icons";
src: url(data:application/font-woff;charset=utf-8;base64,d09GRgABAAAAAASEAAsAAAAABDgAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABPUy8yAAABCAAAAGAAAABgDxIGKmNtYXAAAAFoAAAAVAAAAFQXVtKHZ2FzcAAAAbwAAAAIAAAACAAAABBnbHlmAAABxAAAAHwAAAB8CohkJ2hlYWQAAAJAAAAANgAAADYOavgEaGhlYQAAAngAAAAkAAAAJAarA8ZobXR4AAACnAAAABQAAAAUCAABP2xvY2EAAAKwAAAADAAAAAwAKABSbWF4cAAAArwAAAAgAAAAIAAHABduYW1lAAAC3AAAAYYAAAGGmUoJ+3Bvc3QAAARkAAAAIAAAACAAAwAAAAMEAAGQAAUAAAKZAswAAACPApkCzAAAAesAMwEJAAAAAAAAAAAAAAAAAAAAARAAAAAAAAAAAAAAAAAAAAAAQAAA6QADwP/AAEADwABAAAAAAQAAAAAAAAAAAAAAIAAAAAAAAwAAAAMAAAAcAAEAAwAAABwAAwABAAAAHAAEADgAAAAKAAgAAgACAAEAIOkA//3//wAAAAAAIOkA//3//wAB/+MXBAADAAEAAAAAAAAAAAAAAAEAAf//AA8AAQAAAAAAAAAAAAIAADc5AQAAAAABAAAAAAAAAAAAAgAANzkBAAAAAAEAAAAAAAAAAAACAAA3OQEAAAAAAQE/AUAC6QIVABQAAAEwFx4BFxYxMDc+ATc2MTAjKgEjIgE/ISJPIiEhIk8iIUNCoEJDAhUhIk8iISEiTyIhAAEAAAABAABvL5bdXw889QALBAAAAAAA1jHaeQAAAADWMdp5AAAAAALpAhUAAAAIAAIAAAAAAAAAAQAAA8D/wAAABAAAAAAAAukAAQAAAAAAAAAAAAAAAAAAAAUEAAAAAAAAAAAAAAAAAAAABAABPwAAAAAACgAUAB4APgABAAAABQAVAAEAAAAAAAIAAAAAAAAAAAAAAAAAAAAAAAAADgCuAAEAAAAAAAEABwAAAAEAAAAAAAIABwBgAAEAAAAAAAMABwA2AAEAAAAAAAQABwB1AAEAAAAAAAUACwAVAAEAAAAAAAYABwBLAAEAAAAAAAoAGgCKAAMAAQQJAAEADgAHAAMAAQQJAAIADgBnAAMAAQQJAAMADgA9AAMAAQQJAAQADgB8AAMAAQQJAAUAFgAgAAMAAQQJAAYADgBSAAMAAQQJAAoANACkaWNvbW9vbgBpAGMAbwBtAG8AbwBuVmVyc2lvbiAxLjAAVgBlAHIAcwBpAG8AbgAgADEALgAwaWNvbW9vbgBpAGMAbwBtAG8AbwBuaWNvbW9vbgBpAGMAbwBtAG8AbwBuUmVndWxhcgBSAGUAZwB1AGwAYQByaWNvbW9vbgBpAGMAbwBtAG8AbwBuRm9udCBnZW5lcmF0ZWQgYnkgSWNvTW9vbi4ARgBvAG4AdAAgAGcAZQBuAGUAcgBhAHQAZQBkACAAYgB5ACAASQBjAG8ATQBvAG8AbgAuAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) format('woff');
font-weight: normal;
font-style: normal;
}
</style>
</custom-style>
<dom-module id="vaadin-dropdown-menu">
<template>
<style>
:host {
display: inline-block;
}
vaadin-dropdown-menu-text-field {
width: 100%;
min-width: 0;
}
:host([hidden]) {
display: none !important;
}
[part="toggle-button"] {
font-family: "vaadin-dropdown-menu-icons";
}
[part="toggle-button"]::before {
content: "\e900";
}
</style>
<vaadin-dropdown-menu-text-field
placeholder="[[placeholder]]"
label="[[label]]"
required="[[required]]"
invalid="[[invalid]]"
error-message="[[errorMessage]]"
readonly$="[[readonly]]"
>
<slot name="prefix" slot="prefix"></slot>
<div part="value"></div>
<div part="toggle-button" slot="suffix" role="button" aria-label="Toggle"></div>
</vaadin-dropdown-menu-text-field>
<vaadin-dropdown-menu-overlay opened="{{opened}}" with-backdrop="[[_phone]]" phone$="[[_phone]]"></vaadin-dropdown-menu-overlay>
<iron-media-query
query="[[_phoneMediaQuery]]"
query-matches="{{_phone}}">
</iron-media-query>
</template>
<script>
(function() {
/**
* `<vaadin-dropdown-menu>` is a Web Component for selecting values from a list of items.
*
* ```
* <vaadin-dropdown-menu>
* <template>
* <vaadin-list-box>
* <vaadin-item label="foo">Foo</vaadin-item>
* <vaadin-item>Bar</vaadin-item>
* <vaadin-item>Baz</vaadin-item>
* </vaadin-list-box>
* </template>
* </vaadin-dropdown-menu>
* ```
*
* Hint: By setting the `label` property of inner vaadin-items you will
* be able to change the visual representation of the selected value in the input part.
*
* ### Styling
*
* The following shadow DOM parts are available for styling:
*
* Part name | Description
* ----------------|----------------
* `toggle-button` | The toggle button
*
* The following state attributes are available for styling:
*
* Attribute | Description | Part name
* -------------|-------------|------------
* `opened` | Set when the dropdown menu is open | :host
* `invalid` | Set when the element is invalid | :host
* `focused` | Set when the element is focused | :host
* `focus-ring` | Set when the element is keyboard focused | :host
* `readonly` | Set when the dropdown menu is read only | :host
*
* `<vaadin-dropdown-menu>` element sets these custom CSS properties:
*
* Property name | Description | Theme for element
* --- | --- | ---
* `--vaadin-dropdown-menu-text-field-width` | Width of the menu text field | `vaadin-dropdown-menu-overlay`
*
* See [ThemableMixin – how to apply styles for shadow parts](https://github.com/vaadin/vaadin-themable-mixin/wiki)
*
* @memberof Vaadin
* @mixes Vaadin.ElementMixin
* @mixes Vaadin.ControlStateMixin
* @mixes Vaadin.ThemableMixin
* @demo demo/index.html
*/
class DropdownMenuElement extends
Vaadin.ElementMixin(
Vaadin.ControlStateMixin(
Vaadin.ThemableMixin(
Polymer.mixinBehaviors(Polymer.IronResizableBehavior, Polymer.Element)))) {
static get is() {
return 'vaadin-dropdown-menu';
}
static get version() {
return '1.1.0-beta2';
}
static get properties() {
return {
/**
* Set when the dropdown menu is open
*/
opened: {
type: Boolean,
value: false,
notify: true,
reflectToAttribute: true,
observer: '_openedChanged'
},
/**
* The error message to display when the dropdown menu value is invalid
*/
errorMessage: {
type: String,
value: ''
},
/**
* String used for the label element.
*/
label: {
type: String
},
/**
* It stores the the `value` property of the selected item, providing the
* value for iron-form.
* When there’s an item selected, it's the value of that item, otherwise
* it's an empty string.
* On change or initialization, the component finds the item which matches the
* value and displays it.
* If no value is provided to the component, it selects the first item without
* value or empty value.
* Hint: If you do not want to select any item by default, you can either set all
* the values of inner vaadin-items, or set the vaadin-dropdown-menu value to
* an inexistent value in the items list.
*/
value: {
type: String,
value: '',
notify: true,
observer: '_valueChanged'
},
/**
* The current required state of the dropdown menu. True if required.
*/
required: {
type: Boolean,
reflectToAttribute: true,
observer: '_requiredChanged'
},
/**
* Set to true if the value is invalid.
*/
invalid: {
type: Boolean,
reflectToAttribute: true,
notify: true,
value: false
},
/**
* The name of this element.
*/
name: {
type: String,
reflectToAttribute: true
},
/**
* A hint to the user of what can be entered in the control.
* The placeholder will be displayed in the case that there
* is no item selected, or the selected item has an empty
* string label, or the selected item has no label and it's
* DOM content is empty.
*/
placeholder: {
type: String
},
/**
* When present, it specifies that the element is read-only.
*/
readonly: {
type: Boolean,
value: false,
reflectToAttribute: true
},
_phone: Boolean,
_phoneMediaQuery: {
value: '(max-width: 420px), (max-height: 420px)'
},
_overlayElement: Object,
_inputElement: Object,
_toggleElement: Object,
_items: Object
};
}
static get observers() {
return [
'_updateSelectedItem(value, _items)',
'_updateAriaExpanded(opened, _toggleElement)'
];
}
/** @private */
constructor() {
super();
this._boundSetPosition = this._setPosition.bind(this);
}
/** @private */
connectedCallback() {
super.connectedCallback();
this.addEventListener('iron-resize', this._boundSetPosition);
}
ready() {
super.ready();
this._overlayElement = this.shadowRoot.querySelector('vaadin-dropdown-menu-overlay');
this._valueElement = this.shadowRoot.querySelector('[part="value"]');
this._toggleElement = this.shadowRoot.querySelector('[part="toggle-button"]');
this._nativeInput = this.focusElement.shadowRoot.querySelector('input');
this._nativeInput.setAttribute('aria-hidden', true);
this._nativeInput.setAttribute('tabindex', -1);
this._nativeInput.style.pointerEvents = 'none';
this.focusElement.addEventListener('click', e => this.opened = !this.readonly);
this.focusElement.addEventListener('keydown', e => this._onKeyDown(e));
const template = this.querySelector('template');
this._overlayElement.template = template;
if (this._overlayElement.content) {
const origForwardHostProp = this._overlayElement._instance && this._overlayElement._instance.forwardHostProp;
this._overlayElement._instance.forwardHostProp = (...args) => {
origForwardHostProp.apply(this._overlayElement._instance, args);
setTimeout(() => {
this._updateValueSlot();
});
};
this._menuElement = Array.from(this._overlayElement.content.children).filter(element => element.localName !== 'style')[0];
if (this._menuElement) {
this._menuElement.addEventListener('items-changed', e => {
this._items = this._menuElement.items;
});
this._menuElement.addEventListener('selected-changed', () => this._updateValueSlot());
this._menuElement.addEventListener('keydown', e => this._onKeyDownInside(e));
this._menuElement.addEventListener('click', e => this.opened = false);
}
}
}
/** @protected */
get focusElement() {
return this._inputElement ||
(this._inputElement = this.shadowRoot.querySelector('vaadin-dropdown-menu-text-field'));
}
/** @private */
disconnectedCallback() {
super.disconnectedCallback();
this.removeEventListener('iron-resize', this._boundSetPosition);
// Making sure the dropdown is closed and removed from DOM after detaching the dropdown.
this.opened = false;
}
/** @private */
notifyResize() {
super.notifyResize();
if (this.positionTarget && this.opened) {
this._setPosition();
// Schedule another position update (to cover virtual keyboard opening for example)
requestAnimationFrame(this._setPosition.bind(this));
}
}
_requiredChanged(required) {
this.setAttribute('aria-required', required);
}
_valueChanged(value, oldValue) {
if (value === '') {
this.focusElement.removeAttribute('has-value');
} else {
this.focusElement.setAttribute('has-value', '');
}
// Skip validation for the initial empty string value
if (value === '' && oldValue === undefined) {
return;
}
this.validate();
}
_onKeyDown(e) {
if (!this.readonly && !this.opened && /^(ArrowDown|Down|ArrowUp|Up|Enter|SpaceBar| )$/.test(e.key)) {
e.preventDefault();
this.opened = true;
}
}
_onKeyDownInside(e) {
if (/^(Tab)$/.test(e.key)) {
this.opened = false;
}
}
_openedChanged(opened, wasOpened) {
if (
!this._overlayElement ||
!this._menuElement ||
!this._toggleElement ||
!this.focusElement ||
this.disabled ||
this.readonly
) {
this.opened = false;
return;
}
if (opened) {
this._openedWithFocusRing = this.hasAttribute('focus-ring') || this.focusElement.hasAttribute('focus-ring');
this._menuElement.focus();
this._setPosition();
window.addEventListener('scroll', this._boundSetPosition, true);
} else if (wasOpened) {
if (this._phone) {
this._setFocused(false);
} else {
this.focusElement.focus();
if (this._openedWithFocusRing) {
this.focusElement.setAttribute('focus-ring', '');
}
}
this.validate();
window.removeEventListener('scroll', this._boundSetPosition, true);
}
}
_hasContent(selected) {
if (!selected) {
return false;
}
return Boolean(
selected.hasAttribute('label') ?
selected.getAttribute('label') :
selected.textContent.trim() || selected.children.length
);
}
_attachSelectedItem(selected) {
if (!selected) {
return;
}
let labelItem;
if (selected.hasAttribute('label')) {
labelItem = document.createElement('vaadin-item');
labelItem.textContent = selected.getAttribute('label');
} else {
labelItem = selected.cloneNode(true);
}
labelItem.removeAttribute('tabindex');
this._valueElement.appendChild(labelItem);
labelItem.selected = true;
}
_updateAriaExpanded(opened, toggleElement) {
toggleElement && toggleElement.setAttribute('aria-expanded', opened);
}
_updateValueSlot() {
this.opened = false;
this._valueElement.innerHTML = '';
const selected = this._items[this._menuElement.selected];
const hasContent = this._hasContent(selected);
// Toggle visibility of _valueElement vs fallback input with placeholder
this._valueElement.slot = hasContent ? 'value' : '';
// Ensure the slot distribution to apply correct style scope for cloned item
if (hasContent && window.ShadyDOM) {
window.ShadyDOM.flush();
}
this._attachSelectedItem(selected);
if (!this._valueChanging && selected) {
this._selectedChanging = true;
this.value = selected.value || '';
delete this._selectedChanging;
}
}
_updateSelectedItem(value, items) {
if (items) {
this._menuElement.selected = items.reduce((prev, item, idx) => {
return prev === undefined && item.value === value ? idx : prev;
}, undefined);
if (!this._selectedChanging) {
this._valueChanging = true;
this._updateValueSlot();
delete this._valueChanging;
}
}
}
/** @override */
_setFocused(focused) {
// Keep `focused` state when opening the overlay for styling purpose.
super._setFocused(this.opened || focused);
this.focusElement._setFocused(this.hasAttribute('focused'));
!this.hasAttribute('focused') && this.validate();
}
_setPosition() {
const inputRect = this._inputElement.shadowRoot.querySelector('[part~="input-field"]').getBoundingClientRect();
const viewportHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
const bottomAlign = inputRect.top > (viewportHeight - inputRect.height) / 2;
this._overlayElement.style.left = inputRect.left + 'px';
if (bottomAlign) {
this._overlayElement.setAttribute('bottom-aligned', '');
this._overlayElement.style.removeProperty('top');
this._overlayElement.style.bottom = (viewportHeight - inputRect.bottom) + 'px';
} else {
this._overlayElement.removeAttribute('bottom-aligned');
this._overlayElement.style.removeProperty('bottom');
this._overlayElement.style.top = inputRect.top + 'px';
}
this._overlayElement.updateStyles({'--vaadin-dropdown-menu-text-field-width': inputRect.width + 'px'});
}
/**
* Returns true if `value` is valid, and sets the `invalid` flag appropriately.
*
* @return {boolean} True if the value is valid and sets the `invalid` flag appropriately
*/
validate() {
return !(this.invalid = !(this.disabled || !this.required || this.value));
}
}
customElements.define(DropdownMenuElement.is, DropdownMenuElement);
/**
* @namespace Vaadin
*/
window.Vaadin.DropdownMenuElement = DropdownMenuElement;
})();
</script>
</dom-module>