11import React , { useRef } from "react" ;
2- import { useVirtualizer } from "@tanstack/react-virtual" ;
2+ import { useVirtualizer , useWindowVirtualizer } from "@tanstack/react-virtual" ;
33
44interface Props < T > {
55 data : T [ ] ;
@@ -8,14 +8,22 @@ interface Props<T> {
88
99export function LazyList < T > ( { data, render } : Props < T > ) {
1010 const parentRef = useRef < HTMLDivElement > ( null ) ;
11+ const parentOffsetRef = useRef ( 0 ) ;
1112
12- const virtualizer = useVirtualizer ( {
13+ const virtualizer = useWindowVirtualizer ( {
1314 count : data . length ,
14- getScrollElement : ( ) => parentRef . current ?. parentElement ?? null ,
1515 estimateSize : ( ) => 80 ,
1616 overscan : 10 ,
17+ scrollMargin : parentOffsetRef . current ,
1718 } ) ;
1819
20+ // Capture offset once on mount
21+ React . useLayoutEffect ( ( ) => {
22+ if ( parentRef . current ) {
23+ parentOffsetRef . current = parentRef . current . getBoundingClientRect ( ) . top + window . scrollY ;
24+ }
25+ } , [ ] ) ;
26+
1927 return (
2028 < div ref = { parentRef } >
2129 < div style = { { height : virtualizer . getTotalSize ( ) , position : "relative" } } >
@@ -29,7 +37,7 @@ export function LazyList<T>({ data, render }: Props<T>) {
2937 top : 0 ,
3038 left : 0 ,
3139 width : "100%" ,
32- transform : `translateY(${ virtualRow . start } px)` ,
40+ transform : `translateY(${ virtualRow . start - parentOffsetRef . current } px)` ,
3341 } }
3442 >
3543 { render ( data [ virtualRow . index ] ) }
0 commit comments