@tanstack/react-virtual 3.0.0-beta.2 → 3.0.0-beta.20

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.
@@ -22,76 +22,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
22
22
  *
23
23
  * @license MIT
24
24
  */
25
- var props = ["bottom", "height", "left", "right", "top", "width"];
26
-
27
- var rectChanged = function rectChanged(a, b) {
28
- if (a === void 0) {
29
- a = {};
30
- }
31
-
32
- if (b === void 0) {
33
- b = {};
34
- }
35
-
36
- return props.some(function (prop) {
37
- return a[prop] !== b[prop];
38
- });
39
- };
40
-
41
- var observedNodes = /*#__PURE__*/new Map();
42
- var rafId;
43
-
44
- var run = function run() {
45
- var changedStates = [];
46
- observedNodes.forEach(function (state, node) {
47
- var newRect = node.getBoundingClientRect();
48
-
49
- if (rectChanged(newRect, state.rect)) {
50
- state.rect = newRect;
51
- changedStates.push(state);
52
- }
53
- });
54
- changedStates.forEach(function (state) {
55
- state.callbacks.forEach(function (cb) {
56
- return cb(state.rect);
57
- });
58
- });
59
- rafId = window.requestAnimationFrame(run);
60
- };
61
-
62
- function observeRect(node, cb) {
63
- return {
64
- observe: function observe() {
65
- var wasEmpty = observedNodes.size === 0;
66
-
67
- if (observedNodes.has(node)) {
68
- observedNodes.get(node).callbacks.push(cb);
69
- } else {
70
- observedNodes.set(node, {
71
- rect: undefined,
72
- hasRectChanged: false,
73
- callbacks: [cb]
74
- });
75
- }
76
-
77
- if (wasEmpty) run();
78
- },
79
- unobserve: function unobserve() {
80
- var state = observedNodes.get(node);
81
-
82
- if (state) {
83
- // Remove the callback
84
- var index = state.callbacks.indexOf(cb);
85
- if (index >= 0) state.callbacks.splice(index, 1); // Remove the node reference
86
-
87
- if (!state.callbacks.length) observedNodes["delete"](node); // Stop the loop
88
-
89
- if (!observedNodes.size) cancelAnimationFrame(rafId);
90
- }
91
- }
92
- };
93
- }
94
-
95
25
  function memo(getDeps, fn, opts) {
96
26
  let deps = [];
97
27
  let result;
@@ -146,9 +76,29 @@ const defaultRangeExtractor = range => {
146
76
 
147
77
  return arr;
148
78
  };
79
+
80
+ const memoRectCallback = (instance, cb) => {
81
+ let prev = {
82
+ height: -1,
83
+ width: -1
84
+ };
85
+ return rect => {
86
+ if (instance.options.horizontal ? rect.width !== prev.width : rect.height !== prev.height) {
87
+ cb(rect);
88
+ }
89
+
90
+ prev = rect;
91
+ };
92
+ };
93
+
149
94
  const observeElementRect = (instance, cb) => {
150
- const observer = observeRect(instance.scrollElement, rect => {
151
- cb(rect);
95
+ const observer = new ResizeObserver(entries => {
96
+ var _entries$, _entries$2;
97
+
98
+ cb({
99
+ width: (_entries$ = entries[0]) == null ? void 0 : _entries$.contentRect.width,
100
+ height: (_entries$2 = entries[0]) == null ? void 0 : _entries$2.contentRect.height
101
+ });
152
102
  });
153
103
 
154
104
  if (!instance.scrollElement) {
@@ -156,18 +106,18 @@ const observeElementRect = (instance, cb) => {
156
106
  }
157
107
 
158
108
  cb(instance.scrollElement.getBoundingClientRect());
159
- observer.observe();
109
+ observer.observe(instance.scrollElement);
160
110
  return () => {
161
- observer.unobserve();
111
+ observer.unobserve(instance.scrollElement);
162
112
  };
163
113
  };
164
114
  const observeWindowRect = (instance, cb) => {
165
- const onResize = () => {
166
- cb({
167
- width: instance.scrollElement.innerWidth,
168
- height: instance.scrollElement.innerHeight
169
- });
170
- };
115
+ const memoizedCallback = memoRectCallback(instance, cb);
116
+
117
+ const onResize = () => memoizedCallback({
118
+ width: instance.scrollElement.innerWidth,
119
+ height: instance.scrollElement.innerHeight
120
+ });
171
121
 
172
122
  if (!instance.scrollElement) {
173
123
  return;
@@ -182,52 +132,80 @@ const observeWindowRect = (instance, cb) => {
182
132
  instance.scrollElement.removeEventListener('resize', onResize);
183
133
  };
184
134
  };
185
- const observeElementOffset = (instance, cb) => {
186
- const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollLeft' : 'scrollTop']);
135
+ const scrollProps = {
136
+ element: ['scrollLeft', 'scrollTop'],
137
+ window: ['scrollX', 'scrollY']
138
+ };
187
139
 
188
- if (!instance.scrollElement) {
189
- return;
190
- }
140
+ const createOffsetObserver = mode => {
141
+ return (instance, cb) => {
142
+ if (!instance.scrollElement) {
143
+ return;
144
+ }
191
145
 
192
- onScroll();
193
- instance.scrollElement.addEventListener('scroll', onScroll, {
194
- capture: false,
195
- passive: true
196
- });
197
- return () => {
198
- instance.scrollElement.removeEventListener('scroll', onScroll);
199
- };
200
- };
201
- const observeWindowOffset = (instance, cb) => {
202
- const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollX' : 'scrollY']);
146
+ const propX = scrollProps[mode][0];
147
+ const propY = scrollProps[mode][1];
148
+ let prevX = instance.scrollElement[propX];
149
+ let prevY = instance.scrollElement[propY];
203
150
 
204
- if (!instance.scrollElement) {
205
- return;
206
- }
151
+ const scroll = () => {
152
+ const offset = instance.scrollElement[instance.options.horizontal ? propX : propY];
153
+ cb(Math.max(0, offset - instance.options.scrollMargin));
154
+ };
207
155
 
208
- onScroll();
209
- instance.scrollElement.addEventListener('scroll', onScroll, {
210
- capture: false,
211
- passive: true
212
- });
213
- return () => {
214
- instance.scrollElement.removeEventListener('scroll', onScroll);
156
+ scroll();
157
+
158
+ const onScroll = e => {
159
+ const target = e.currentTarget;
160
+ const scrollX = target[propX];
161
+ const scrollY = target[propY];
162
+
163
+ if (instance.options.horizontal ? prevX - scrollX : prevY - scrollY) {
164
+ scroll();
165
+ }
166
+
167
+ prevX = scrollX;
168
+ prevY = scrollY;
169
+ };
170
+
171
+ instance.scrollElement.addEventListener('scroll', onScroll, {
172
+ capture: false,
173
+ passive: true
174
+ });
175
+ return () => {
176
+ instance.scrollElement.removeEventListener('scroll', onScroll);
177
+ };
215
178
  };
216
179
  };
180
+
181
+ const observeElementOffset = createOffsetObserver('element');
182
+ const observeWindowOffset = createOffsetObserver('window');
217
183
  const measureElement = (element, instance) => {
218
184
  return element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height'];
219
185
  };
220
- const windowScroll = (offset, canSmooth, instance) => {
186
+ const windowScroll = (offset, _ref, instance) => {
221
187
  var _instance$scrollEleme;
222
- (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo({
223
- [instance.options.horizontal ? 'left' : 'top']: offset,
188
+
189
+ let {
190
+ canSmooth,
191
+ sync
192
+ } = _ref;
193
+ const toOffset = sync ? offset : offset + instance.options.scrollMargin;
194
+ (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo == null ? void 0 : _instance$scrollEleme.scrollTo({
195
+ [instance.options.horizontal ? 'left' : 'top']: toOffset,
224
196
  behavior: canSmooth ? 'smooth' : undefined
225
197
  });
226
198
  };
227
- const elementScroll = (offset, canSmooth, instance) => {
199
+ const elementScroll = (offset, _ref2, instance) => {
228
200
  var _instance$scrollEleme2;
229
- (_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo({
230
- [instance.options.horizontal ? 'left' : 'top']: offset,
201
+
202
+ let {
203
+ canSmooth,
204
+ sync
205
+ } = _ref2;
206
+ const toOffset = sync ? offset : offset + instance.options.scrollMargin;
207
+ (_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo == null ? void 0 : _instance$scrollEleme2.scrollTo({
208
+ [instance.options.horizontal ? 'left' : 'top']: toOffset,
231
209
  behavior: canSmooth ? 'smooth' : undefined
232
210
  });
233
211
  };
@@ -237,13 +215,21 @@ class Virtualizer {
237
215
 
238
216
  this.unsubs = [];
239
217
  this.scrollElement = null;
218
+ this.isScrolling = false;
219
+ this.isScrollingTimeoutId = null;
240
220
  this.measurementsCache = [];
241
221
  this.itemMeasurementsCache = {};
242
222
  this.pendingMeasuredCacheIndexes = [];
223
+ this.scrollDelta = 0;
224
+ this.measureElementCache = {};
225
+ this.range = {
226
+ startIndex: 0,
227
+ endIndex: 0
228
+ };
243
229
 
244
230
  this.setOptions = opts => {
245
- Object.entries(opts).forEach(_ref => {
246
- let [key, value] = _ref;
231
+ Object.entries(opts).forEach(_ref3 => {
232
+ let [key, value] = _ref3;
247
233
  if (typeof value === 'undefined') delete opts[key];
248
234
  });
249
235
  this.options = {
@@ -264,6 +250,8 @@ class Virtualizer {
264
250
  width: 0,
265
251
  height: 0
266
252
  },
253
+ scrollMargin: 0,
254
+ scrollingDelay: 150,
267
255
  ...opts
268
256
  };
269
257
  };
@@ -277,6 +265,7 @@ class Virtualizer {
277
265
  this.cleanup = () => {
278
266
  this.unsubs.filter(Boolean).forEach(d => d());
279
267
  this.unsubs = [];
268
+ this.scrollElement = null;
280
269
  };
281
270
 
282
271
  this._didMount = () => {
@@ -291,14 +280,41 @@ class Virtualizer {
291
280
  if (this.scrollElement !== scrollElement) {
292
281
  this.cleanup();
293
282
  this.scrollElement = scrollElement;
283
+
284
+ this._scrollToOffset(this.scrollOffset, {
285
+ canSmooth: false,
286
+ sync: true,
287
+ requested: false
288
+ });
289
+
294
290
  this.unsubs.push(this.options.observeElementRect(this, rect => {
295
291
  this.scrollRect = rect;
296
- this.notify();
292
+ this.calculateRange();
297
293
  }));
298
294
  this.unsubs.push(this.options.observeElementOffset(this, offset => {
299
- this.scrollOffset = offset;
300
- this.notify();
295
+ if (this.isScrollingTimeoutId !== null) {
296
+ clearTimeout(this.isScrollingTimeoutId);
297
+ this.isScrollingTimeoutId = null;
298
+ }
299
+
300
+ if (this.scrollOffset !== offset) {
301
+ this.scrollOffset = offset;
302
+ this.isScrolling = true;
303
+ this.scrollDelta = 0;
304
+ this.isScrollingTimeoutId = setTimeout(() => {
305
+ this.isScrollingTimeoutId = null;
306
+ this.isScrolling = false;
307
+ this.notify();
308
+ }, this.options.scrollingDelay);
309
+ } else {
310
+ this.isScrolling = false;
311
+ this.scrollDelta = 0;
312
+ }
313
+
314
+ this.calculateRange();
301
315
  }));
316
+ } else if (!this.isScrolling) {
317
+ this.calculateRange();
302
318
  }
303
319
  };
304
320
 
@@ -329,108 +345,134 @@ class Virtualizer {
329
345
  this.measurementsCache = measurements;
330
346
  return measurements;
331
347
  }, {
332
- key: process.env.NODE_ENV === 'development' && 'getMeasurements',
348
+ key: process.env.NODE_ENV !== 'production' && 'getMeasurements',
333
349
  debug: () => this.options.debug
334
350
  });
335
351
  this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
336
- return calculateRange({
352
+ const range = calculateRange({
337
353
  measurements,
338
354
  outerSize,
339
355
  scrollOffset
340
356
  });
357
+
358
+ if (range.startIndex !== this.range.startIndex || range.endIndex !== this.range.endIndex) {
359
+ this.range = range;
360
+ this.notify();
361
+ }
362
+
363
+ return this.range;
341
364
  }, {
342
- key: process.env.NODE_ENV === 'development' && 'calculateRange',
365
+ key: process.env.NODE_ENV !== 'production' && 'calculateRange',
343
366
  debug: () => this.options.debug
344
367
  });
345
- this.getIndexes = memo(() => [this.options.rangeExtractor, this.calculateRange(), this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
368
+ this.getIndexes = memo(() => [this.options.rangeExtractor, this.range, this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
346
369
  return rangeExtractor({ ...range,
347
370
  overscan,
348
371
  count: count
349
372
  });
350
373
  }, {
351
- key: process.env.NODE_ENV === 'development' && 'getIndexes'
374
+ key: process.env.NODE_ENV !== 'production' && 'getIndexes',
375
+ debug: () => this.options.debug
352
376
  });
353
377
  this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements(), this.options.measureElement], (indexes, measurements, measureElement) => {
378
+ const makeMeasureElement = index => measurableItem => {
379
+ var _this$itemMeasurement;
380
+
381
+ const item = this.measurementsCache[index];
382
+
383
+ if (!measurableItem) {
384
+ return;
385
+ }
386
+
387
+ const measuredItemSize = measureElement(measurableItem, this);
388
+ const itemSize = (_this$itemMeasurement = this.itemMeasurementsCache[item.key]) != null ? _this$itemMeasurement : item.size;
389
+
390
+ if (measuredItemSize !== itemSize) {
391
+ if (item.start < this.scrollOffset) {
392
+ if (process.env.NODE_ENV !== 'production' && this.options.debug) {
393
+ console.info('correction', measuredItemSize - itemSize);
394
+ }
395
+
396
+ if (this.destinationOffset === undefined) {
397
+ this.scrollDelta += measuredItemSize - itemSize;
398
+
399
+ this._scrollToOffset(this.scrollOffset + this.scrollDelta, {
400
+ canSmooth: false,
401
+ sync: false,
402
+ requested: false
403
+ });
404
+ }
405
+ }
406
+
407
+ this.pendingMeasuredCacheIndexes.push(index);
408
+ this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
409
+ [item.key]: measuredItemSize
410
+ };
411
+ this.notify();
412
+ }
413
+ };
414
+
354
415
  const virtualItems = [];
416
+ const currentMeasureElements = {};
355
417
 
356
418
  for (let k = 0, len = indexes.length; k < len; k++) {
419
+ var _this$measureElementC;
420
+
357
421
  const i = indexes[k];
358
422
  const measurement = measurements[i];
359
423
  const item = { ...measurement,
360
- measureElement: measurableItem => {
361
- if (measurableItem) {
362
- const measuredItemSize = measureElement(measurableItem, this);
363
-
364
- if (measuredItemSize !== item.size) {
365
- if (item.start < this.scrollOffset) {
366
- if (process.env.NODE_ENV === 'development' && this.options.debug) console.info('correction', measuredItemSize - item.size);
367
-
368
- if (!this.destinationOffset) {
369
- this._scrollToOffset(this.scrollOffset + (measuredItemSize - item.size), false);
370
- }
371
- }
372
-
373
- this.pendingMeasuredCacheIndexes.push(i);
374
- this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
375
- [item.key]: measuredItemSize
376
- };
377
- this.notify();
378
- }
379
- }
380
- }
424
+ measureElement: currentMeasureElements[i] = (_this$measureElementC = this.measureElementCache[i]) != null ? _this$measureElementC : makeMeasureElement(i)
381
425
  };
382
426
  virtualItems.push(item);
383
427
  }
384
428
 
429
+ this.measureElementCache = currentMeasureElements;
385
430
  return virtualItems;
386
431
  }, {
387
- key: process.env.NODE_ENV === 'development' && 'getIndexes'
432
+ key: process.env.NODE_ENV !== 'production' && 'getIndexes',
433
+ debug: () => this.options.debug
388
434
  });
389
435
 
390
436
  this.scrollToOffset = function (toOffset, _temp) {
391
437
  let {
392
- align
393
- } = _temp === void 0 ? {
394
- align: 'start'
395
- } : _temp;
396
-
397
- const attempt = () => {
398
- const offset = _this.scrollOffset;
438
+ align = 'start',
439
+ smoothScroll = _this.options.enableSmoothScroll
440
+ } = _temp === void 0 ? {} : _temp;
441
+ const offset = _this.scrollOffset;
399
442
 
400
- const size = _this.getSize();
443
+ const size = _this.getSize();
401
444
 
402
- if (align === 'auto') {
403
- if (toOffset <= offset) {
404
- align = 'start';
405
- } else if (toOffset >= offset + size) {
406
- align = 'end';
407
- } else {
408
- align = 'start';
409
- }
445
+ if (align === 'auto') {
446
+ if (toOffset <= offset) {
447
+ align = 'start';
448
+ } else if (toOffset >= offset + size) {
449
+ align = 'end';
450
+ } else {
451
+ align = 'start';
410
452
  }
453
+ }
411
454
 
412
- if (align === 'start') {
413
- _this._scrollToOffset(toOffset, true);
414
- } else if (align === 'end') {
415
- _this._scrollToOffset(toOffset - size, true);
416
- } else if (align === 'center') {
417
- _this._scrollToOffset(toOffset - size / 2, true);
418
- }
455
+ const options = {
456
+ canSmooth: smoothScroll,
457
+ sync: false,
458
+ requested: true
419
459
  };
420
460
 
421
- attempt();
422
- requestAnimationFrame(() => {
423
- attempt();
424
- });
461
+ if (align === 'start') {
462
+ _this._scrollToOffset(toOffset, options);
463
+ } else if (align === 'end') {
464
+ _this._scrollToOffset(toOffset - size, options);
465
+ } else if (align === 'center') {
466
+ _this._scrollToOffset(toOffset - size / 2, options);
467
+ }
425
468
  };
426
469
 
427
470
  this.scrollToIndex = function (index, _temp2) {
428
471
  let {
429
- align,
472
+ align = 'auto',
473
+ smoothScroll = _this.options.enableSmoothScroll,
430
474
  ...rest
431
- } = _temp2 === void 0 ? {
432
- align: 'auto'
433
- } : _temp2;
475
+ } = _temp2 === void 0 ? {} : _temp2;
434
476
 
435
477
  const measurements = _this.getMeasurements();
436
478
 
@@ -461,6 +503,7 @@ class Virtualizer {
461
503
 
462
504
  _this.scrollToOffset(toOffset, {
463
505
  align,
506
+ smoothScroll,
464
507
  ...rest
465
508
  });
466
509
  };
@@ -471,10 +514,22 @@ class Virtualizer {
471
514
  return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
472
515
  };
473
516
 
474
- this._scrollToOffset = (offset, canSmooth) => {
517
+ this._scrollToOffset = (offset, _ref4) => {
518
+ let {
519
+ requested,
520
+ canSmooth,
521
+ sync
522
+ } = _ref4;
475
523
  clearTimeout(this.scrollCheckFrame);
476
- this.destinationOffset = offset;
477
- this.options.scrollToFn(offset, this.options.enableSmoothScroll && canSmooth, this);
524
+
525
+ if (requested) {
526
+ this.destinationOffset = offset;
527
+ }
528
+
529
+ this.options.scrollToFn(offset, {
530
+ canSmooth,
531
+ sync
532
+ }, this);
478
533
  let scrollCheckFrame;
479
534
 
480
535
  const check = () => {
@@ -505,6 +560,7 @@ class Virtualizer {
505
560
  this.setOptions(_opts);
506
561
  this.scrollRect = this.options.initialRect;
507
562
  this.scrollOffset = this.options.initialOffset;
563
+ this.calculateRange();
508
564
  }
509
565
 
510
566
  }
@@ -530,12 +586,12 @@ const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
530
586
  }
531
587
  };
532
588
 
533
- function calculateRange(_ref2) {
589
+ function calculateRange(_ref5) {
534
590
  let {
535
591
  measurements,
536
592
  outerSize,
537
593
  scrollOffset
538
- } = _ref2;
594
+ } = _ref5;
539
595
  const count = measurements.length - 1;
540
596
 
541
597
  const getOffset = index => measurements[index].start;