@tanstack/react-virtual 3.0.0-alpha.0 → 3.0.0-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +1 -18
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
- package/build/cjs/react-virtual/src/index.js +90 -0
- package/build/cjs/react-virtual/src/index.js.map +1 -0
- package/build/cjs/virtual-core/build/esm/index.js +565 -0
- package/build/cjs/virtual-core/build/esm/index.js.map +1 -0
- package/build/esm/index.js +432 -350
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +24 -119
- package/build/types/react-virtual/src/index.d.ts +4 -0
- package/build/types/virtual-core/src/utils.d.ts +7 -0
- package/build/umd/index.development.js +443 -352
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +11 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +16 -17
- package/src/index.tsx +66 -386
- package/build/cjs/packages/react-virtual/src/index.js +0 -371
- package/build/cjs/packages/react-virtual/src/index.js.map +0 -1
- package/build/cjs/packages/react-virtual/src/useIsomorphicLayoutEffect.js +0 -40
- package/build/cjs/packages/react-virtual/src/useIsomorphicLayoutEffect.js.map +0 -1
- package/build/cjs/packages/react-virtual/src/useRect.js +0 -85
- package/build/cjs/packages/react-virtual/src/useRect.js.map +0 -1
- package/build/types/index.d.ts +0 -53
- package/build/types/useIsomorphicLayoutEffect.d.ts +0 -2
- package/build/types/useRect.d.ts +0 -6
- package/src/useIsomorphicLayoutEffect.ts +0 -4
- package/src/useRect.ts +0 -58
package/src/index.tsx
CHANGED
|
@@ -1,400 +1,80 @@
|
|
|
1
1
|
import * as React from 'react'
|
|
2
|
-
import {
|
|
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
|
-
export const defaultRangeExtractor = (range: Range) => {
|
|
33
|
-
const start = Math.max(range.start - range.overscan, 0)
|
|
34
|
-
const end = Math.min(range.end + range.overscan, range.size - 1)
|
|
35
|
-
|
|
36
|
-
const arr = []
|
|
37
|
-
|
|
38
|
-
for (let i = start; i <= end; i++) {
|
|
39
|
-
arr.push(i)
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
return arr
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
type Key = number | string
|
|
46
|
-
|
|
47
|
-
interface Item {
|
|
48
|
-
key: Key
|
|
49
|
-
index: number
|
|
50
|
-
start: number
|
|
51
|
-
end: number
|
|
52
|
-
size: number
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
export interface VirtualItem extends Item {
|
|
56
|
-
measureRef: (el: HTMLElement | null) => void
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
export interface Options<T> {
|
|
60
|
-
size: number
|
|
61
|
-
parentRef: React.RefObject<T>
|
|
62
|
-
estimateSize?: (index: number) => number
|
|
63
|
-
overscan?: number
|
|
64
|
-
horizontal?: boolean
|
|
65
|
-
scrollToFn?: (
|
|
66
|
-
offset: number,
|
|
67
|
-
defaultScrollToFn?: (offset: number) => void,
|
|
68
|
-
) => void
|
|
69
|
-
paddingStart?: number
|
|
70
|
-
paddingEnd?: number
|
|
71
|
-
useObserver?: (ref: React.RefObject<T>, initialRect?: Rect) => Rect
|
|
72
|
-
initialRect?: Rect
|
|
73
|
-
keyExtractor?: (index: number) => Key
|
|
74
|
-
onScrollElement?: React.RefObject<HTMLElement>
|
|
75
|
-
scrollOffsetFn?: (event?: Event) => number
|
|
76
|
-
rangeExtractor?: (range: Range) => number[]
|
|
77
|
-
measureSize?: (el: HTMLElement, horizontal: boolean) => number
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
export const useVirtual = <T extends HTMLElement>({
|
|
81
|
-
size = 0,
|
|
82
|
-
estimateSize = defaultEstimateSize,
|
|
83
|
-
overscan = 1,
|
|
84
|
-
paddingStart = 0,
|
|
85
|
-
paddingEnd = 0,
|
|
86
|
-
parentRef,
|
|
87
|
-
horizontal = false,
|
|
88
|
-
scrollToFn,
|
|
89
|
-
useObserver,
|
|
90
|
-
initialRect,
|
|
91
|
-
onScrollElement,
|
|
92
|
-
scrollOffsetFn,
|
|
93
|
-
keyExtractor = defaultKeyExtractor,
|
|
94
|
-
measureSize = defaultMeasureSize,
|
|
95
|
-
rangeExtractor = defaultRangeExtractor,
|
|
96
|
-
}: Options<T>) => {
|
|
97
|
-
const sizeKey = horizontal ? 'width' : 'height'
|
|
98
|
-
const scrollKey = horizontal ? 'scrollLeft' : 'scrollTop'
|
|
99
|
-
|
|
100
|
-
const latestRef = React.useRef<{
|
|
101
|
-
scrollOffset: number
|
|
102
|
-
measurements: Item[]
|
|
103
|
-
outerSize: number
|
|
104
|
-
totalSize: number
|
|
105
|
-
}>({
|
|
106
|
-
outerSize: 0,
|
|
107
|
-
scrollOffset: 0,
|
|
108
|
-
measurements: [],
|
|
109
|
-
totalSize: 0,
|
|
110
|
-
})
|
|
111
|
-
|
|
112
|
-
const [scrollOffset, setScrollOffset] = React.useState(0)
|
|
113
|
-
latestRef.current.scrollOffset = scrollOffset
|
|
114
|
-
|
|
115
|
-
const useMeasureParent = useObserver || useRect
|
|
116
|
-
|
|
117
|
-
const { [sizeKey]: outerSize } = useMeasureParent(parentRef, initialRect)
|
|
118
|
-
|
|
119
|
-
latestRef.current.outerSize = outerSize
|
|
120
|
-
|
|
121
|
-
const defaultScrollToFn = React.useCallback(
|
|
122
|
-
(offset: number) => {
|
|
123
|
-
if (parentRef.current) {
|
|
124
|
-
parentRef.current[scrollKey] = offset
|
|
125
|
-
}
|
|
126
|
-
},
|
|
127
|
-
[parentRef, scrollKey],
|
|
128
|
-
)
|
|
129
|
-
|
|
130
|
-
const resolvedScrollToFn = scrollToFn || defaultScrollToFn
|
|
131
|
-
|
|
132
|
-
const scrollTo = React.useCallback(
|
|
133
|
-
(offset: number) => {
|
|
134
|
-
resolvedScrollToFn(offset, defaultScrollToFn)
|
|
2
|
+
import {
|
|
3
|
+
elementScroll,
|
|
4
|
+
observeElementOffset,
|
|
5
|
+
observeElementRect,
|
|
6
|
+
observeWindowOffset,
|
|
7
|
+
observeWindowRect,
|
|
8
|
+
PartialKeys,
|
|
9
|
+
Virtualizer,
|
|
10
|
+
VirtualizerOptions,
|
|
11
|
+
windowScroll,
|
|
12
|
+
} from '@tanstack/virtual-core'
|
|
13
|
+
export * from '@tanstack/virtual-core'
|
|
14
|
+
|
|
15
|
+
//
|
|
16
|
+
|
|
17
|
+
const useIsomorphicLayoutEffect =
|
|
18
|
+
typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect
|
|
19
|
+
|
|
20
|
+
function useVirtualizerBase<TScrollElement, TItemElement = unknown>(
|
|
21
|
+
options: VirtualizerOptions<TScrollElement, TItemElement>,
|
|
22
|
+
): Virtualizer<TScrollElement, TItemElement> {
|
|
23
|
+
const rerender = React.useReducer(() => ({}), {})[1]
|
|
24
|
+
|
|
25
|
+
const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {
|
|
26
|
+
...options,
|
|
27
|
+
onChange: (instance) => {
|
|
28
|
+
rerender()
|
|
29
|
+
options.onChange?.(instance)
|
|
135
30
|
},
|
|
136
|
-
|
|
137
|
-
)
|
|
138
|
-
|
|
139
|
-
const [measuredCache, setMeasuredCache] = React.useState<Record<Key, number>>(
|
|
140
|
-
{},
|
|
141
|
-
)
|
|
142
|
-
|
|
143
|
-
const measure = React.useCallback(() => setMeasuredCache({}), [])
|
|
144
|
-
|
|
145
|
-
const pendingMeasuredCacheIndexesRef = React.useRef<number[]>([])
|
|
146
|
-
|
|
147
|
-
const measurements = React.useMemo(() => {
|
|
148
|
-
const min =
|
|
149
|
-
pendingMeasuredCacheIndexesRef.current.length > 0
|
|
150
|
-
? Math.min(...pendingMeasuredCacheIndexesRef.current)
|
|
151
|
-
: 0
|
|
152
|
-
pendingMeasuredCacheIndexesRef.current = []
|
|
153
|
-
|
|
154
|
-
const measurements = latestRef.current.measurements.slice(0, min)
|
|
155
|
-
|
|
156
|
-
for (let i = min; i < size; i++) {
|
|
157
|
-
const key = keyExtractor(i)
|
|
158
|
-
const measuredSize = measuredCache[key]
|
|
159
|
-
const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart
|
|
160
|
-
const size =
|
|
161
|
-
typeof measuredSize === 'number' ? measuredSize : estimateSize(i)
|
|
162
|
-
const end = start + size
|
|
163
|
-
measurements[i] = { index: i, start, size, end, key }
|
|
164
|
-
}
|
|
165
|
-
return measurements
|
|
166
|
-
}, [estimateSize, measuredCache, paddingStart, size, keyExtractor])
|
|
167
|
-
|
|
168
|
-
const totalSize = (measurements[size - 1]?.end || paddingStart) + paddingEnd
|
|
169
|
-
|
|
170
|
-
latestRef.current.measurements = measurements
|
|
171
|
-
latestRef.current.totalSize = totalSize
|
|
172
|
-
|
|
173
|
-
const element = onScrollElement ? onScrollElement.current : parentRef.current
|
|
174
|
-
|
|
175
|
-
const scrollOffsetFnRef = React.useRef(scrollOffsetFn)
|
|
176
|
-
scrollOffsetFnRef.current = scrollOffsetFn
|
|
177
|
-
|
|
178
|
-
useIsomorphicLayoutEffect(() => {
|
|
179
|
-
if (!element) {
|
|
180
|
-
setScrollOffset(0)
|
|
181
|
-
|
|
182
|
-
return
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
const onScroll = (event?: Event) => {
|
|
186
|
-
const offset = scrollOffsetFnRef.current
|
|
187
|
-
? scrollOffsetFnRef.current(event)
|
|
188
|
-
: element[scrollKey]
|
|
189
|
-
|
|
190
|
-
setScrollOffset(offset)
|
|
191
|
-
}
|
|
192
|
-
|
|
193
|
-
onScroll()
|
|
194
|
-
|
|
195
|
-
element.addEventListener('scroll', onScroll, {
|
|
196
|
-
capture: false,
|
|
197
|
-
passive: true,
|
|
198
|
-
})
|
|
199
|
-
|
|
200
|
-
return () => {
|
|
201
|
-
element.removeEventListener('scroll', onScroll)
|
|
202
|
-
}
|
|
203
|
-
}, [element, scrollKey])
|
|
204
|
-
|
|
205
|
-
const { start, end } = calculateRange(latestRef.current)
|
|
31
|
+
}
|
|
206
32
|
|
|
207
|
-
const
|
|
208
|
-
() =>
|
|
209
|
-
rangeExtractor({
|
|
210
|
-
start,
|
|
211
|
-
end,
|
|
212
|
-
overscan,
|
|
213
|
-
size: measurements.length,
|
|
214
|
-
}),
|
|
215
|
-
[start, end, overscan, measurements.length, rangeExtractor],
|
|
33
|
+
const [instance] = React.useState(
|
|
34
|
+
() => new Virtualizer<TScrollElement, TItemElement>(resolvedOptions),
|
|
216
35
|
)
|
|
217
36
|
|
|
218
|
-
|
|
219
|
-
measureSizeRef.current = measureSize
|
|
220
|
-
|
|
221
|
-
const virtualItems: VirtualItem[] = React.useMemo(() => {
|
|
222
|
-
const virtualItems = []
|
|
37
|
+
instance.setOptions(resolvedOptions)
|
|
223
38
|
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
const item = {
|
|
229
|
-
...measurement,
|
|
230
|
-
measureRef: (el: HTMLElement | null) => {
|
|
231
|
-
if (el) {
|
|
232
|
-
const measuredSize = measureSizeRef.current(el, horizontal)
|
|
233
|
-
|
|
234
|
-
if (measuredSize !== item.size) {
|
|
235
|
-
const { scrollOffset } = latestRef.current
|
|
236
|
-
|
|
237
|
-
if (item.start < scrollOffset) {
|
|
238
|
-
defaultScrollToFn(scrollOffset + (measuredSize - item.size))
|
|
239
|
-
}
|
|
240
|
-
|
|
241
|
-
pendingMeasuredCacheIndexesRef.current.push(i)
|
|
242
|
-
|
|
243
|
-
setMeasuredCache((old) => ({
|
|
244
|
-
...old,
|
|
245
|
-
[item.key]: measuredSize,
|
|
246
|
-
}))
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
},
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
virtualItems.push(item)
|
|
253
|
-
}
|
|
254
|
-
|
|
255
|
-
return virtualItems
|
|
256
|
-
}, [indexes, defaultScrollToFn, horizontal, measurements])
|
|
257
|
-
|
|
258
|
-
const mountedRef = React.useRef(false)
|
|
39
|
+
React.useEffect(() => {
|
|
40
|
+
return instance._didMount()
|
|
41
|
+
}, [])
|
|
259
42
|
|
|
260
43
|
useIsomorphicLayoutEffect(() => {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
}
|
|
264
|
-
mountedRef.current = true
|
|
265
|
-
}, [estimateSize])
|
|
266
|
-
|
|
267
|
-
const scrollToOffset = React.useCallback(
|
|
268
|
-
(
|
|
269
|
-
toOffset: number,
|
|
270
|
-
{ align }: ScrollToOffsetOptions = { align: 'start' },
|
|
271
|
-
) => {
|
|
272
|
-
const { scrollOffset, outerSize } = latestRef.current
|
|
273
|
-
|
|
274
|
-
if (align === 'auto') {
|
|
275
|
-
if (toOffset <= scrollOffset) {
|
|
276
|
-
align = 'start'
|
|
277
|
-
} else if (toOffset >= scrollOffset + outerSize) {
|
|
278
|
-
align = 'end'
|
|
279
|
-
} else {
|
|
280
|
-
align = 'start'
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
if (align === 'start') {
|
|
285
|
-
scrollTo(toOffset)
|
|
286
|
-
} else if (align === 'end') {
|
|
287
|
-
scrollTo(toOffset - outerSize)
|
|
288
|
-
} else if (align === 'center') {
|
|
289
|
-
scrollTo(toOffset - outerSize / 2)
|
|
290
|
-
}
|
|
291
|
-
},
|
|
292
|
-
[scrollTo],
|
|
293
|
-
)
|
|
294
|
-
|
|
295
|
-
const tryScrollToIndex = React.useCallback(
|
|
296
|
-
(
|
|
297
|
-
index: number,
|
|
298
|
-
{ align, ...rest }: ScrollToIndexOptions = { align: 'auto' },
|
|
299
|
-
) => {
|
|
300
|
-
const { measurements, scrollOffset, outerSize } = latestRef.current
|
|
301
|
-
|
|
302
|
-
const measurement = measurements[Math.max(0, Math.min(index, size - 1))]
|
|
303
|
-
|
|
304
|
-
if (!measurement) {
|
|
305
|
-
return
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
if (align === 'auto') {
|
|
309
|
-
if (measurement.end >= scrollOffset + outerSize) {
|
|
310
|
-
align = 'end'
|
|
311
|
-
} else if (measurement.start <= scrollOffset) {
|
|
312
|
-
align = 'start'
|
|
313
|
-
} else {
|
|
314
|
-
return
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
const toOffset =
|
|
319
|
-
align === 'center'
|
|
320
|
-
? measurement.start + measurement.size / 2
|
|
321
|
-
: align === 'end'
|
|
322
|
-
? measurement.end
|
|
323
|
-
: measurement.start
|
|
324
|
-
|
|
325
|
-
scrollToOffset(toOffset, { align, ...rest })
|
|
326
|
-
},
|
|
327
|
-
[scrollToOffset, size],
|
|
328
|
-
)
|
|
329
|
-
|
|
330
|
-
const scrollToIndex = React.useCallback(
|
|
331
|
-
(index: number, options?: ScrollToIndexOptions) => {
|
|
332
|
-
// We do a double request here because of
|
|
333
|
-
// dynamic sizes which can cause offset shift
|
|
334
|
-
// and end up in the wrong spot. Unfortunately,
|
|
335
|
-
// we can't know about those dynamic sizes until
|
|
336
|
-
// we try and render them. So double down!
|
|
337
|
-
tryScrollToIndex(index, options)
|
|
338
|
-
requestAnimationFrame(() => {
|
|
339
|
-
tryScrollToIndex(index, options)
|
|
340
|
-
})
|
|
341
|
-
},
|
|
342
|
-
[tryScrollToIndex],
|
|
343
|
-
)
|
|
44
|
+
return instance._willUpdate()
|
|
45
|
+
})
|
|
344
46
|
|
|
345
|
-
return
|
|
346
|
-
virtualItems,
|
|
347
|
-
totalSize,
|
|
348
|
-
scrollToOffset,
|
|
349
|
-
scrollToIndex,
|
|
350
|
-
measure,
|
|
351
|
-
}
|
|
47
|
+
return instance
|
|
352
48
|
}
|
|
353
49
|
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
)
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
} else if (currentValue > value) {
|
|
367
|
-
high = middle - 1
|
|
368
|
-
} else {
|
|
369
|
-
return middle
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
if (low > 0) {
|
|
374
|
-
return low - 1
|
|
375
|
-
} else {
|
|
376
|
-
return 0
|
|
377
|
-
}
|
|
50
|
+
export function useVirtualizer<TScrollElement, TItemElement = unknown>(
|
|
51
|
+
options: PartialKeys<
|
|
52
|
+
VirtualizerOptions<TScrollElement, TItemElement>,
|
|
53
|
+
'observeElementRect' | 'observeElementOffset' | 'scrollToFn'
|
|
54
|
+
>,
|
|
55
|
+
): Virtualizer<TScrollElement, TItemElement> {
|
|
56
|
+
return useVirtualizerBase<TScrollElement, TItemElement>({
|
|
57
|
+
observeElementRect: observeElementRect,
|
|
58
|
+
observeElementOffset: observeElementOffset,
|
|
59
|
+
scrollToFn: elementScroll,
|
|
60
|
+
...options,
|
|
61
|
+
})
|
|
378
62
|
}
|
|
379
63
|
|
|
380
|
-
function
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
end++
|
|
397
|
-
}
|
|
398
|
-
|
|
399
|
-
return { start, end }
|
|
64
|
+
export function useWindowVirtualizer<TItemElement = unknown>(
|
|
65
|
+
options: PartialKeys<
|
|
66
|
+
VirtualizerOptions<Window, TItemElement>,
|
|
67
|
+
| 'getScrollElement'
|
|
68
|
+
| 'observeElementRect'
|
|
69
|
+
| 'observeElementOffset'
|
|
70
|
+
| 'scrollToFn'
|
|
71
|
+
>,
|
|
72
|
+
): Virtualizer<Window, TItemElement> {
|
|
73
|
+
return useVirtualizerBase<Window, TItemElement>({
|
|
74
|
+
getScrollElement: () => (typeof window !== 'undefined' ? window : null!),
|
|
75
|
+
observeElementRect: observeWindowRect,
|
|
76
|
+
observeElementOffset: observeWindowOffset,
|
|
77
|
+
scrollToFn: windowScroll,
|
|
78
|
+
...options,
|
|
79
|
+
})
|
|
400
80
|
}
|