@tanstack/virtual-core 3.19.3 → 3.19.4

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.3",
3
+ "version": "3.19.4",
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
@@ -86,6 +86,25 @@ const getRect = (element: HTMLElement): Rect => {
86
86
  return { width: offsetWidth, height: offsetHeight }
87
87
  }
88
88
 
89
+ // Animation frames for `useAnimationFrameWithResizeObserver`, requested from
90
+ // the scroll element's own window rather than the global one.
91
+ const createFrameQueue = (targetWindow: Window) => {
92
+ const ids = new Set<number>()
93
+ return {
94
+ schedule: (fn: () => void) => {
95
+ const id = targetWindow.requestAnimationFrame(() => {
96
+ ids.delete(id)
97
+ fn()
98
+ })
99
+ ids.add(id)
100
+ },
101
+ cancel: () => {
102
+ ids.forEach((id) => targetWindow.cancelAnimationFrame(id))
103
+ ids.clear()
104
+ },
105
+ }
106
+ }
107
+
89
108
  export const defaultKeyExtractor = (index: number) => index
90
109
 
91
110
  export const defaultRangeExtractor = (range: Range) => {
@@ -129,6 +148,7 @@ export const observeElementRect = <T extends Element>(
129
148
  return () => {}
130
149
  }
131
150
 
151
+ const frames = createFrameQueue(targetWindow)
132
152
  const observer = new targetWindow.ResizeObserver((entries) => {
133
153
  const run = () => {
134
154
  const entry = entries[0]
@@ -143,7 +163,7 @@ export const observeElementRect = <T extends Element>(
143
163
  }
144
164
 
145
165
  instance.options.useAnimationFrameWithResizeObserver
146
- ? requestAnimationFrame(run)
166
+ ? frames.schedule(run)
147
167
  : run()
148
168
  })
149
169
 
@@ -151,6 +171,7 @@ export const observeElementRect = <T extends Element>(
151
171
 
152
172
  return () => {
153
173
  observer.unobserve(element)
174
+ frames.cancel()
154
175
  }
155
176
  }
156
177
 
@@ -566,17 +587,20 @@ export class Virtualizer<
566
587
  private now = () => this.targetWindow?.performance?.now?.() ?? Date.now()
567
588
  private observer = (() => {
568
589
  let _ro: ResizeObserver | null = null
590
+ let _frames: ReturnType<typeof createFrameQueue> | null = null
569
591
 
570
592
  const get = () => {
571
593
  if (_ro) {
572
594
  return _ro
573
595
  }
574
596
 
575
- if (!this.targetWindow || !this.targetWindow.ResizeObserver) {
597
+ const targetWindow = this.targetWindow
598
+ if (!targetWindow || !targetWindow.ResizeObserver) {
576
599
  return null
577
600
  }
578
601
 
579
- return (_ro = new this.targetWindow.ResizeObserver((entries) => {
602
+ const frames = (_frames = createFrameQueue(targetWindow))
603
+ return (_ro = new targetWindow.ResizeObserver((entries) => {
580
604
  const run = () => {
581
605
  // Measure every entry before notifying. A notify that adjusted
582
606
  // scroll re-renders synchronously, and that render can unmount
@@ -596,7 +620,7 @@ export class Virtualizer<
596
620
  }
597
621
  }
598
622
  this.options.useAnimationFrameWithResizeObserver
599
- ? requestAnimationFrame(run)
623
+ ? frames.schedule(run)
600
624
  : run()
601
625
  }))
602
626
  }
@@ -634,7 +658,9 @@ export class Virtualizer<
634
658
  return {
635
659
  disconnect: () => {
636
660
  get()?.disconnect()
661
+ _frames?.cancel()
637
662
  _ro = null
663
+ _frames = null
638
664
  },
639
665
  observe: (target: Element) =>
640
666
  get()?.observe(target, { box: 'border-box' }),