@tanstack/virtual-core 3.0.0-beta.1 → 3.0.0-beta.8
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/packages/virtual-core/src/index.js +38 -21
- package/build/cjs/packages/virtual-core/src/index.js.map +1 -1
- package/build/esm/index.js +38 -21
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/{stats-react.json → stats.json} +21 -21
- package/build/types/index.d.ts +2 -1
- package/build/umd/index.development.js +38 -21
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +1 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +1 -1
- package/src/index.ts +49 -33
package/src/index.ts
CHANGED
|
@@ -1,6 +1,4 @@
|
|
|
1
1
|
import observeRect from '@reach/observe-rect'
|
|
2
|
-
import { check } from 'prettier'
|
|
3
|
-
import React from 'react'
|
|
4
2
|
import { memo } from './utils'
|
|
5
3
|
|
|
6
4
|
export * from './utils'
|
|
@@ -9,7 +7,7 @@ export * from './utils'
|
|
|
9
7
|
|
|
10
8
|
type ScrollAlignment = 'start' | 'center' | 'end' | 'auto'
|
|
11
9
|
|
|
12
|
-
interface ScrollToOptions {
|
|
10
|
+
export interface ScrollToOptions {
|
|
13
11
|
align: ScrollAlignment
|
|
14
12
|
}
|
|
15
13
|
|
|
@@ -248,6 +246,10 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
|
|
|
248
246
|
private scrollOffset: number
|
|
249
247
|
private destinationOffset: undefined | number
|
|
250
248
|
private scrollCheckFrame!: ReturnType<typeof setTimeout>
|
|
249
|
+
private measureElementCache: Record<
|
|
250
|
+
number,
|
|
251
|
+
(measurableItem: TItemElement | null) => void
|
|
252
|
+
> = {}
|
|
251
253
|
|
|
252
254
|
constructor(opts: VirtualizerOptions<TScrollElement, TItemElement>) {
|
|
253
255
|
this.setOptions(opts)
|
|
@@ -286,6 +288,7 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
|
|
|
286
288
|
private cleanup = () => {
|
|
287
289
|
this.unsubs.filter(Boolean).forEach((d) => d!())
|
|
288
290
|
this.unsubs = []
|
|
291
|
+
this.scrollElement = null
|
|
289
292
|
}
|
|
290
293
|
|
|
291
294
|
_didMount = () => {
|
|
@@ -402,48 +405,61 @@ export class Virtualizer<TScrollElement = unknown, TItemElement = unknown> {
|
|
|
402
405
|
this.options.measureElement,
|
|
403
406
|
],
|
|
404
407
|
(indexes, measurements, measureElement) => {
|
|
408
|
+
const makeMeasureElement =
|
|
409
|
+
(index: number) => (measurableItem: TItemElement | null) => {
|
|
410
|
+
const item = this.measurementsCache[index]!
|
|
411
|
+
|
|
412
|
+
if (!measurableItem) {
|
|
413
|
+
return
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
const measuredItemSize = measureElement(measurableItem, this)
|
|
417
|
+
const itemSize = this.itemMeasurementsCache[item.key] ?? item.size
|
|
418
|
+
|
|
419
|
+
if (measuredItemSize !== itemSize) {
|
|
420
|
+
if (item.start < this.scrollOffset) {
|
|
421
|
+
if (
|
|
422
|
+
process.env.NODE_ENV === 'development' &&
|
|
423
|
+
this.options.debug
|
|
424
|
+
) {
|
|
425
|
+
console.info('correction', measuredItemSize - itemSize)
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
if (!this.destinationOffset) {
|
|
429
|
+
this._scrollToOffset(
|
|
430
|
+
this.scrollOffset + (measuredItemSize - itemSize),
|
|
431
|
+
false,
|
|
432
|
+
)
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
this.pendingMeasuredCacheIndexes.push(index)
|
|
437
|
+
this.itemMeasurementsCache = {
|
|
438
|
+
...this.itemMeasurementsCache,
|
|
439
|
+
[item.key]: measuredItemSize,
|
|
440
|
+
}
|
|
441
|
+
this.notify()
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
|
|
405
445
|
const virtualItems: VirtualItem<TItemElement>[] = []
|
|
406
446
|
|
|
447
|
+
const currentMeasureElements: typeof this.measureElementCache = {}
|
|
448
|
+
|
|
407
449
|
for (let k = 0, len = indexes.length; k < len; k++) {
|
|
408
450
|
const i = indexes[k]!
|
|
409
451
|
const measurement = measurements[i]!
|
|
410
452
|
|
|
411
453
|
const item = {
|
|
412
454
|
...measurement,
|
|
413
|
-
measureElement: (
|
|
414
|
-
|
|
415
|
-
const measuredItemSize = measureElement(measurableItem, this)
|
|
416
|
-
|
|
417
|
-
if (measuredItemSize !== item.size) {
|
|
418
|
-
if (item.start < this.scrollOffset) {
|
|
419
|
-
if (
|
|
420
|
-
process.env.NODE_ENV === 'development' &&
|
|
421
|
-
this.options.debug
|
|
422
|
-
)
|
|
423
|
-
console.info('correction', measuredItemSize - item.size)
|
|
424
|
-
|
|
425
|
-
if (!this.destinationOffset) {
|
|
426
|
-
this._scrollToOffset(
|
|
427
|
-
this.scrollOffset + (measuredItemSize - item.size),
|
|
428
|
-
false,
|
|
429
|
-
)
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
|
|
433
|
-
this.pendingMeasuredCacheIndexes.push(i)
|
|
434
|
-
this.itemMeasurementsCache = {
|
|
435
|
-
...this.itemMeasurementsCache,
|
|
436
|
-
[item.key]: measuredItemSize,
|
|
437
|
-
}
|
|
438
|
-
this.notify()
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
},
|
|
455
|
+
measureElement: (currentMeasureElements[i] =
|
|
456
|
+
this.measureElementCache[i] ?? makeMeasureElement(i)),
|
|
442
457
|
}
|
|
443
|
-
|
|
444
458
|
virtualItems.push(item)
|
|
445
459
|
}
|
|
446
460
|
|
|
461
|
+
this.measureElementCache = currentMeasureElements
|
|
462
|
+
|
|
447
463
|
return virtualItems
|
|
448
464
|
},
|
|
449
465
|
{
|