Skip to content

Commit 390afca

Browse files
authored
fix(cdk/scrolling): Prevents NaN being returned as firstVisibleIndex. (#21505)
When firstVisibleIndex divides by zero, which can happen when the page is built and loaded dynamically; the NaN result will be used in other calculations. This then throws off things like the `newRange.start` value also returning NaN, breaking the plugin.
1 parent 13aa359 commit 390afca

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

src/cdk/scrolling/fixed-size-virtual-scroll.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ export class FixedSizeVirtualScrollStrategy implements VirtualScrollStrategy {
125125
const viewportSize = this._viewport.getViewportSize();
126126
const dataLength = this._viewport.getDataLength();
127127
let scrollOffset = this._viewport.measureScrollOffset();
128-
let firstVisibleIndex = scrollOffset / this._itemSize;
128+
// Prevent NaN as result when dividing by zero.
129+
let firstVisibleIndex = (this._itemSize > 0) ? scrollOffset / this._itemSize : 0;
129130

130131
// If user scrolls to the bottom of the list and data changes to a smaller list
131132
if (newRange.end > dataLength) {

0 commit comments

Comments
 (0)