Open History to see the last commit
1import * as React from 'react'
2import * as ReactDOM from 'react-dom/client'
3β
4import './index.css'
5β
6import { useWindowVirtualizer } from '@tanstack/react-virtual'
7β
8function Example() {
9 const listRef = React.useRef<HTMLDivElement | null>(null)
10β
11 const virtualizer = useWindowVirtualizer({
12 count: 10000,
13 estimateSize: () => 35,
14 overscan: 5,
15 scrollMargin: listRef.current?.offsetTop ?? 0,
16 })
17β
18 return (
19 <>
20 <div ref={listRef} className="List">
21 <div
22 style={{
23 height: `${virtualizer.getTotalSize()}px`,
24 width: '100%',
25 position: 'relative',
26 }}
27 >
28 {virtualizer.getVirtualItems().map((item) => (
29 <div
30 key={item.key}
31 className={item.index % 2 ? 'ListItemOdd' : 'ListItemEven'}
32 style={{
33 position: 'absolute',
34 top: 0,
35 left: 0,
36 width: '100%',
37 height: `${item.size}px`,
38 transform: `translateY(${
39 item.start - virtualizer.options.scrollMargin
40 }px)`,
41 }}
42 >
43 Row {item.index}
44 </div>
45 ))}
46 </div>
47 </div>
48 </>
49 )
50}
51β
52function App() {
53 return (
54 <div>
55 <p>
56 In many cases, when implementing a virtualizer with a window as the
57 scrolling element, developers often find the need to specify a
58 "scrollMargin." The scroll margin is a crucial setting that defines the
59 space or gap between the start of the page and the edges of the list.
60 </p>
61 <br />
62 <br />
63 <h3>Window scroller</h3>
64 <Example />
65 <br />
66 <br />
67 {process.env.NODE_ENV === 'development' ? (
68 <p>
69 <strong>Notice:</strong> You are currently running React in
70 development mode. Rendering performance will be slightly degraded
71 until this application is built for production.
72 </p>
73 ) : null}
74 </div>
75 )
76}
77β
78ReactDOM.createRoot(document.getElementById('root')!).render(
79 <React.StrictMode>
80 <App />
81 </React.StrictMode>,
82)