@tanstack/virtual-core 3.19.16 → 3.19.17

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tanstack/virtual-core",
3
- "version": "3.19.16",
3
+ "version": "3.19.17",
4
4
  "description": "Headless UI for virtualizing scrollable elements in TS/JS + Frameworks",
5
5
  "author": "Tanner Linsley",
6
6
  "license": "MIT",
package/src/index.ts CHANGED
@@ -502,6 +502,7 @@ export class Virtualizer<
502
502
  items: Array<Key | VirtualItem>
503
503
  } | null = null
504
504
  itemSizeCache = new Map<Key, number>()
505
+ private elementKeys = new WeakMap<TItemElement, Key>()
505
506
  private itemSizeCacheVersion = 0
506
507
  private builtMeasurementOptions: object | null = null
507
508
  private builtItemSizeCacheVersion = -1
@@ -803,6 +804,16 @@ export class Virtualizer<
803
804
  pendingObservations.delete(target)
804
805
  get()?.unobserve(target)
805
806
  },
807
+ isDelivering: () => delivering,
808
+ asDelivery: (fn: () => void) => {
809
+ const prev = delivering
810
+ delivering = true
811
+ try {
812
+ fn()
813
+ } finally {
814
+ delivering = prev
815
+ }
816
+ },
806
817
  }
807
818
  })()
808
819
  range: { startIndex: number; endIndex: number } | null = null
@@ -2172,6 +2183,23 @@ export class Virtualizer<
2172
2183
  return
2173
2184
  }
2174
2185
 
2186
+ // Solid calls the ref before its render effects set the index attribute,
2187
+ // and inserts the row after them. Read the index once they have run.
2188
+ // A row mounted during a resize delivery is still registered next frame.
2189
+ if (!node.isConnected && !node.getAttribute(this.options.indexAttribute)) {
2190
+ const delivering = this.observer.isDelivering()
2191
+ queueMicrotask(() => {
2192
+ if (!node.getAttribute(this.options.indexAttribute)) {
2193
+ this.indexFromElement(node)
2194
+ } else if (delivering) {
2195
+ this.observer.asDelivery(() => this.measureElement(node))
2196
+ } else {
2197
+ this.measureElement(node)
2198
+ }
2199
+ })
2200
+ return
2201
+ }
2202
+
2175
2203
  const index = this.indexFromElement(node)
2176
2204
  if (!this.isIndexInRange(index)) return
2177
2205
 
@@ -2182,8 +2210,20 @@ export class Virtualizer<
2182
2210
  if (prevNode) {
2183
2211
  this.observer.unobserve(prevNode)
2184
2212
  }
2213
+ // The node may still be registered under another key: rows were
2214
+ // reordered and the framework kept the DOM node but gave it a new index
2215
+ // (or getItemKey now returns a different key for its index). Drop the
2216
+ // old entry so a later node for that key does not unobserve this one,
2217
+ // and unobserve it first so the observe() below delivers a fresh size
2218
+ // for the new key instead of being a no-op.
2219
+ const prevKey = this.elementKeys.get(node)
2220
+ if (prevKey !== undefined && this.elementsCache.get(prevKey) === node) {
2221
+ this.elementsCache.delete(prevKey)
2222
+ this.observer.unobserve(node)
2223
+ }
2185
2224
  this.observer.observe(node)
2186
2225
  this.elementsCache.set(key, node)
2226
+ this.elementKeys.set(node, key)
2187
2227
  }
2188
2228
 
2189
2229
  // Sync-measure when idle (initial render) or during programmatic scrolling
@@ -2202,6 +2242,13 @@ export class Virtualizer<
2202
2242
  }
2203
2243
  }
2204
2244
 
2245
+ // Every resizeItem() since the last getMeasurements() bumped the version
2246
+ // once and queued one pending resize. Any other bump (measure(), edge keys
2247
+ // change) means the built layout no longer matches itemSizeCache.
2248
+ private onlyPendingResizesSinceBuild = (): boolean =>
2249
+ this.itemSizeCacheVersion ===
2250
+ this.builtItemSizeCacheVersion + this.pendingResizes.length
2251
+
2205
2252
  private canShiftFromPendingResizes = (): boolean =>
2206
2253
  this.options.lanes === 1 &&
2207
2254
  this._singleLaneMeasurements !== null &&
@@ -2209,16 +2256,19 @@ export class Virtualizer<
2209
2256
  !this.lanesChangedFlag &&
2210
2257
  !this.lanesSettling &&
2211
2258
  this.getMeasurementOptions() === this.builtMeasurementOptions &&
2212
- this.itemSizeCacheVersion ===
2213
- this.builtItemSizeCacheVersion + this.pendingResizes.length
2259
+ this.onlyPendingResizesSinceBuild()
2214
2260
 
2215
2261
  resizeItem = (index: number, size: number) => {
2216
2262
  if (!this.isIndexInRange(index)) return
2217
2263
 
2218
- // Options can change before the next render rebuilds the measurements.
2219
- // Use the current item position and estimate when deciding whether this
2220
- // resize needs a scroll correction.
2221
- if (this.getMeasurementOptions() !== this.builtMeasurementOptions) {
2264
+ // Options or the size cache (measure()) can change before the next render
2265
+ // rebuilds the measurements. Rebuild so this resize is compared with the
2266
+ // current position and estimate, also when deciding whether it needs a
2267
+ // scroll correction.
2268
+ if (
2269
+ this.getMeasurementOptions() !== this.builtMeasurementOptions ||
2270
+ !this.onlyPendingResizesSinceBuild()
2271
+ ) {
2222
2272
  this.getMeasurements()
2223
2273
  }
2224
2274