Skip to content
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

fix: calculate vidxOffset correctly when clientHeight != offsetHeight (#7268) (CP: 24.3) #7272

Merged
merged 2 commits into from
Mar 26, 2024
Merged
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
14 changes: 9 additions & 5 deletions packages/component-base/src/virtualizer-iron-list-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ export class IronListAdapter {
return this.lastVisibleIndex + this._vidxOffset;
}

get _maxVirtualIndexOffset() {
return this.size - this._virtualCount;
}

__hasPlaceholders() {
return this.__getVisibleElements().some((el) => el.__virtualizerPlaceholder);
}
Expand All @@ -106,7 +110,7 @@ export class IronListAdapter {
let targetVirtualIndex = Math.floor((index / this.size) * this._virtualCount);
if (this._virtualCount - targetVirtualIndex < visibleElementCount) {
targetVirtualIndex = this._virtualCount - (this.size - index);
this._vidxOffset = this.size - this._virtualCount;
this._vidxOffset = this._maxVirtualIndexOffset;
} else if (targetVirtualIndex < visibleElementCount) {
if (index < OFFSET_ADJUST_MIN_THRESHOLD) {
targetVirtualIndex = index;
Expand Down Expand Up @@ -719,15 +723,16 @@ export class IronListAdapter {

/** @private */
_adjustVirtualIndexOffset(delta) {
const maxOffset = this._maxVirtualIndexOffset;

if (this._virtualCount >= this.size) {
this._vidxOffset = 0;
} else if (this.__skipNextVirtualIndexAdjust) {
this.__skipNextVirtualIndexAdjust = false;
} else if (Math.abs(delta) > 10000) {
// Process a large scroll position change
const scale = this._scrollTop / (this.scrollTarget.scrollHeight - this.scrollTarget.offsetHeight);
const offset = scale * this.size;
this._vidxOffset = Math.round(offset - scale * this._virtualCount);
const scale = this._scrollTop / (this.scrollTarget.scrollHeight - this.scrollTarget.clientHeight);
this._vidxOffset = Math.round(scale * maxOffset);
} else {
// Make sure user can always swipe/wheel scroll to the start and end
const oldOffset = this._vidxOffset;
Expand All @@ -746,7 +751,6 @@ export class IronListAdapter {
}

// Near end
const maxOffset = this.size - this._virtualCount;
if (this._scrollTop >= this._maxScrollTop && this._maxScrollTop > 0) {
this._vidxOffset = maxOffset;
if (oldOffset !== this._vidxOffset) {
Expand Down
10 changes: 10 additions & 0 deletions packages/component-base/test/virtualizer-unlimited-size.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,16 @@ describe('unlimited size', () => {
expect(item.getBoundingClientRect().top).to.equal(scrollTarget.getBoundingClientRect().top);
});

it('should manually scroll to end when the scroll target has a border', async () => {
scrollTarget.style.borderTop = '20px solid black';

scrollTarget.scrollTop = scrollTarget.scrollHeight;
await oneEvent(scrollTarget, 'scroll');

const item = elementsContainer.querySelector(`#item-${virtualizer.size - 1}`);
expect(item.getBoundingClientRect().bottom).to.be.closeTo(scrollTarget.getBoundingClientRect().bottom, 1);
});

it('should manually scroll to start after scroll to index', async () => {
virtualizer.scrollToIndex(virtualizer.size / 400);

Expand Down
Loading