Skip to content

fix: adjust virtualizer indexes when scrolling at the end #9142

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion packages/component-base/src/virtualizer-iron-list-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,9 +551,19 @@ export class IronListAdapter {
this._adjustVirtualIndexOffset(this._scrollTop - (this.__previousScrollTop || 0));
const delta = this.scrollTarget.scrollTop - this._scrollPosition;

const lastIndexVisible = this.adjustedLastVisibleIndex === this.size - 1;
const hasEmptySpace = lastIndexVisible && this._physicalBottom < this._scrollBottom;

super._scrollHandler();

if (this._physicalCount !== 0) {
const needToCreateItemsAbove = lastIndexVisible && (delta < 0 || hasEmptySpace);
if (needToCreateItemsAbove) {
const idxAdjustment = Math.round((delta - this._scrollOffset) / this._physicalAverage);
this._virtualStart = Math.max(0, this._virtualStart + idxAdjustment);
this._physicalStart = Math.max(0, this._physicalStart + idxAdjustment);
this._physicalTop = Math.min(Math.floor(this._virtualStart) * this._physicalAverage, this._scrollPosition);
this._update();
} else if (this._physicalCount !== 0) {
const isScrollingDown = delta >= 0;
const reusables = this._getReusables(!isScrollingDown);

Expand Down
6 changes: 5 additions & 1 deletion packages/component-base/test/virtualizer-scrolling.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,9 +231,13 @@ describe('virtualizer - scrollbar scrolling', () => {
// Sanity check for iron-list internal properties
const adapter = virtualizer.__adapter;
const firstItem = adapter._physicalItems[adapter._physicalStart];
expect(firstItem.__virtualIndex).to.equal(adapter._virtualStart);
expect(firstItem.__virtualIndex).to.closeTo(adapter._virtualStart, 10);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indexes can go out of sync temporarily while scrolling.

}

const adapter = virtualizer.__adapter;
const firstItem = adapter._physicalItems[adapter._physicalStart];
expect(firstItem.__virtualIndex).to.eq(adapter._virtualStart);

// There should be an item at the bottom of the viewport
await nextFrame();
const listRect = scrollTarget.getBoundingClientRect();
Expand Down
30 changes: 30 additions & 0 deletions packages/grid/test/resizing.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,36 @@ describe('resizing', () => {
expect(grid.$.scroller.getBoundingClientRect().bottom).to.equal(grid.$.table.getBoundingClientRect().bottom);
});

it('should create rows when resized while scrolled to bottom', async () => {
// Have a full width grid inside a fixed width container
component = fixtureSync(`
<div style="height: 200px;">
<vaadin-grid style="height: 100%;">
<vaadin-grid-column></vaadin-grid-column>
</vaadin-grid>
</div>
`);
grid = component.querySelector('vaadin-grid');
grid.querySelector('vaadin-grid-column').renderer = (root, _, model) => {
root.textContent = model.item.name;
};
const itemCount = 1000;
grid.items = Array.from({ length: itemCount }, (_, i) => ({
name: `Item ${i}`,
}));
// Scroll to end
grid.scrollToIndex(itemCount - 1);
await aTimeout(200);
// Resize container
component.style.height = `${component.offsetHeight + 200}px`;
flushGrid(grid);
const gridRect = grid.getBoundingClientRect();
// Get an element from the area where new rows should be created
const elementInResizedArea = document.elementFromPoint(gridRect.left + 1, gridRect.top + 50);
const isCell = elementInResizedArea && elementInResizedArea.tagName === 'VAADIN-GRID-CELL-CONTENT';
expect(isCell).to.be.true;
});

describe('flexbox parent', () => {
beforeEach(() => {
grid.style.height = grid.style.width = '';
Expand Down