@vaadin/component-base 24.4.0-alpha17 → 24.4.0-alpha19

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-alpha17",
3
+ "version": "24.4.0-alpha19",
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": "eb6b4dac66be1c0ddc77757bed5f84a6d355e5cf"
45
+ "gitHead": "5fad5eb53de7da69c1a19776a11ba74ceba27ea0"
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-alpha17';
12
+ return '24.4.0-alpha19';
13
13
  },
14
14
  });
15
15
 
@@ -89,6 +89,10 @@ export class IronListAdapter {
89
89
  return this.lastVisibleIndex + this._vidxOffset;
90
90
  }
91
91
 
92
+ get _maxVirtualIndexOffset() {
93
+ return this.size - this._virtualCount;
94
+ }
95
+
92
96
  __hasPlaceholders() {
93
97
  return this.__getVisibleElements().some((el) => el.__virtualizerPlaceholder);
94
98
  }
@@ -113,7 +117,7 @@ export class IronListAdapter {
113
117
  let targetVirtualIndex = Math.floor((index / this.size) * this._virtualCount);
114
118
  if (this._virtualCount - targetVirtualIndex < visibleElementCount) {
115
119
  targetVirtualIndex = this._virtualCount - (this.size - index);
116
- this._vidxOffset = this.size - this._virtualCount;
120
+ this._vidxOffset = this._maxVirtualIndexOffset;
117
121
  } else if (targetVirtualIndex < visibleElementCount) {
118
122
  if (index < OFFSET_ADJUST_MIN_THRESHOLD) {
119
123
  targetVirtualIndex = index;
@@ -300,24 +304,41 @@ export class IronListAdapter {
300
304
  this._debouncers._increasePoolIfNeeded.cancel();
301
305
  }
302
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
+
303
318
  // Change the size
304
319
  this.__size = size;
305
320
 
306
- if (!this._physicalItems) {
307
- // Not initialized yet
308
- this._itemsChanged({
309
- path: 'items',
310
- });
311
- this.__preventElementUpdates = true;
312
- flush();
313
- this.__preventElementUpdates = false;
314
- } else {
315
- // Already initialized, just update _virtualCount
316
- this._updateScrollerSize();
317
- this._virtualCount = this.items.length;
318
- 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
+ }
319
338
  }
320
339
 
340
+ this.__preventElementUpdates = false;
341
+
321
342
  // When reducing size while invisible, iron-list does not update items, so
322
343
  // their hidden state is not updated and their __lastUpdatedIndex is not
323
344
  // reset. In that case force an update here.
@@ -329,8 +350,7 @@ export class IronListAdapter {
329
350
  requestAnimationFrame(() => this._resizeHandler());
330
351
  }
331
352
 
332
- // Schedule and flush a resize handler. This will cause a
333
- // re-render for the elements.
353
+ // Schedule and flush a resize handler
334
354
  this._resizeHandler();
335
355
  flush();
336
356
  }
@@ -769,15 +789,16 @@ export class IronListAdapter {
769
789
 
770
790
  /** @private */
771
791
  _adjustVirtualIndexOffset(delta) {
792
+ const maxOffset = this._maxVirtualIndexOffset;
793
+
772
794
  if (this._virtualCount >= this.size) {
773
795
  this._vidxOffset = 0;
774
796
  } else if (this.__skipNextVirtualIndexAdjust) {
775
797
  this.__skipNextVirtualIndexAdjust = false;
776
798
  } else if (Math.abs(delta) > 10000) {
777
799
  // Process a large scroll position change
778
- const scale = this._scrollTop / (this.scrollTarget.scrollHeight - this.scrollTarget.offsetHeight);
779
- const offset = scale * this.size;
780
- 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);
781
802
  } else {
782
803
  // Make sure user can always swipe/wheel scroll to the start and end
783
804
  const oldOffset = this._vidxOffset;
@@ -796,7 +817,6 @@ export class IronListAdapter {
796
817
  }
797
818
 
798
819
  // Near end
799
- const maxOffset = this.size - this._virtualCount;
800
820
  if (this._scrollTop >= this._maxScrollTop && this._maxScrollTop > 0) {
801
821
  this._vidxOffset = maxOffset;
802
822
  if (oldOffset !== this._vidxOffset) {