@tanstack/react-virtual 3.0.0-beta.22 → 3.0.0-beta.26

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.
@@ -52,53 +52,59 @@
52
52
  if (opts.key && opts.debug != null && opts.debug()) depTime = Date.now();
53
53
  const newDeps = getDeps();
54
54
  const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => deps[index] !== dep);
55
-
56
55
  if (!depsChanged) {
57
56
  return result;
58
57
  }
59
-
60
58
  deps = newDeps;
61
59
  let resultTime;
62
60
  if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now();
63
61
  result = fn(...newDeps);
64
62
  opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result);
65
-
66
63
  if (opts.key && opts.debug != null && opts.debug()) {
67
64
  const depEndTime = Math.round((Date.now() - depTime) * 100) / 100;
68
65
  const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100;
69
66
  const resultFpsPercentage = resultEndTime / 16;
70
-
71
67
  const pad = (str, num) => {
72
68
  str = String(str);
73
-
74
69
  while (str.length < num) {
75
70
  str = ' ' + str;
76
71
  }
77
-
78
72
  return str;
79
73
  };
80
-
81
- console.info("%c\u23F1 " + pad(resultEndTime, 5) + " /" + pad(depEndTime, 5) + " ms", "\n font-size: .6rem;\n font-weight: bold;\n color: hsl(" + Math.max(0, Math.min(120 - 120 * resultFpsPercentage, 120)) + "deg 100% 31%);", opts == null ? void 0 : opts.key);
74
+ console.info(`%c⏱ ${pad(resultEndTime, 5)} /${pad(depEndTime, 5)} ms`, `
75
+ font-size: .6rem;
76
+ font-weight: bold;
77
+ color: hsl(${Math.max(0, Math.min(120 - 120 * resultFpsPercentage, 120))}deg 100% 31%);`, opts == null ? void 0 : opts.key);
82
78
  }
83
-
84
79
  return result;
85
80
  };
86
81
  }
87
82
 
83
+ /**
84
+ * virtual-core
85
+ *
86
+ * Copyright (c) TanStack
87
+ *
88
+ * This source code is licensed under the MIT license found in the
89
+ * LICENSE.md file in the root directory of this source tree.
90
+ *
91
+ * @license MIT
92
+ */
93
+
94
+ //
95
+
88
96
  //
97
+
89
98
  const defaultKeyExtractor = index => index;
90
99
  const defaultRangeExtractor = range => {
91
100
  const start = Math.max(range.startIndex - range.overscan, 0);
92
101
  const end = Math.min(range.endIndex + range.overscan, range.count - 1);
93
102
  const arr = [];
94
-
95
103
  for (let i = start; i <= end; i++) {
96
104
  arr.push(i);
97
105
  }
98
-
99
106
  return arr;
100
107
  };
101
-
102
108
  const memoRectCallback = (instance, cb) => {
103
109
  let prev = {
104
110
  height: -1,
@@ -108,25 +114,20 @@
108
114
  if (instance.options.horizontal ? rect.width !== prev.width : rect.height !== prev.height) {
109
115
  cb(rect);
110
116
  }
111
-
112
117
  prev = rect;
113
118
  };
114
119
  };
115
-
116
120
  const observeElementRect = (instance, cb) => {
117
121
  const observer = new ResizeObserver(entries => {
118
122
  var _entries$, _entries$2;
119
-
120
123
  cb({
121
124
  width: (_entries$ = entries[0]) == null ? void 0 : _entries$.contentRect.width,
122
125
  height: (_entries$2 = entries[0]) == null ? void 0 : _entries$2.contentRect.height
123
126
  });
124
127
  });
125
-
126
128
  if (!instance.scrollElement) {
127
129
  return;
128
130
  }
129
-
130
131
  cb(instance.scrollElement.getBoundingClientRect());
131
132
  observer.observe(instance.scrollElement);
132
133
  return () => {
@@ -135,16 +136,13 @@
135
136
  };
136
137
  const observeWindowRect = (instance, cb) => {
137
138
  const memoizedCallback = memoRectCallback(instance, cb);
138
-
139
139
  const onResize = () => memoizedCallback({
140
140
  width: instance.scrollElement.innerWidth,
141
141
  height: instance.scrollElement.innerHeight
142
142
  });
143
-
144
143
  if (!instance.scrollElement) {
145
144
  return;
146
145
  }
147
-
148
146
  onResize();
149
147
  instance.scrollElement.addEventListener('resize', onResize, {
150
148
  capture: false,
@@ -158,38 +156,30 @@
158
156
  element: ['scrollLeft', 'scrollTop'],
159
157
  window: ['scrollX', 'scrollY']
160
158
  };
161
-
162
159
  const createOffsetObserver = mode => {
163
160
  return (instance, cb) => {
164
161
  if (!instance.scrollElement) {
165
162
  return;
166
163
  }
167
-
168
164
  const propX = scrollProps[mode][0];
169
165
  const propY = scrollProps[mode][1];
170
166
  let prevX = instance.scrollElement[propX];
171
167
  let prevY = instance.scrollElement[propY];
172
-
173
168
  const scroll = () => {
174
169
  const offset = instance.scrollElement[instance.options.horizontal ? propX : propY];
175
170
  cb(Math.max(0, offset - instance.options.scrollMargin));
176
171
  };
177
-
178
172
  scroll();
179
-
180
173
  const onScroll = e => {
181
174
  const target = e.currentTarget;
182
175
  const scrollX = target[propX];
183
176
  const scrollY = target[propY];
184
-
185
177
  if (instance.options.horizontal ? prevX - scrollX : prevY - scrollY) {
186
178
  scroll();
187
179
  }
188
-
189
180
  prevX = scrollX;
190
181
  prevY = scrollY;
191
182
  };
192
-
193
183
  instance.scrollElement.addEventListener('scroll', onScroll, {
194
184
  capture: false,
195
185
  passive: true
@@ -199,7 +189,6 @@
199
189
  };
200
190
  };
201
191
  };
202
-
203
192
  const observeElementOffset = createOffsetObserver('element');
204
193
  const observeWindowOffset = createOffsetObserver('window');
205
194
  const measureElement = (element, instance) => {
@@ -207,34 +196,33 @@
207
196
  };
208
197
  const windowScroll = (offset, _ref, instance) => {
209
198
  var _instance$scrollEleme;
210
-
211
199
  let {
212
- canSmooth,
200
+ adjustments,
201
+ behavior,
213
202
  sync
214
203
  } = _ref;
215
- const toOffset = sync ? offset : offset + instance.options.scrollMargin;
204
+ const toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments ?? 0);
216
205
  (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo == null ? void 0 : _instance$scrollEleme.scrollTo({
217
206
  [instance.options.horizontal ? 'left' : 'top']: toOffset,
218
- behavior: canSmooth ? 'smooth' : undefined
207
+ behavior
219
208
  });
220
209
  };
221
210
  const elementScroll = (offset, _ref2, instance) => {
222
211
  var _instance$scrollEleme2;
223
-
224
212
  let {
225
- canSmooth,
213
+ adjustments,
214
+ behavior,
226
215
  sync
227
216
  } = _ref2;
228
- const toOffset = sync ? offset : offset + instance.options.scrollMargin;
217
+ const toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments ?? 0);
229
218
  (_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo == null ? void 0 : _instance$scrollEleme2.scrollTo({
230
219
  [instance.options.horizontal ? 'left' : 'top']: toOffset,
231
- behavior: canSmooth ? 'smooth' : undefined
220
+ behavior
232
221
  });
233
222
  };
234
223
  class Virtualizer {
235
224
  constructor(_opts) {
236
225
  var _this = this;
237
-
238
226
  this.unsubs = [];
239
227
  this.scrollElement = null;
240
228
  this.isScrolling = false;
@@ -242,9 +230,8 @@
242
230
  this.measurementsCache = [];
243
231
  this.itemMeasurementsCache = {};
244
232
  this.pendingMeasuredCacheIndexes = [];
245
- this.scrollDelta = 0;
233
+ this.scrollAdjustments = 0;
246
234
  this.measureElementCache = {};
247
-
248
235
  this.getResizeObserver = (() => {
249
236
  let _ro = null;
250
237
  return () => {
@@ -261,12 +248,10 @@
261
248
  }
262
249
  };
263
250
  })();
264
-
265
251
  this.range = {
266
252
  startIndex: 0,
267
253
  endIndex: 0
268
254
  };
269
-
270
255
  this.setOptions = opts => {
271
256
  Object.entries(opts).forEach(_ref3 => {
272
257
  let [key, value] = _ref3;
@@ -283,7 +268,6 @@
283
268
  horizontal: false,
284
269
  getItemKey: defaultKeyExtractor,
285
270
  rangeExtractor: defaultRangeExtractor,
286
- enableSmoothScroll: true,
287
271
  onChange: () => {},
288
272
  measureElement,
289
273
  initialRect: {
@@ -296,42 +280,33 @@
296
280
  ...opts
297
281
  };
298
282
  };
299
-
300
283
  this.notify = () => {
301
284
  var _this$options$onChang, _this$options;
302
-
303
285
  (_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
304
286
  };
305
-
306
287
  this.cleanup = () => {
307
288
  this.unsubs.filter(Boolean).forEach(d => d());
308
289
  this.unsubs = [];
309
290
  this.scrollElement = null;
310
291
  };
311
-
312
292
  this._didMount = () => {
313
293
  return () => {
314
294
  var _this$getResizeObserv;
315
-
316
295
  (_this$getResizeObserv = this.getResizeObserver()) == null ? void 0 : _this$getResizeObserv.disconnect();
317
296
  this.measureElementCache = {};
318
297
  this.cleanup();
319
298
  };
320
299
  };
321
-
322
300
  this._willUpdate = () => {
323
301
  const scrollElement = this.options.getScrollElement();
324
-
325
302
  if (this.scrollElement !== scrollElement) {
326
303
  this.cleanup();
327
304
  this.scrollElement = scrollElement;
328
-
329
305
  this._scrollToOffset(this.scrollOffset, {
330
- canSmooth: false,
331
- sync: true,
332
- requested: false
306
+ adjustments: undefined,
307
+ behavior: undefined,
308
+ sync: true
333
309
  });
334
-
335
310
  this.unsubs.push(this.options.observeElementRect(this, rect => {
336
311
  this.scrollRect = rect;
337
312
  this.calculateRange();
@@ -341,11 +316,10 @@
341
316
  clearTimeout(this.isScrollingTimeoutId);
342
317
  this.isScrollingTimeoutId = null;
343
318
  }
344
-
345
319
  if (this.scrollOffset !== offset) {
346
320
  this.scrollOffset = offset;
347
321
  this.isScrolling = true;
348
- this.scrollDelta = 0;
322
+ this.scrollAdjustments = 0;
349
323
  this.isScrollingTimeoutId = setTimeout(() => {
350
324
  this.isScrollingTimeoutId = null;
351
325
  this.isScrolling = false;
@@ -353,25 +327,21 @@
353
327
  }, this.options.scrollingDelay);
354
328
  } else {
355
329
  this.isScrolling = false;
356
- this.scrollDelta = 0;
330
+ this.scrollAdjustments = 0;
357
331
  }
358
-
359
332
  this.calculateRange();
360
333
  }));
361
334
  } else if (!this.isScrolling) {
362
335
  this.calculateRange();
363
336
  }
364
337
  };
365
-
366
338
  this.getSize = () => {
367
339
  return this.scrollRect[this.options.horizontal ? 'width' : 'height'];
368
340
  };
369
-
370
341
  this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.options.getItemKey, this.itemMeasurementsCache], (count, paddingStart, getItemKey, measurementsCache) => {
371
342
  const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
372
343
  this.pendingMeasuredCacheIndexes = [];
373
344
  const measurements = this.measurementsCache.slice(0, min);
374
-
375
345
  for (let i = min; i < count; i++) {
376
346
  const key = getItemKey(i);
377
347
  const measuredSize = measurementsCache[key];
@@ -386,7 +356,6 @@
386
356
  key
387
357
  };
388
358
  }
389
-
390
359
  this.measurementsCache = measurements;
391
360
  return measurements;
392
361
  }, {
@@ -399,19 +368,18 @@
399
368
  outerSize,
400
369
  scrollOffset
401
370
  });
402
-
403
371
  if (range.startIndex !== this.range.startIndex || range.endIndex !== this.range.endIndex) {
404
372
  this.range = range;
405
373
  this.notify();
406
374
  }
407
-
408
375
  return this.range;
409
376
  }, {
410
377
  key: 'calculateRange',
411
378
  debug: () => this.options.debug
412
379
  });
413
380
  this.getIndexes = memo(() => [this.options.rangeExtractor, this.range, this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
414
- return rangeExtractor({ ...range,
381
+ return rangeExtractor({
382
+ ...range,
415
383
  overscan,
416
384
  count: count
417
385
  });
@@ -419,110 +387,84 @@
419
387
  key: 'getIndexes',
420
388
  debug: () => this.options.debug
421
389
  });
422
-
423
390
  this.indexFromElement = node => {
424
391
  const attributeName = this.options.indexAttribute;
425
392
  const indexStr = node.getAttribute(attributeName);
426
-
427
393
  if (!indexStr) {
428
- console.warn("Missing attribute name '" + attributeName + "={index}' on measured element.");
394
+ console.warn(`Missing attribute name '${attributeName}={index}' on measured element.`);
429
395
  return -1;
430
396
  }
431
-
432
397
  return parseInt(indexStr, 10);
433
398
  };
434
-
435
399
  this._measureElement = (node, _sync) => {
436
- var _this$itemMeasurement;
437
-
438
400
  const index = this.indexFromElement(node);
439
401
  const item = this.measurementsCache[index];
440
-
441
402
  if (!item) {
442
403
  return;
443
404
  }
444
-
445
405
  const prevNode = this.measureElementCache[item.key];
446
406
  const ro = this.getResizeObserver();
447
-
448
407
  if (!node.isConnected) {
449
408
  if (prevNode) {
450
409
  ro == null ? void 0 : ro.unobserve(prevNode);
451
410
  delete this.measureElementCache[item.key];
452
411
  }
453
-
454
412
  return;
455
413
  }
456
-
457
414
  if (!prevNode || prevNode !== node) {
458
415
  if (prevNode) {
459
416
  ro == null ? void 0 : ro.unobserve(prevNode);
460
417
  }
461
-
462
418
  this.measureElementCache[item.key] = node;
463
419
  ro == null ? void 0 : ro.observe(node);
464
420
  }
465
-
466
421
  const measuredItemSize = this.options.measureElement(node, this);
467
- const itemSize = (_this$itemMeasurement = this.itemMeasurementsCache[item.key]) != null ? _this$itemMeasurement : item.size;
468
-
469
- if (measuredItemSize !== itemSize) {
470
- if (item.start < this.scrollOffset) {
422
+ const itemSize = this.itemMeasurementsCache[item.key] ?? item.size;
423
+ const delta = measuredItemSize - itemSize;
424
+ if (delta !== 0) {
425
+ if (item.start < this.scrollOffset && this.isScrolling) {
471
426
  if (this.options.debug) {
472
- console.info('correction', measuredItemSize - itemSize);
473
- }
474
-
475
- if (this.destinationOffset === undefined) {
476
- this.scrollDelta += measuredItemSize - itemSize;
477
-
478
- this._scrollToOffset(this.scrollOffset + this.scrollDelta, {
479
- canSmooth: false,
480
- sync: false,
481
- requested: false
482
- });
427
+ console.info('correction', delta);
483
428
  }
429
+ this._scrollToOffset(this.scrollOffset, {
430
+ adjustments: this.scrollAdjustments += delta,
431
+ behavior: undefined,
432
+ sync: false
433
+ });
484
434
  }
485
-
486
435
  this.pendingMeasuredCacheIndexes.push(index);
487
- this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
436
+ this.itemMeasurementsCache = {
437
+ ...this.itemMeasurementsCache,
488
438
  [item.key]: measuredItemSize
489
439
  };
490
440
  this.notify();
491
441
  }
492
442
  };
493
-
494
443
  this.measureElement = node => {
495
444
  if (!node) {
496
445
  return;
497
446
  }
498
-
499
447
  this._measureElement(node, true);
500
448
  };
501
-
502
449
  this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements()], (indexes, measurements) => {
503
450
  const virtualItems = [];
504
-
505
451
  for (let k = 0, len = indexes.length; k < len; k++) {
506
452
  const i = indexes[k];
507
453
  const measurement = measurements[i];
508
454
  virtualItems.push(measurement);
509
455
  }
510
-
511
456
  return virtualItems;
512
457
  }, {
513
458
  key: 'getIndexes',
514
459
  debug: () => this.options.debug
515
460
  });
516
-
517
461
  this.scrollToOffset = function (toOffset, _temp) {
518
462
  let {
519
463
  align = 'start',
520
- smoothScroll = _this.options.enableSmoothScroll
464
+ behavior
521
465
  } = _temp === void 0 ? {} : _temp;
522
466
  const offset = _this.scrollOffset;
523
-
524
467
  const size = _this.getSize();
525
-
526
468
  if (align === 'auto') {
527
469
  if (toOffset <= offset) {
528
470
  align = 'start';
@@ -532,13 +474,11 @@
532
474
  align = 'start';
533
475
  }
534
476
  }
535
-
536
477
  const options = {
537
- canSmooth: smoothScroll,
538
- sync: false,
539
- requested: true
478
+ adjustments: undefined,
479
+ behavior,
480
+ sync: false
540
481
  };
541
-
542
482
  if (align === 'start') {
543
483
  _this._scrollToOffset(toOffset, options);
544
484
  } else if (align === 'end') {
@@ -547,29 +487,21 @@
547
487
  _this._scrollToOffset(toOffset - size / 2, options);
548
488
  }
549
489
  };
550
-
551
490
  this.scrollToIndex = function (index, _temp2) {
552
491
  let {
553
492
  align = 'auto',
554
- smoothScroll = _this.options.enableSmoothScroll,
555
493
  ...rest
556
494
  } = _temp2 === void 0 ? {} : _temp2;
557
-
558
495
  const measurements = _this.getMeasurements();
559
-
560
496
  const offset = _this.scrollOffset;
561
-
562
497
  const size = _this.getSize();
563
-
564
498
  const {
565
499
  count
566
500
  } = _this.options;
567
501
  const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
568
-
569
502
  if (!measurement) {
570
503
  return;
571
504
  }
572
-
573
505
  if (align === 'auto') {
574
506
  if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
575
507
  align = 'end';
@@ -579,78 +511,42 @@
579
511
  return;
580
512
  }
581
513
  }
582
-
583
514
  const toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
584
-
585
515
  _this.scrollToOffset(toOffset, {
586
516
  align,
587
- smoothScroll,
588
517
  ...rest
589
518
  });
590
519
  };
591
-
592
520
  this.getTotalSize = () => {
593
521
  var _this$getMeasurements;
594
-
595
522
  return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
596
523
  };
597
-
598
524
  this._scrollToOffset = (offset, _ref4) => {
599
525
  let {
600
- requested,
601
- canSmooth,
526
+ adjustments,
527
+ behavior,
602
528
  sync
603
529
  } = _ref4;
604
- clearTimeout(this.scrollCheckFrame);
605
-
606
- if (requested) {
607
- this.destinationOffset = offset;
608
- }
609
-
610
530
  this.options.scrollToFn(offset, {
611
- canSmooth,
612
- sync
531
+ behavior,
532
+ sync,
533
+ adjustments
613
534
  }, this);
614
- let scrollCheckFrame;
615
-
616
- const check = () => {
617
- let lastOffset = this.scrollOffset;
618
- this.scrollCheckFrame = scrollCheckFrame = setTimeout(() => {
619
- if (this.scrollCheckFrame !== scrollCheckFrame) {
620
- return;
621
- }
622
-
623
- if (this.scrollOffset === lastOffset) {
624
- this.destinationOffset = undefined;
625
- return;
626
- }
627
-
628
- lastOffset = this.scrollOffset;
629
- check();
630
- }, 100);
631
- };
632
-
633
- check();
634
535
  };
635
-
636
536
  this.measure = () => {
637
537
  this.itemMeasurementsCache = {};
638
538
  this.notify();
639
539
  };
640
-
641
540
  this.setOptions(_opts);
642
541
  this.scrollRect = this.options.initialRect;
643
542
  this.scrollOffset = this.options.initialOffset;
644
543
  this.calculateRange();
645
544
  }
646
-
647
545
  }
648
-
649
546
  const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
650
547
  while (low <= high) {
651
548
  const middle = (low + high) / 2 | 0;
652
549
  const currentValue = getCurrentValue(middle);
653
-
654
550
  if (currentValue < value) {
655
551
  low = middle + 1;
656
552
  } else if (currentValue > value) {
@@ -659,14 +555,12 @@
659
555
  return middle;
660
556
  }
661
557
  }
662
-
663
558
  if (low > 0) {
664
559
  return low - 1;
665
560
  } else {
666
561
  return 0;
667
562
  }
668
563
  };
669
-
670
564
  function calculateRange(_ref5) {
671
565
  let {
672
566
  measurements,
@@ -674,27 +568,25 @@
674
568
  scrollOffset
675
569
  } = _ref5;
676
570
  const count = measurements.length - 1;
677
-
678
571
  const getOffset = index => measurements[index].start;
679
-
680
572
  const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
681
573
  let endIndex = startIndex;
682
-
683
574
  while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
684
575
  endIndex++;
685
576
  }
686
-
687
577
  return {
688
578
  startIndex,
689
579
  endIndex
690
580
  };
691
581
  }
692
582
 
693
- const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
583
+ //
694
584
 
585
+ const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
695
586
  function useVirtualizerBase(options) {
696
587
  const rerender = React__namespace.useReducer(() => ({}), {})[1];
697
- const resolvedOptions = { ...options,
588
+ const resolvedOptions = {
589
+ ...options,
698
590
  onChange: instance => {
699
591
  rerender();
700
592
  options.onChange == null ? void 0 : options.onChange(instance);
@@ -710,7 +602,6 @@
710
602
  });
711
603
  return instance;
712
604
  }
713
-
714
605
  function useVirtualizer(options) {
715
606
  return useVirtualizerBase({
716
607
  observeElementRect: observeElementRect,