-
Couldn't load subscription status.
- Fork 87
refactor: skip scroll to index when virtualizer has no items #1971
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
Conversation
|
Kudos, SonarCloud Quality Gate passed!
|
|
|
||
| scrollToIndex(index) { | ||
| if (typeof index !== 'number' || isNaN(index) || !this.scrollTarget.offsetHeight) { | ||
| if (typeof index !== 'number' || isNaN(index) || this.size === 0 || !this.scrollTarget.offsetHeight) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| if (el.style.minHeight) { | ||
| el.style.minHeight = ''; | ||
| } |
There was a problem hiding this comment.
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
| 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; | ||
| } |
There was a problem hiding this comment.
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(); |
There was a problem hiding this comment.
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)
Prevent scroll to index when the virtualizer has 0 size.
Reorganize and document code in the iron-list-adapter.