forked from TanStack/virtual
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.tsx
More file actions
106 lines (98 loc) · 2.65 KB
/
main.tsx
File metadata and controls
106 lines (98 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import React from 'react'
import ReactDOM from 'react-dom'
import './index.css'
import { useMeasure } from '@react-hookz/web/esm'
import { useVirtualizer } from '@tanstack/react-virtual'
function App() {
const parentRef = React.useRef<HTMLDivElement>()
const [theadSize, theadRef] = useMeasure<HTMLTableSectionElement>()
const rowVirtualizer = useVirtualizer({
count: 10000,
getScrollElement: () => parentRef.current,
estimateSize: React.useCallback(() => 35, []),
overscan: 5,
paddingStart: theadSize?.height ?? 0,
scrollPaddingStart: theadSize?.height ?? 0,
})
return (
<>
<div className="flex gap-2">
<button
onClick={() => {
rowVirtualizer.scrollToIndex(40)
}}
className="border border-black"
>
Scroll to index 40
</button>
<button
onClick={() => {
rowVirtualizer.scrollToIndex(20)
}}
className="border border-black"
>
Then scroll to index 20
</button>
</div>
<br />
<br />
<div
ref={parentRef}
className="List"
style={{
height: `200px`,
width: `400px`,
overflow: 'auto',
}}
>
<table
style={{
height: `${rowVirtualizer.getTotalSize()}px`,
width: '100%',
}}
>
<thead ref={theadRef}>
<tr>
<th>Index</th>
<th>Key</th>
</tr>
</thead>
<tbody>
{rowVirtualizer.getVirtualItems().map((virtualRow) => (
<tr
key={virtualRow.index}
className={
virtualRow.index % 2 ? 'ListItemOdd' : 'ListItemEven'
}
style={{
position: 'absolute',
top: 0,
left: 0,
width: '100%',
height: `${virtualRow.size}px`,
transform: `translateY(${virtualRow.start}px)`,
}}
>
<td>#{virtualRow.index}</td>
<td>{virtualRow.key}</td>
</tr>
))}
</tbody>
</table>
</div>
{process.env.NODE_ENV === 'development' ? (
<p>
<strong>Notice:</strong> You are currently running React in
development mode. Rendering performance will be slightly degraded
until this application is build for production.
</p>
) : null}
</>
)
}
ReactDOM.render(
<React.StrictMode>
<App />
</React.StrictMode>,
document.getElementById('root'),
)