@vaadin/component-base 24.4.0-alpha9 → 24.4.0-beta1

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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vaadin/component-base",
3
- "version": "24.4.0-alpha9",
3
+ "version": "24.4.0-beta1",
4
4
  "publishConfig": {
5
5
  "access": "public"
6
6
  },
@@ -42,5 +42,5 @@
42
42
  "@vaadin/testing-helpers": "^0.6.0",
43
43
  "sinon": "^13.0.2"
44
44
  },
45
- "gitHead": "effb81abe3c6283a6ec620cc0cee56069af58226"
45
+ "gitHead": "504787f741d677467ae93ca7cd31d84489366a9c"
46
46
  }
package/src/define.js CHANGED
@@ -9,7 +9,7 @@ export function defineCustomElement(CustomElement) {
9
9
  if (!defined) {
10
10
  Object.defineProperty(CustomElement, 'version', {
11
11
  get() {
12
- return '24.4.0-alpha9';
12
+ return '24.4.0-beta1';
13
13
  },
14
14
  });
15
15
 
@@ -5,7 +5,11 @@
5
5
  */
6
6
 
7
7
  /**
8
- * Check if two paths can be resolved as URLs
9
- * with the same origin and pathname.
8
+ * Checks if two paths match based on their origin, pathname, and query parameters.
9
+ *
10
+ * The function matches an actual URL against an expected URL to see if they share
11
+ * the same base origin (like https://example.com), the same path (like /path/to/page),
12
+ * and if the actual URL contains at least all the query parameters with the same values
13
+ * from the expected URL.
10
14
  */
11
- export declare function matchPaths(path1: string, path2: string): boolean;
15
+ export declare function matchPaths(actual: string, expected: string): boolean;
package/src/url-utils.js CHANGED
@@ -5,15 +5,37 @@
5
5
  */
6
6
 
7
7
  /**
8
- * Check if two paths can be resolved as URLs
9
- * with the same origin and pathname.
8
+ * Checks if one set of URL parameters contains all the parameters
9
+ * with the same values from another set.
10
10
  *
11
- * @param {string} path1
12
- * @param {string} path2
11
+ * @param {URLSearchParams} actual
12
+ * @param {URLSearchParams} expected
13
13
  */
14
- export function matchPaths(path1, path2) {
14
+ function containsQueryParams(actual, expected) {
15
+ return [...expected.entries()].every(([key, value]) => {
16
+ return actual.getAll(key).includes(value);
17
+ });
18
+ }
19
+
20
+ /**
21
+ * Checks if two paths match based on their origin, pathname, and query parameters.
22
+ *
23
+ * The function matches an actual URL against an expected URL to see if they share
24
+ * the same base origin (like https://example.com), the same path (like /path/to/page),
25
+ * and if the actual URL contains at least all the query parameters with the same values
26
+ * from the expected URL.
27
+ *
28
+ * @param {string} actual The actual URL to match.
29
+ * @param {string} expected The expected URL to match.
30
+ */
31
+ export function matchPaths(actual, expected) {
15
32
  const base = document.baseURI;
16
- const url1 = new URL(path1, base);
17
- const url2 = new URL(path2, base);
18
- return url1.origin === url2.origin && url1.pathname === url2.pathname;
33
+ const actualUrl = new URL(actual, base);
34
+ const expectedUrl = new URL(expected, base);
35
+
36
+ return (
37
+ actualUrl.origin === expectedUrl.origin &&
38
+ actualUrl.pathname === expectedUrl.pathname &&
39
+ containsQueryParams(actualUrl.searchParams, expectedUrl.searchParams)
40
+ );
19
41
  }
@@ -55,6 +55,13 @@ export class IronListAdapter {
55
55
  this._scrollLineHeight = this._getScrollLineHeight();
56
56
  this.scrollTarget.addEventListener('wheel', (e) => this.__onWheel(e));
57
57
 
58
+ this.scrollTarget.addEventListener('virtualizer-element-focused', (e) => this.__onElementFocused(e));
59
+ this.elementsContainer.addEventListener('focusin', (e) => {
60
+ this.scrollTarget.dispatchEvent(
61
+ new CustomEvent('virtualizer-element-focused', { detail: { element: this.__getFocusedElement() } }),
62
+ );
63
+ });
64
+
58
65
  if (this.reorderElements) {
59
66
  // Reordering the physical elements cancels the user's grab of the scroll bar handle on Safari.
60
67
  // Need to defer reordering until the user lets go of the scroll bar handle.
@@ -82,6 +89,10 @@ export class IronListAdapter {
82
89
  return this.lastVisibleIndex + this._vidxOffset;
83
90
  }
84
91
 
92
+ get _maxVirtualIndexOffset() {
93
+ return this.size - this._virtualCount;
94
+ }
95
+
85
96
  __hasPlaceholders() {
86
97
  return this.__getVisibleElements().some((el) => el.__virtualizerPlaceholder);
87
98
  }
@@ -106,7 +117,7 @@ export class IronListAdapter {
106
117
  let targetVirtualIndex = Math.floor((index / this.size) * this._virtualCount);
107
118
  if (this._virtualCount - targetVirtualIndex < visibleElementCount) {
108
119
  targetVirtualIndex = this._virtualCount - (this.size - index);
109
- this._vidxOffset = this.size - this._virtualCount;
120
+ this._vidxOffset = this._maxVirtualIndexOffset;
110
121
  } else if (targetVirtualIndex < visibleElementCount) {
111
122
  if (index < OFFSET_ADJUST_MIN_THRESHOLD) {
112
123
  targetVirtualIndex = index;
@@ -136,7 +147,6 @@ export class IronListAdapter {
136
147
  }
137
148
 
138
149
  flush() {
139
- const startPhysicalCount = this._physicalCount;
140
150
  // The scroll target is hidden.
141
151
  if (this.scrollTarget.offsetHeight === 0) {
142
152
  return;
@@ -154,11 +164,6 @@ export class IronListAdapter {
154
164
  if (this.__debouncerWheelAnimationFrame) {
155
165
  this.__debouncerWheelAnimationFrame.flush();
156
166
  }
157
-
158
- if (this._physicalCount !== startPhysicalCount) {
159
- // Flushing again until physical count stabilizes fixes https://github.com/vaadin/flow-components/issues/5595#issuecomment-1770278913
160
- this.flush();
161
- }
162
167
  }
163
168
 
164
169
  update(startIndex = 0, endIndex = this.size - 1) {
@@ -227,6 +232,7 @@ export class IronListAdapter {
227
232
  // Clean up temporary placeholder sizing
228
233
  if (el.__virtualizerPlaceholder) {
229
234
  el.style.paddingTop = '';
235
+ el.style.opacity = '';
230
236
  el.__virtualizerPlaceholder = false;
231
237
  }
232
238
 
@@ -251,6 +257,7 @@ export class IronListAdapter {
251
257
  // Assign a temporary placeholder sizing to elements that would otherwise end up having
252
258
  // no height.
253
259
  el.style.paddingTop = `${this.__placeholderHeight}px`;
260
+ el.style.opacity = '0';
254
261
  el.__virtualizerPlaceholder = true;
255
262
 
256
263
  // Manually schedule the resize handler to make sure the placeholder padding is
@@ -297,24 +304,41 @@ export class IronListAdapter {
297
304
  this._debouncers._increasePoolIfNeeded.cancel();
298
305
  }
299
306
 
307
+ // Prevent element update while the scroll position is being restored
308
+ this.__preventElementUpdates = true;
309
+
310
+ // Record the scroll position before changing the size
311
+ let fvi; // First visible index
312
+ let fviOffsetBefore; // Scroll offset of the first visible index
313
+ if (size > 0) {
314
+ fvi = this.adjustedFirstVisibleIndex;
315
+ fviOffsetBefore = this.__getIndexScrollOffset(fvi);
316
+ }
317
+
300
318
  // Change the size
301
319
  this.__size = size;
302
320
 
303
- if (!this._physicalItems) {
304
- // Not initialized yet
305
- this._itemsChanged({
306
- path: 'items',
307
- });
308
- this.__preventElementUpdates = true;
309
- flush();
310
- this.__preventElementUpdates = false;
311
- } else {
312
- // Already initialized, just update _virtualCount
313
- this._updateScrollerSize();
314
- this._virtualCount = this.items.length;
315
- this._render();
321
+ this._itemsChanged({
322
+ path: 'items',
323
+ });
324
+ flush();
325
+
326
+ // Try to restore the scroll position if the new size is larger than 0
327
+ if (size > 0) {
328
+ fvi = Math.min(fvi, size - 1);
329
+ // Note, calling scrollToIndex also updates the virtual index offset,
330
+ // causing the virtualizer to add more items when size is increased,
331
+ // and remove exceeding items when size is decreased.
332
+ this.scrollToIndex(fvi);
333
+
334
+ const fviOffsetAfter = this.__getIndexScrollOffset(fvi);
335
+ if (fviOffsetBefore !== undefined && fviOffsetAfter !== undefined) {
336
+ this._scrollTop += fviOffsetBefore - fviOffsetAfter;
337
+ }
316
338
  }
317
339
 
340
+ this.__preventElementUpdates = false;
341
+
318
342
  // When reducing size while invisible, iron-list does not update items, so
319
343
  // their hidden state is not updated and their __lastUpdatedIndex is not
320
344
  // reset. In that case force an update here.
@@ -326,8 +350,7 @@ export class IronListAdapter {
326
350
  requestAnimationFrame(() => this._resizeHandler());
327
351
  }
328
352
 
329
- // Schedule and flush a resize handler. This will cause a
330
- // re-render for the elements.
353
+ // Schedule and flush a resize handler
331
354
  this._resizeHandler();
332
355
  flush();
333
356
  }
@@ -425,6 +448,75 @@ export class IronListAdapter {
425
448
  /** @private */
426
449
  toggleScrollListener() {}
427
450
 
451
+ /** @private */
452
+ __getFocusedElement(visibleElements = this.__getVisibleElements()) {
453
+ return visibleElements.find(
454
+ (element) =>
455
+ element.contains(this.elementsContainer.getRootNode().activeElement) ||
456
+ element.contains(this.scrollTarget.getRootNode().activeElement),
457
+ );
458
+ }
459
+
460
+ /** @private */
461
+ __nextFocusableSiblingMissing(focusedElement, visibleElements) {
462
+ return (
463
+ // Check if focused element is the last visible DOM element
464
+ visibleElements.indexOf(focusedElement) === visibleElements.length - 1 &&
465
+ // ...while there are more items available
466
+ this.size > focusedElement.__virtualIndex + 1
467
+ );
468
+ }
469
+
470
+ /** @private */
471
+ __previousFocusableSiblingMissing(focusedElement, visibleElements) {
472
+ return (
473
+ // Check if focused element is the first visible DOM element
474
+ visibleElements.indexOf(focusedElement) === 0 &&
475
+ // ...while there are preceding items available
476
+ focusedElement.__virtualIndex > 0
477
+ );
478
+ }
479
+
480
+ /** @private */
481
+ __onElementFocused(e) {
482
+ if (!this.reorderElements) {
483
+ return;
484
+ }
485
+
486
+ const focusedElement = e.detail.element;
487
+ if (!focusedElement) {
488
+ return;
489
+ }
490
+
491
+ // User has tabbed to or within a virtualizer element.
492
+ // Check if a next or previous focusable sibling is missing while it should be there (so the user can continue tabbing).
493
+ // The focusable sibling might be missing due to the elements not yet being in the correct DOM order.
494
+ // First try flushing (which also flushes any active __scrollReorderDebouncer).
495
+ const visibleElements = this.__getVisibleElements();
496
+ if (
497
+ this.__previousFocusableSiblingMissing(focusedElement, visibleElements) ||
498
+ this.__nextFocusableSiblingMissing(focusedElement, visibleElements)
499
+ ) {
500
+ this.flush();
501
+ }
502
+
503
+ // If the focusable sibling is still missing (because the focused element is at the edge of the viewport and
504
+ // the virtual scrolling logic hasn't had the need to recycle elements), scroll the virtualizer just enough to
505
+ // have the focusable sibling inside the visible viewport to force the virtualizer to recycle.
506
+ const reorderedVisibleElements = this.__getVisibleElements();
507
+ if (this.__nextFocusableSiblingMissing(focusedElement, reorderedVisibleElements)) {
508
+ this._scrollTop +=
509
+ Math.ceil(focusedElement.getBoundingClientRect().bottom) -
510
+ Math.floor(this.scrollTarget.getBoundingClientRect().bottom - 1);
511
+ this.flush();
512
+ } else if (this.__previousFocusableSiblingMissing(focusedElement, reorderedVisibleElements)) {
513
+ this._scrollTop -=
514
+ Math.ceil(this.scrollTarget.getBoundingClientRect().top + 1) -
515
+ Math.floor(focusedElement.getBoundingClientRect().top);
516
+ this.flush();
517
+ }
518
+ }
519
+
428
520
  _scrollHandler() {
429
521
  // The scroll target is hidden.
430
522
  if (this.scrollTarget.offsetHeight === 0) {
@@ -662,13 +754,7 @@ export class IronListAdapter {
662
754
 
663
755
  // Which row to use as a target?
664
756
  const visibleElements = this.__getVisibleElements();
665
-
666
- const elementWithFocus = visibleElements.find(
667
- (element) =>
668
- element.contains(this.elementsContainer.getRootNode().activeElement) ||
669
- element.contains(this.scrollTarget.getRootNode().activeElement),
670
- );
671
- const targetElement = elementWithFocus || visibleElements[0];
757
+ const targetElement = this.__getFocusedElement(visibleElements) || visibleElements[0];
672
758
  if (!targetElement) {
673
759
  // All elements are hidden, don't reorder
674
760
  return;
@@ -703,15 +789,16 @@ export class IronListAdapter {
703
789
 
704
790
  /** @private */
705
791
  _adjustVirtualIndexOffset(delta) {
792
+ const maxOffset = this._maxVirtualIndexOffset;
793
+
706
794
  if (this._virtualCount >= this.size) {
707
795
  this._vidxOffset = 0;
708
796
  } else if (this.__skipNextVirtualIndexAdjust) {
709
797
  this.__skipNextVirtualIndexAdjust = false;
710
798
  } else if (Math.abs(delta) > 10000) {
711
799
  // Process a large scroll position change
712
- const scale = this._scrollTop / (this.scrollTarget.scrollHeight - this.scrollTarget.offsetHeight);
713
- const offset = scale * this.size;
714
- this._vidxOffset = Math.round(offset - scale * this._virtualCount);
800
+ const scale = this._scrollTop / (this.scrollTarget.scrollHeight - this.scrollTarget.clientHeight);
801
+ this._vidxOffset = Math.round(scale * maxOffset);
715
802
  } else {
716
803
  // Make sure user can always swipe/wheel scroll to the start and end
717
804
  const oldOffset = this._vidxOffset;
@@ -730,7 +817,6 @@ export class IronListAdapter {
730
817
  }
731
818
 
732
819
  // Near end
733
- const maxOffset = this.size - this._virtualCount;
734
820
  if (this._scrollTop >= this._maxScrollTop && this._maxScrollTop > 0) {
735
821
  this._vidxOffset = maxOffset;
736
822
  if (oldOffset !== this._vidxOffset) {