@vaadin/component-base 23.2.15 → 23.2.16

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (70) hide show
  1. package/custom_typings/vaadin-usage-statistics.d.ts +2 -2
  2. package/index.d.ts +0 -1
  3. package/index.js +0 -1
  4. package/package.json +3 -3
  5. package/src/a11y-announcer.d.ts +1 -1
  6. package/src/a11y-announcer.js +1 -1
  7. package/src/active-mixin.d.ts +1 -1
  8. package/src/active-mixin.js +1 -1
  9. package/src/async.d.ts +3 -0
  10. package/src/async.js +2 -1
  11. package/src/browser-utils.js +7 -7
  12. package/src/controller-mixin.d.ts +1 -1
  13. package/src/controller-mixin.js +1 -1
  14. package/src/debounce.js +2 -2
  15. package/src/delegate-focus-mixin.d.ts +48 -0
  16. package/src/delegate-focus-mixin.js +228 -0
  17. package/src/delegate-state-mixin.d.ts +20 -0
  18. package/src/delegate-state-mixin.js +125 -0
  19. package/src/dir-mixin.d.ts +2 -4
  20. package/src/dir-mixin.js +17 -39
  21. package/src/dir-utils.d.ts +19 -0
  22. package/src/dir-utils.js +36 -0
  23. package/src/disabled-mixin.d.ts +1 -1
  24. package/src/disabled-mixin.js +1 -1
  25. package/src/dom-utils.d.ts +6 -1
  26. package/src/dom-utils.js +11 -1
  27. package/src/element-mixin.d.ts +1 -1
  28. package/src/element-mixin.js +11 -5
  29. package/src/focus-mixin.d.ts +1 -1
  30. package/src/focus-mixin.js +2 -2
  31. package/src/focus-trap-controller.d.ts +1 -1
  32. package/src/focus-trap-controller.js +22 -22
  33. package/src/focus-utils.d.ts +1 -1
  34. package/src/focus-utils.js +57 -57
  35. package/src/gestures.d.ts +6 -12
  36. package/src/gestures.js +6 -4
  37. package/src/iron-list-core.js +22 -44
  38. package/src/keyboard-direction-mixin.d.ts +41 -0
  39. package/src/keyboard-direction-mixin.js +192 -0
  40. package/src/keyboard-mixin.d.ts +1 -1
  41. package/src/keyboard-mixin.js +1 -1
  42. package/src/list-mixin.d.ts +57 -0
  43. package/src/list-mixin.js +354 -0
  44. package/src/media-query-controller.d.ts +1 -1
  45. package/src/media-query-controller.js +1 -1
  46. package/src/overflow-controller.d.ts +1 -1
  47. package/src/overflow-controller.js +3 -3
  48. package/src/overlay-class-mixin.d.ts +33 -0
  49. package/src/overlay-class-mixin.js +79 -0
  50. package/src/polylit-mixin.d.ts +3 -3
  51. package/src/polylit-mixin.js +9 -4
  52. package/src/resize-mixin.d.ts +1 -1
  53. package/src/resize-mixin.js +11 -21
  54. package/src/slot-child-observe-controller.d.ts +28 -0
  55. package/src/slot-child-observe-controller.js +176 -0
  56. package/src/slot-controller.d.ts +33 -5
  57. package/src/slot-controller.js +103 -40
  58. package/src/tabindex-mixin.d.ts +1 -1
  59. package/src/tabindex-mixin.js +1 -1
  60. package/src/templates.js +1 -1
  61. package/src/tooltip-controller.d.ts +86 -0
  62. package/src/tooltip-controller.js +130 -0
  63. package/src/unique-id-utils.d.ts +1 -1
  64. package/src/unique-id-utils.js +1 -1
  65. package/src/virtualizer-iron-list-adapter.js +56 -2
  66. package/src/virtualizer.js +18 -18
  67. package/src/dir-helper.d.ts +0 -42
  68. package/src/dir-helper.js +0 -93
  69. package/src/slot-mixin.d.ts +0 -18
  70. package/src/slot-mixin.js +0 -60
@@ -10,7 +10,7 @@
10
10
  import { animationFrame, idlePeriod, microTask } from './async.js';
11
11
  import { Debouncer, enqueueDebouncer, flush } from './debounce.js';
12
12
 
13
- const IOS = navigator.userAgent.match(/iP(?:hone|ad;(?: U;)? CPU) OS (\d+)/);
13
+ const IOS = navigator.userAgent.match(/iP(?:hone|ad;(?: U;)? CPU) OS (\d+)/u);
14
14
  const IOS_TOUCH_SCROLLING = IOS && IOS[1] >= 8;
15
15
  const DEFAULT_PHYSICAL_COUNT = 3;
16
16
 
@@ -23,6 +23,7 @@ const DEFAULT_PHYSICAL_COUNT = 3;
23
23
  * If something in the scrolling engine needs to be changed
24
24
  * for the virtualizer's purposes, override a function
25
25
  * in virtualizer-iron-list-adapter.js instead of changing it here.
26
+ * If a function on this file is no longer needed, the code can be safely deleted.
26
27
  *
27
28
  * This will allow us to keep the iron-list code here as close to
28
29
  * the original as possible.
@@ -501,9 +502,12 @@ export const ironList = {
501
502
  this._physicalIndexForKey = {};
502
503
  this._firstVisibleIndexVal = null;
503
504
  this._lastVisibleIndexVal = null;
504
- this._physicalCount = this._physicalCount || 0;
505
- this._physicalItems = this._physicalItems || [];
506
- this._physicalSizes = this._physicalSizes || [];
505
+ if (!this._physicalItems) {
506
+ this._physicalItems = [];
507
+ }
508
+ if (!this._physicalSizes) {
509
+ this._physicalSizes = [];
510
+ }
507
511
  this._physicalStart = 0;
508
512
  if (this._scrollTop > this._scrollOffset) {
509
513
  this._resetScrollPosition(0);
@@ -560,39 +564,6 @@ export const ironList = {
560
564
  return this._virtualStart + (this._physicalCount - this._physicalStart) + pidx;
561
565
  },
562
566
 
563
- /**
564
- * Updates the height for a given set of items.
565
- *
566
- * @param {!Array<number>=} itemSet
567
- */
568
- _updateMetrics(itemSet) {
569
- // Make sure we distributed all the physical items
570
- // so we can measure them.
571
- flush();
572
-
573
- let newPhysicalSize = 0;
574
- let oldPhysicalSize = 0;
575
- const prevAvgCount = this._physicalAverageCount;
576
- const prevPhysicalAvg = this._physicalAverage;
577
-
578
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
579
- this._iterateItems((pidx, vidx) => {
580
- oldPhysicalSize += this._physicalSizes[pidx];
581
- this._physicalSizes[pidx] = this._physicalItems[pidx].offsetHeight;
582
- newPhysicalSize += this._physicalSizes[pidx];
583
- this._physicalAverageCount += this._physicalSizes[pidx] ? 1 : 0;
584
- }, itemSet);
585
-
586
- this._physicalSize = this._physicalSize + newPhysicalSize - oldPhysicalSize;
587
-
588
- // Update the average if it measured something.
589
- if (this._physicalAverageCount !== prevAvgCount) {
590
- this._physicalAverage = Math.round(
591
- (prevPhysicalAvg * prevAvgCount + newPhysicalSize) / this._physicalAverageCount,
592
- );
593
- }
594
- },
595
-
596
567
  /**
597
568
  * Updates the position of the physical items.
598
569
  */
@@ -645,16 +616,21 @@ export const ironList = {
645
616
  * @param {boolean=} forceUpdate If true, updates the height no matter what.
646
617
  */
647
618
  _updateScrollerSize(forceUpdate) {
648
- this._estScrollHeight =
619
+ const estScrollHeight =
649
620
  this._physicalBottom +
650
621
  Math.max(this._virtualCount - this._physicalCount - this._virtualStart, 0) * this._physicalAverage;
651
622
 
652
- forceUpdate = forceUpdate || this._scrollHeight === 0;
653
- forceUpdate = forceUpdate || this._scrollPosition >= this._estScrollHeight - this._physicalSize;
623
+ this._estScrollHeight = estScrollHeight;
624
+
654
625
  // Amortize height adjustment, so it won't trigger large repaints too often.
655
- if (forceUpdate || Math.abs(this._estScrollHeight - this._scrollHeight) >= this._viewportHeight) {
656
- this.$.items.style.height = `${this._estScrollHeight}px`;
657
- this._scrollHeight = this._estScrollHeight;
626
+ if (
627
+ forceUpdate ||
628
+ this._scrollHeight === 0 ||
629
+ this._scrollPosition >= estScrollHeight - this._physicalSize ||
630
+ Math.abs(estScrollHeight - this._scrollHeight) >= this._viewportHeight
631
+ ) {
632
+ this.$.items.style.height = `${estScrollHeight}px`;
633
+ this._scrollHeight = estScrollHeight;
658
634
  }
659
635
  },
660
636
 
@@ -750,7 +726,9 @@ export const ironList = {
750
726
  },
751
727
 
752
728
  _debounce(name, cb, asyncModule) {
753
- this._debouncers = this._debouncers || {};
729
+ if (!this._debouncers) {
730
+ this._debouncers = {};
731
+ }
754
732
  this._debouncers[name] = Debouncer.debounce(this._debouncers[name], asyncModule, cb.bind(this));
755
733
  enqueueDebouncer(this._debouncers[name]);
756
734
  },
@@ -0,0 +1,41 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2022 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import type { Constructor } from '@open-wc/dedupe-mixin';
7
+ import type { KeyboardMixinClass } from './keyboard-mixin.js';
8
+
9
+ /**
10
+ * A mixin for navigating items with keyboard.
11
+ */
12
+ export declare function KeyboardDirectionMixin<T extends Constructor<HTMLElement>>(
13
+ base: T,
14
+ ): Constructor<KeyboardDirectionMixinClass> & Constructor<KeyboardMixinClass> & T;
15
+
16
+ export declare class KeyboardDirectionMixinClass {
17
+ protected readonly focused: Element | null;
18
+
19
+ protected readonly _vertical: boolean;
20
+
21
+ /**
22
+ * Returns index of the next item that satisfies the given condition,
23
+ * based on the index of the current item and a numeric increment.
24
+ */
25
+ protected _getAvailableIndex(
26
+ items: Element[],
27
+ index: number,
28
+ increment: number,
29
+ condition: (item: Element) => boolean,
30
+ ): number;
31
+
32
+ /**
33
+ * Focus the item at given index. Override this method to add custom logic.
34
+ */
35
+ protected _focus(index: number, navigating: boolean): void;
36
+
37
+ /**
38
+ * Focus the given item. Override this method to add custom logic.
39
+ */
40
+ protected _focusItem(item: Element, navigating: boolean): void;
41
+ }
@@ -0,0 +1,192 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2022 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { isElementFocused, isElementHidden } from './focus-utils.js';
7
+ import { KeyboardMixin } from './keyboard-mixin.js';
8
+
9
+ /**
10
+ * A mixin for navigating items with keyboard.
11
+ *
12
+ * @polymerMixin
13
+ * @mixes KeyboardMixin
14
+ */
15
+ export const KeyboardDirectionMixin = (superclass) =>
16
+ class KeyboardDirectionMixinClass extends KeyboardMixin(superclass) {
17
+ /**
18
+ * @return {Element | null}
19
+ * @protected
20
+ */
21
+ get focused() {
22
+ return (this._getItems() || []).find(isElementFocused);
23
+ }
24
+
25
+ /**
26
+ * @return {boolean}
27
+ * @protected
28
+ */
29
+ get _vertical() {
30
+ return true;
31
+ }
32
+
33
+ /** @protected */
34
+ focus() {
35
+ const items = this._getItems();
36
+ if (Array.isArray(items)) {
37
+ const idx = this._getAvailableIndex(items, 0, null, (item) => !isElementHidden(item));
38
+ if (idx >= 0) {
39
+ items[idx].focus();
40
+ }
41
+ }
42
+ }
43
+
44
+ /**
45
+ * Get the list of items participating in keyboard navigation.
46
+ * By default, it treats all the light DOM children as items.
47
+ * Override this method to provide custom list of elements.
48
+ *
49
+ * @return {Element[]}
50
+ * @protected
51
+ */
52
+ _getItems() {
53
+ return Array.from(this.children);
54
+ }
55
+
56
+ /**
57
+ * Override an event listener from `KeyboardMixin`.
58
+ *
59
+ * @param {!KeyboardEvent} event
60
+ * @protected
61
+ * @override
62
+ */
63
+ _onKeyDown(event) {
64
+ super._onKeyDown(event);
65
+
66
+ if (event.metaKey || event.ctrlKey) {
67
+ return;
68
+ }
69
+
70
+ const { key } = event;
71
+ const items = this._getItems() || [];
72
+ const currentIdx = items.indexOf(this.focused);
73
+
74
+ let idx;
75
+ let increment;
76
+
77
+ const isRTL = !this._vertical && this.getAttribute('dir') === 'rtl';
78
+ const dirIncrement = isRTL ? -1 : 1;
79
+
80
+ if (this.__isPrevKey(key)) {
81
+ increment = -dirIncrement;
82
+ idx = currentIdx - dirIncrement;
83
+ } else if (this.__isNextKey(key)) {
84
+ increment = dirIncrement;
85
+ idx = currentIdx + dirIncrement;
86
+ } else if (key === 'Home') {
87
+ increment = 1;
88
+ idx = 0;
89
+ } else if (key === 'End') {
90
+ increment = -1;
91
+ idx = items.length - 1;
92
+ }
93
+
94
+ idx = this._getAvailableIndex(items, idx, increment, (item) => !isElementHidden(item));
95
+
96
+ if (idx >= 0) {
97
+ event.preventDefault();
98
+ this._focus(idx, true);
99
+ }
100
+ }
101
+
102
+ /**
103
+ * @param {string} key
104
+ * @return {boolean}
105
+ * @private
106
+ */
107
+ __isPrevKey(key) {
108
+ return this._vertical ? key === 'ArrowUp' : key === 'ArrowLeft';
109
+ }
110
+
111
+ /**
112
+ * @param {string} key
113
+ * @return {boolean}
114
+ * @private
115
+ */
116
+ __isNextKey(key) {
117
+ return this._vertical ? key === 'ArrowDown' : key === 'ArrowRight';
118
+ }
119
+
120
+ /**
121
+ * Focus the item at given index. Override this method to add custom logic.
122
+ *
123
+ * @param {number} index
124
+ * @param {boolean} navigating
125
+ * @protected
126
+ */
127
+ _focus(index, navigating = false) {
128
+ const items = this._getItems();
129
+
130
+ this._focusItem(items[index], navigating);
131
+ }
132
+
133
+ /**
134
+ * Focus the given item. Override this method to add custom logic.
135
+ *
136
+ * @param {Element} item
137
+ * @param {boolean} navigating
138
+ * @protected
139
+ */
140
+ _focusItem(item) {
141
+ if (item) {
142
+ item.focus();
143
+
144
+ // Generally, the items are expected to implement `FocusMixin`
145
+ // that would set this attribute based on the `keydown` event.
146
+ // We set it manually to handle programmatic focus() calls.
147
+ item.setAttribute('focus-ring', '');
148
+ }
149
+ }
150
+
151
+ /**
152
+ * Returns index of the next item that satisfies the given condition,
153
+ * based on the index of the current item and a numeric increment.
154
+ *
155
+ * @param {Element[]} items - array of items to iterate over
156
+ * @param {number} index - index of the current item
157
+ * @param {number} increment - numeric increment, can be either 1 or -1
158
+ * @param {Function} condition - function used to check the item
159
+ * @return {number}
160
+ * @protected
161
+ */
162
+ _getAvailableIndex(items, index, increment, condition) {
163
+ const totalItems = items.length;
164
+ let idx = index;
165
+ for (let i = 0; typeof idx === 'number' && i < totalItems; i += 1, idx += increment || 1) {
166
+ if (idx < 0) {
167
+ idx = totalItems - 1;
168
+ } else if (idx >= totalItems) {
169
+ idx = 0;
170
+ }
171
+
172
+ const item = items[idx];
173
+
174
+ if (!item.hasAttribute('disabled') && this.__isMatchingItem(item, condition)) {
175
+ return idx;
176
+ }
177
+ }
178
+ return -1;
179
+ }
180
+
181
+ /**
182
+ * Returns true if the item matches condition.
183
+ *
184
+ * @param {Element} item - item to check
185
+ * @param {Function} condition - function used to check the item
186
+ * @return {number}
187
+ * @private
188
+ */
189
+ __isMatchingItem(item, condition) {
190
+ return typeof condition === 'function' ? condition(item) : true;
191
+ }
192
+ };
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 - 2022 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2023 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import type { Constructor } from '@open-wc/dedupe-mixin';
@@ -1,6 +1,6 @@
1
1
  /**
2
2
  * @license
3
- * Copyright (c) 2021 - 2022 Vaadin Ltd.
3
+ * Copyright (c) 2021 - 2023 Vaadin Ltd.
4
4
  * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
5
  */
6
6
  import { dedupingMixin } from '@polymer/polymer/lib/utils/mixin.js';
@@ -0,0 +1,57 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2017 - 2023 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import type { Constructor } from '@open-wc/dedupe-mixin';
7
+ import type { KeyboardDirectionMixinClass } from './keyboard-direction-mixin.js';
8
+ import type { KeyboardMixinClass } from './keyboard-mixin.js';
9
+
10
+ /**
11
+ * A mixin for list elements, facilitating navigation and selection of items.
12
+ */
13
+ export declare function ListMixin<T extends Constructor<HTMLElement>>(
14
+ base: T,
15
+ ): Constructor<KeyboardDirectionMixinClass> & Constructor<KeyboardMixinClass> & Constructor<ListMixinClass> & T;
16
+
17
+ export declare class ListMixinClass {
18
+ /**
19
+ * Used for mixin detection because `instanceof` does not work with mixins.
20
+ */
21
+ _hasVaadinListMixin: boolean;
22
+
23
+ /**
24
+ * If true, the user cannot interact with this element.
25
+ * When the element is disabled, the selected item is
26
+ * not updated when `selected` property is changed.
27
+ */
28
+ disabled: boolean;
29
+
30
+ /**
31
+ * The index of the item selected in the items array.
32
+ * Note: Not updated when used in `multiple` selection mode.
33
+ */
34
+ selected: number | null | undefined;
35
+
36
+ /**
37
+ * Define how items are disposed in the dom.
38
+ * Possible values are: `horizontal|vertical`.
39
+ * It also changes navigation keys from left/right to up/down.
40
+ */
41
+ orientation: 'horizontal' | 'vertical';
42
+
43
+ /**
44
+ * The list of items from which a selection can be made.
45
+ * It is populated from the elements passed to the light DOM,
46
+ * and updated dynamically when adding or removing items.
47
+ *
48
+ * The item elements must implement `Vaadin.ItemMixin`.
49
+ *
50
+ * Note: unlike `<vaadin-combo-box>`, this property is read-only,
51
+ * so if you want to provide items by iterating array of data,
52
+ * you have to use `dom-repeat` and place it to the light DOM.
53
+ */
54
+ readonly items: Element[] | undefined;
55
+
56
+ protected readonly _scrollerElement: HTMLElement;
57
+ }