Skip to content

Commit 5961e35

Browse files
authoredFeb 25, 2025··
fix: align startIndex & endIndex to lanes for masonry mode (#933)
1 parent 8e55113 commit 5961e35

File tree

1 file changed

+25
-5
lines changed

1 file changed

+25
-5
lines changed
 

‎packages/virtual-core/src/index.ts

+25-5
Original file line numberDiff line numberDiff line change
@@ -702,14 +702,20 @@ export class Virtualizer<
702702
)
703703

704704
calculateRange = memo(
705-
() => [this.getMeasurements(), this.getSize(), this.getScrollOffset()],
706-
(measurements, outerSize, scrollOffset) => {
705+
() => [
706+
this.getMeasurements(),
707+
this.getSize(),
708+
this.getScrollOffset(),
709+
this.options.lanes,
710+
],
711+
(measurements, outerSize, scrollOffset, lanes) => {
707712
return (this.range =
708713
measurements.length > 0 && outerSize > 0
709714
? calculateRange({
710715
measurements,
711716
outerSize,
712717
scrollOffset,
718+
lanes,
713719
})
714720
: null)
715721
},
@@ -1105,23 +1111,37 @@ function calculateRange({
11051111
measurements,
11061112
outerSize,
11071113
scrollOffset,
1114+
lanes,
11081115
}: {
11091116
measurements: Array<VirtualItem>
11101117
outerSize: number
11111118
scrollOffset: number
1119+
lanes: number
11121120
}) {
1113-
const count = measurements.length - 1
1121+
const lastIndex = measurements.length - 1
11141122
const getOffset = (index: number) => measurements[index]!.start
11151123

1116-
const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset)
1124+
let startIndex = findNearestBinarySearch(
1125+
0,
1126+
lastIndex,
1127+
getOffset,
1128+
scrollOffset,
1129+
)
11171130
let endIndex = startIndex
11181131

11191132
while (
1120-
endIndex < count &&
1133+
endIndex < lastIndex &&
11211134
measurements[endIndex]!.end < scrollOffset + outerSize
11221135
) {
11231136
endIndex++
11241137
}
11251138

1139+
if (lanes > 1) {
1140+
// Align startIndex to the beginning of its lane
1141+
startIndex = Math.max(0, startIndex - (startIndex % lanes))
1142+
// Align endIndex to the end of its lane
1143+
endIndex = Math.min(lastIndex, endIndex + (lanes - 1 - (endIndex % lanes)))
1144+
}
1145+
11261146
return { startIndex, endIndex }
11271147
}

0 commit comments

Comments
 (0)
Please sign in to comment.