-
-
Notifications
You must be signed in to change notification settings - Fork 426
Expand file tree
/
Copy pathmain.tsx
More file actions
261 lines (242 loc) · 7.83 KB
/
main.tsx
File metadata and controls
261 lines (242 loc) · 7.83 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
import * as React from 'react'
import { createRoot } from 'react-dom/client'
import { faker } from '@faker-js/faker'
import { useVirtualizer } from '@tanstack/react-virtual'
import './index.css'
type SizingMode = 'dynamic' | 'fixed' | 'variable'
const randomNumber = (min: number, max: number) =>
faker.number.int({ min, max })
const rowCount = 10000
const columnCount = 10000
// Generate cell content for dynamic mode
const generateCellContent = (row: number, col: number) => {
// Use a deterministic seed based on position
faker.seed(row * columnCount + col)
return faker.lorem.words(randomNumber(1, 4))
}
// Pre-computed variable sizes for "variable" mode
const variableRowHeights = new Array(rowCount)
.fill(true)
.map(() => 25 + Math.round(Math.random() * 75))
const variableColumnWidths = new Array(columnCount)
.fill(true)
.map(() => 75 + Math.round(Math.random() * 125))
function Grid() {
const parentRef = React.useRef<HTMLDivElement>(null)
const [sizingMode, setSizingMode] = React.useState<SizingMode>('dynamic')
// Row virtualizer
const rowVirtualizer = useVirtualizer({
count: rowCount,
getScrollElement: () => parentRef.current,
estimateSize: React.useCallback(
(index: number) => {
switch (sizingMode) {
case 'fixed':
return 35
case 'variable':
return variableRowHeights[index]
case 'dynamic':
default:
return 50
}
},
[sizingMode],
),
overscan: 5,
})
// Column virtualizer
const columnVirtualizer = useVirtualizer({
horizontal: true,
count: columnCount,
getScrollElement: () => parentRef.current,
estimateSize: React.useCallback(
(index: number) => {
switch (sizingMode) {
case 'fixed':
return 100
case 'variable':
return variableColumnWidths[index]
case 'dynamic':
default:
return 120
}
},
[sizingMode],
),
overscan: 5,
})
const virtualRows = rowVirtualizer.getVirtualItems()
const virtualColumns = columnVirtualizer.getVirtualItems()
return (
<div>
<div className="controls">
<div className="mode-selector">
<strong>Sizing Mode:</strong>
{(['dynamic', 'fixed', 'variable'] as const).map((mode) => (
<label key={mode}>
<input
type="radio"
name="sizingMode"
value={mode}
checked={sizingMode === mode}
onChange={() => setSizingMode(mode)}
style={{ display: 'none' }}
/>
<span>{mode.charAt(0).toUpperCase() + mode.slice(1)}</span>
</label>
))}
</div>
</div>
<div className="controls">
<button
onClick={() => {
rowVirtualizer.scrollToIndex(0)
columnVirtualizer.scrollToIndex(0)
}}
>
Scroll to top-left
</button>
<button
onClick={() => {
rowVirtualizer.scrollToIndex(Math.floor(rowCount / 2))
columnVirtualizer.scrollToIndex(Math.floor(columnCount / 2))
}}
>
Scroll to center
</button>
<button
onClick={() => {
rowVirtualizer.scrollToIndex(rowCount - 1)
columnVirtualizer.scrollToIndex(columnCount - 1)
}}
>
Scroll to bottom-right
</button>
</div>
<p style={{ fontSize: '12px', color: '#666' }}>
{sizingMode === 'dynamic' && (
<>
<strong>Dynamic mode:</strong> Cell sizes are estimated and can vary
based on content. Best for unknown or variable content.
</>
)}
{sizingMode === 'fixed' && (
<>
<strong>Fixed mode:</strong> All cells have the same dimensions
(35px height, 100px width). Best performance for uniform grids.
</>
)}
{sizingMode === 'variable' && (
<>
<strong>Variable mode:</strong> Each row/column has pre-computed
dimensions. Use when sizes are known but vary per row/column.
</>
)}
</p>
<div
ref={parentRef}
className="List"
style={{
height: 500,
width: '100%',
maxWidth: 800,
overflow: 'auto',
contain: 'strict',
}}
>
<div
style={{
height: rowVirtualizer.getTotalSize(),
width: columnVirtualizer.getTotalSize(),
position: 'relative',
}}
>
{virtualRows.map((virtualRow) => (
<React.Fragment key={virtualRow.key}>
{virtualColumns.map((virtualColumn) => {
const isEven =
(virtualRow.index + virtualColumn.index) % 2 === 0
return (
<div
key={virtualColumn.key}
className={isEven ? 'ListItemEven' : 'ListItemOdd'}
style={{
position: 'absolute',
top: 0,
left: 0,
width:
sizingMode === 'fixed'
? 100
: sizingMode === 'variable'
? variableColumnWidths[virtualColumn.index]
: virtualColumn.size,
height:
sizingMode === 'fixed'
? 35
: sizingMode === 'variable'
? variableRowHeights[virtualRow.index]
: virtualRow.size,
transform: `translateX(${virtualColumn.start}px) translateY(${virtualRow.start}px)`,
padding: '4px',
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap',
fontSize: '12px',
}}
>
{sizingMode === 'dynamic' ? (
<div>
<div style={{ fontWeight: 'bold', fontSize: '10px' }}>
{virtualRow.index},{virtualColumn.index}
</div>
<div style={{ fontSize: '11px' }}>
{generateCellContent(
virtualRow.index,
virtualColumn.index,
)}
</div>
</div>
) : (
`${virtualRow.index}, ${virtualColumn.index}`
)}
</div>
)
})}
</React.Fragment>
))}
</div>
</div>
<p style={{ marginTop: '1rem', fontSize: '12px', color: '#666' }}>
Rendering {virtualRows.length * virtualColumns.length} of{' '}
{(rowCount * columnCount).toLocaleString()} cells (
{virtualRows.length} rows x {virtualColumns.length} columns)
</p>
</div>
)
}
function App() {
return (
<div>
<h1>Grid Virtualization</h1>
<p>
Efficiently render large 2D grids (spreadsheets, data tables, game
boards) by only rendering visible cells. Both rows and columns are
virtualized simultaneously.
</p>
<Grid />
{process.env.NODE_ENV === 'development' && (
<p style={{ marginTop: '2rem', fontSize: '12px', color: '#999' }}>
<strong>Note:</strong> Running in development mode. Performance will
improve in production builds.
</p>
)}
</div>
)
}
const container = document.getElementById('root')!
const root = createRoot(container)
root.render(
<React.StrictMode>
<App />
</React.StrictMode>,
)