Skip to content

Commit 83ebcc1

Browse files
authored
refactor: make virtualizer use dynamic placeholder height (#3908)
* refactor: make virtualizer placeholder height dynamic * fix: forward-port workaround from vaadin-grid
1 parent cc7be25 commit 83ebcc1

4 files changed

Lines changed: 57 additions & 13 deletions

File tree

‎packages/combo-box/test/dynamic-size.test.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ describe('dynamic size change', () => {
3535
comboBox.opened = true;
3636
scrollToIndex(comboBox, comboBox.size - 1);
3737
flushComboBox(comboBox);
38+
flushComboBox(comboBox);
3839
const items = getViewportItems(comboBox);
3940
expect(items.length).to.be.above(5);
4041
items.forEach((item) => {

‎packages/component-base/src/virtualizer-iron-list-adapter.js‎

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ export class IronListAdapter {
2626
// Iron-list uses this value to determine how many pages of elements to render
2727
this._maxPages = 1.3;
2828

29+
// Placeholder height (used for sizing elements that have intrinsic 0 height after update)
30+
this.__placeholderHeight = 200;
31+
// A queue of 10 previous element heights
32+
this.__elementHeightQueue = Array(10);
33+
2934
this.timeouts = {
3035
SCROLL_REORDER: 500,
3136
IGNORE_WHEEL: 500,
@@ -141,12 +146,22 @@ export class IronListAdapter {
141146
el.__lastUpdatedIndex = index;
142147
}
143148

144-
if (el.offsetHeight === 0) {
149+
const elementHeight = el.offsetHeight;
150+
if (elementHeight === 0) {
145151
// If the elements have 0 height after update (for example due to lazy rendering),
146152
// it results in iron-list requesting to create an unlimited count of elements.
147153
// Assign a temporary placeholder sizing to elements that would otherwise end up having
148154
// no height.
149-
el.style.paddingTop = '200px';
155+
el.style.paddingTop = `${this.__placeholderHeight}px`;
156+
} else {
157+
// Add element height to the queue
158+
this.__elementHeightQueue.push(elementHeight);
159+
this.__elementHeightQueue.shift();
160+
161+
// Calcualte new placeholder height based on the average of the defined values in the
162+
// element height queue
163+
const filteredHeights = this.__elementHeightQueue.filter((h) => h !== undefined);
164+
this.__placeholderHeight = Math.round(filteredHeights.reduce((a, b) => a + b, 0) / filteredHeights.length);
150165
}
151166
}
152167

@@ -297,16 +312,28 @@ export class IronListAdapter {
297312

298313
_scrollHandler() {
299314
this._adjustVirtualIndexOffset(this._scrollTop - (this.__previousScrollTop || 0));
315+
const delta = this.scrollTarget.scrollTop - this._scrollPosition;
300316

301317
super._scrollHandler();
302318

303319
if (this._physicalCount !== 0) {
304-
// After running super._scrollHandler, fix _virtualStart to workaround an iron-list issue.
305-
// See https://github.com/vaadin/web-components/issues/1691
306-
const reusables = this._getReusables(true);
307-
this._physicalTop = reusables.physicalTop;
308-
this._virtualStart += reusables.indexes.length;
309-
this._physicalStart += reusables.indexes.length;
320+
const isScrollingDown = delta >= 0;
321+
const reusables = this._getReusables(!isScrollingDown);
322+
323+
if (reusables.indexes.length) {
324+
// After running super._scrollHandler, fix internal properties to workaround an iron-list issue.
325+
// See https://github.com/vaadin/web-components/issues/1691
326+
this._physicalTop = reusables.physicalTop;
327+
328+
if (isScrollingDown) {
329+
this._virtualStart -= reusables.indexes.length;
330+
this._physicalStart -= reusables.indexes.length;
331+
} else {
332+
this._virtualStart += reusables.indexes.length;
333+
this._physicalStart += reusables.indexes.length;
334+
}
335+
this._resizeHandler();
336+
}
310337
}
311338

312339
if (this.reorderElements) {

‎packages/component-base/test/virtualizer-item-height.test.js‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import { Virtualizer } from '../src/virtualizer.js';
44

55
describe('virtualizer - item height', () => {
66
let elementsContainer;
7-
const ITEM_HEIGHT = 20;
7+
let virtualizer;
8+
const EVEN_ITEM_HEIGHT = 20;
9+
const ODD_ITEM_HEIGHT = 40;
810

911
beforeEach(async () => {
1012
const scrollTarget = fixtureSync(`
@@ -15,7 +17,7 @@ describe('virtualizer - item height', () => {
1517
const scrollContainer = scrollTarget.firstElementChild;
1618
elementsContainer = scrollContainer;
1719

18-
const virtualizer = new Virtualizer({
20+
virtualizer = new Virtualizer({
1921
createElements: (count) => Array.from({ length: count }, () => document.createElement('div')),
2022
updateElement: (el, index) => {
2123
el.style.width = '100%';
@@ -25,11 +27,12 @@ describe('virtualizer - item height', () => {
2527

2628
// The element initially has a height of 0.
2729
el.style.height = '';
30+
el.textContent = '';
2831

2932
// Update the element content dynamically (after a timeout) so its intrinsic height increases.
3033
setTimeout(() => {
3134
el.textContent = el.id;
32-
el.style.height = `${ITEM_HEIGHT}px`;
35+
el.style.height = `${index % 2 ? ODD_ITEM_HEIGHT : EVEN_ITEM_HEIGHT}px`;
3336
}, 50);
3437
}
3538
},
@@ -49,6 +52,19 @@ describe('virtualizer - item height', () => {
4952
const firstItem = elementsContainer.querySelector(`#item-0`);
5053
// Wait for the content to update
5154
await aTimeout(100);
52-
expect(firstItem.offsetHeight).to.equal(ITEM_HEIGHT);
55+
expect(firstItem.offsetHeight).to.equal(EVEN_ITEM_HEIGHT);
56+
});
57+
58+
it('should adjust the placeholder height', async () => {
59+
// Wait for the content to update
60+
await aTimeout(100);
61+
// Scroll down
62+
virtualizer.scrollToIndex(100);
63+
64+
const item = elementsContainer.querySelector(`#item-100`);
65+
// The item content should not yet have been updated so it uses the placeholder height
66+
// which should be an average of the previous n items heights.
67+
expect(item.offsetHeight).to.be.above(EVEN_ITEM_HEIGHT);
68+
expect(item.offsetHeight).to.be.below(ODD_ITEM_HEIGHT);
5369
});
5470
});

‎packages/component-base/test/virtualizer-reorder-elements.test.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('reorder elements', () => {
2727
resolve(mutations.flatMap((record) => [...record.removedNodes]));
2828
}).observe(elementsContainer, { childList: true });
2929

30-
virtualizer.scrollToIndex(Math.ceil(elementsContainer.childElementCount / 3));
30+
virtualizer.scrollToIndex(Math.ceil(elementsContainer.childElementCount / 2));
3131

3232
if (!skipFlush) {
3333
virtualizer.flush();

0 commit comments

Comments
 (0)