Skip to content

Conversation

@tomivirkki
Copy link
Member

Prevent scroll to index when the virtualizer has 0 size.
Reorganize and document code in the iron-list-adapter.

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication


scrollToIndex(index) {
if (typeof index !== 'number' || isNaN(index) || !this.scrollTarget.offsetHeight) {
if (typeof index !== 'number' || isNaN(index) || this.size === 0 || !this.scrollTarget.offsetHeight) {
Copy link
Member Author

Choose a reason for hiding this comment

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

Skip scroll to index altogether if the virtualizer doesn't have any items. Scrolling to index with an empty virtualizer resulted in division by zero here. This didn't cause a bug since the _vidxOffset, which resulted to NaN, was fixed back to 0 here

Comment on lines +113 to +115
if (el.style.minHeight) {
el.style.minHeight = '';
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Performance improvement, only reset min-height if it has a value beforehand

Comment on lines -136 to +161
let fvi = this.firstVisibleIndex + this._vidxOffset;

const fviOffsetBefore = this.__getIndexScrollOffset(fvi);
// Record the scroll position before changing the size
let fvi; // first visible index
let fviOffsetBefore; // scroll offset of the first visible index
if (size > 0) {
fvi = this.firstVisibleIndex + this._vidxOffset;
fviOffsetBefore = this.__getIndexScrollOffset(fvi);
}

// Change the size
this.__size = size;
this._itemsChanged({
path: 'items'
});
flush();

fvi = Math.min(fvi, size - 1);
this.scrollToIndex(fvi);
// Try to restore the scroll position if the new size is larger than 0
if (size > 0) {
fvi = Math.min(fvi, size - 1);
this.scrollToIndex(fvi);

const fviOffsetAfter = this.__getIndexScrollOffset(fvi);
if (fviOffsetBefore !== undefined && fviOffsetAfter !== undefined) {
this._scrollTop += fviOffsetBefore - fviOffsetAfter;
const fviOffsetAfter = this.__getIndexScrollOffset(fvi);
if (fviOffsetBefore !== undefined && fviOffsetAfter !== undefined) {
this._scrollTop += fviOffsetBefore - fviOffsetAfter;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

Reorganize and document the code better.

this._itemsChanged({
path: 'items'
});
flush();
Copy link
Member Author

@tomivirkki tomivirkki May 20, 2021

Choose a reason for hiding this comment

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

The additional flush() is now required after not invoking super.scrollToIndex (which in turn flushed) when the size is 0 (the issue was caught by tests)

@tomivirkki tomivirkki merged commit aaa8e02 into master May 20, 2021
@tomivirkki tomivirkki deleted the scroll-to-index-no-size branch May 20, 2021 13:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants