@@ -26,6 +26,7 @@ export function useVirtualizer_unstable(props: VirtualizerProps): VirtualizerSta
26
26
scrollViewRef,
27
27
enableScrollLoad,
28
28
updateScrollPosition,
29
+ gap = 0 ,
29
30
} = props ;
30
31
31
32
/* The context is optional, it's useful for injecting additional index logic, or performing uniform state updates*/
@@ -86,7 +87,8 @@ export function useVirtualizer_unstable(props: VirtualizerProps): VirtualizerSta
86
87
}
87
88
88
89
for ( let index = 0 ; index < numItems ; index ++ ) {
89
- childSizes . current [ index ] = getItemSize ( index ) ;
90
+ const _gap = index < numItems - 1 ? gap : 0 ;
91
+ childSizes . current [ index ] = getItemSize ( index ) + _gap ;
90
92
if ( index === 0 ) {
91
93
childProgressiveSizes . current [ index ] = childSizes . current [ index ] ;
92
94
} else {
@@ -162,7 +164,8 @@ export function useVirtualizer_unstable(props: VirtualizerProps): VirtualizerSta
162
164
163
165
let didUpdate = false ;
164
166
for ( let i = startIndex ; i < endIndex ; i ++ ) {
165
- const newSize = getItemSize ( i ) ;
167
+ const _gap = i < numItems - 1 ? gap : 0 ;
168
+ const newSize = getItemSize ( i ) + _gap ;
166
169
if ( newSize !== childSizes . current [ i ] ) {
167
170
childSizes . current [ i ] = newSize ;
168
171
didUpdate = true ;
@@ -177,7 +180,7 @@ export function useVirtualizer_unstable(props: VirtualizerProps): VirtualizerSta
177
180
}
178
181
}
179
182
} ,
180
- [ getItemSize , numItems , virtualizerLength ] ,
183
+ [ getItemSize , numItems , virtualizerLength , gap ] ,
181
184
) ;
182
185
183
186
const batchUpdateNewIndex = React . useCallback (
@@ -243,29 +246,29 @@ export function useVirtualizer_unstable(props: VirtualizerProps): VirtualizerSta
243
246
const getIndexFromScrollPosition = React . useCallback (
244
247
( scrollPos : number ) => {
245
248
if ( ! getItemSize ) {
246
- return Math . round ( scrollPos / itemSize ) ;
249
+ return Math . round ( scrollPos / ( itemSize + gap ) ) ;
247
250
}
248
251
249
252
return getIndexFromSizeArray ( scrollPos ) ;
250
253
} ,
251
- [ getIndexFromSizeArray , getItemSize , itemSize ] ,
254
+ [ getIndexFromSizeArray , getItemSize , itemSize , gap ] ,
252
255
) ;
253
256
254
257
const calculateTotalSize = React . useCallback ( ( ) => {
255
258
if ( ! getItemSize ) {
256
- return itemSize * numItems ;
259
+ return ( itemSize + gap ) * numItems ;
257
260
}
258
261
259
262
// Time for custom size calcs
260
263
return childProgressiveSizes . current [ numItems - 1 ] ;
261
- } , [ getItemSize , itemSize , numItems ] ) ;
264
+ } , [ getItemSize , itemSize , numItems , gap ] ) ;
262
265
263
266
const calculateBefore = React . useCallback ( ( ) => {
264
267
const currentIndex = Math . min ( actualIndex , numItems - 1 ) ;
265
268
266
269
if ( ! getItemSize ) {
267
270
// The missing items from before virtualization starts height
268
- return currentIndex * itemSize ;
271
+ return currentIndex * ( itemSize + gap ) ;
269
272
}
270
273
271
274
if ( currentIndex <= 0 ) {
@@ -274,7 +277,7 @@ export function useVirtualizer_unstable(props: VirtualizerProps): VirtualizerSta
274
277
275
278
// Time for custom size calcs
276
279
return childProgressiveSizes . current [ currentIndex - 1 ] ;
277
- } , [ actualIndex , getItemSize , itemSize , numItems ] ) ;
280
+ } , [ actualIndex , getItemSize , itemSize , numItems , gap ] ) ;
278
281
279
282
const calculateAfter = React . useCallback ( ( ) => {
280
283
if ( numItems === 0 || actualIndex + virtualizerLength >= numItems ) {
@@ -285,12 +288,12 @@ export function useVirtualizer_unstable(props: VirtualizerProps): VirtualizerSta
285
288
if ( ! getItemSize ) {
286
289
// The missing items from after virtualization ends height
287
290
const remainingItems = numItems - lastItemIndex ;
288
- return remainingItems * itemSize ;
291
+ return remainingItems * ( itemSize + gap ) - gap ;
289
292
}
290
293
291
294
// Time for custom size calcs
292
295
return childProgressiveSizes . current [ numItems - 1 ] - childProgressiveSizes . current [ lastItemIndex - 1 ] ;
293
- } , [ actualIndex , getItemSize , itemSize , numItems , virtualizerLength ] ) ;
296
+ } , [ actualIndex , getItemSize , itemSize , numItems , virtualizerLength , gap ] ) ;
294
297
295
298
// Observe intersections of virtualized components
296
299
const { setObserverList } = useIntersectionObserver (
@@ -532,7 +535,7 @@ export function useVirtualizer_unstable(props: VirtualizerProps): VirtualizerSta
532
535
533
536
// We only run this effect on getItemSize change (recalc dynamic sizes)
534
537
// eslint-disable-next-line react-hooks/exhaustive-deps
535
- } , [ getItemSize ] ) ;
538
+ } , [ getItemSize , gap ] ) ;
536
539
537
540
// Effect to check flag index on updates
538
541
React . useEffect ( ( ) => {
0 commit comments