Skip to content

Commit 9ca62ab

Browse files
authored
fix: ensure variable item height virtualizer scrolls to top (#4703)
1 parent c87eed0 commit 9ca62ab

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,12 @@ export class IronListAdapter {
349349
}
350350

351351
this.__previousScrollTop = this._scrollTop;
352+
353+
// If the first visible index is not 0 when scrolled to the top,
354+
// add some scroll offset to enable the user to continue scrolling.
355+
if (this._scrollTop === 0 && this.firstVisibleIndex !== 0) {
356+
this._scrollTop = 1;
357+
}
352358
}
353359

354360
/** @private */
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { expect } from '@esm-bundle/chai';
2+
import { fixtureSync, nextFrame } from '@vaadin/testing-helpers';
3+
import { Virtualizer } from '../src/virtualizer.js';
4+
5+
describe('virtualizer - variable row height', () => {
6+
let virtualizer;
7+
let scrollTarget;
8+
9+
beforeEach(() => {
10+
scrollTarget = fixtureSync(`
11+
<div style="height: 500px; width: 200px;">
12+
<div></div>
13+
</div>
14+
`);
15+
const scrollContainer = scrollTarget.firstElementChild;
16+
17+
virtualizer = new Virtualizer({
18+
createElements: (count) => Array.from(Array(count)).map(() => document.createElement('div')),
19+
updateElement: (el, index) => {
20+
el.style.height = `${30 + index}px`;
21+
el.textContent = `Item ${index}`;
22+
},
23+
scrollTarget,
24+
scrollContainer,
25+
});
26+
27+
virtualizer.size = 100;
28+
});
29+
30+
it('should be able to scroll back to the top', async () => {
31+
// Scroll down
32+
scrollTarget.scrollTop = 1000;
33+
await nextFrame();
34+
35+
// Try scrolling back to the top until the first visible index is 0
36+
let attempts = 100;
37+
while (attempts > 0 && virtualizer.firstVisibleIndex !== 0) {
38+
attempts -= 1;
39+
scrollTarget.scrollTop = 0;
40+
await nextFrame();
41+
}
42+
43+
expect(virtualizer.firstVisibleIndex).to.equal(0);
44+
});
45+
});

0 commit comments

Comments
 (0)