@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.
@@ -44,76 +44,6 @@
44
44
  *
45
45
  * @license MIT
46
46
  */
47
- var props = ["bottom", "height", "left", "right", "top", "width"];
48
-
49
- var rectChanged = function rectChanged(a, b) {
50
- if (a === void 0) {
51
- a = {};
52
- }
53
-
54
- if (b === void 0) {
55
- b = {};
56
- }
57
-
58
- return props.some(function (prop) {
59
- return a[prop] !== b[prop];
60
- });
61
- };
62
-
63
- var observedNodes = /*#__PURE__*/new Map();
64
- var rafId;
65
-
66
- var run = function run() {
67
- var changedStates = [];
68
- observedNodes.forEach(function (state, node) {
69
- var newRect = node.getBoundingClientRect();
70
-
71
- if (rectChanged(newRect, state.rect)) {
72
- state.rect = newRect;
73
- changedStates.push(state);
74
- }
75
- });
76
- changedStates.forEach(function (state) {
77
- state.callbacks.forEach(function (cb) {
78
- return cb(state.rect);
79
- });
80
- });
81
- rafId = window.requestAnimationFrame(run);
82
- };
83
-
84
- function observeRect(node, cb) {
85
- return {
86
- observe: function observe() {
87
- var wasEmpty = observedNodes.size === 0;
88
-
89
- if (observedNodes.has(node)) {
90
- observedNodes.get(node).callbacks.push(cb);
91
- } else {
92
- observedNodes.set(node, {
93
- rect: undefined,
94
- hasRectChanged: false,
95
- callbacks: [cb]
96
- });
97
- }
98
-
99
- if (wasEmpty) run();
100
- },
101
- unobserve: function unobserve() {
102
- var state = observedNodes.get(node);
103
-
104
- if (state) {
105
- // Remove the callback
106
- var index = state.callbacks.indexOf(cb);
107
- if (index >= 0) state.callbacks.splice(index, 1); // Remove the node reference
108
-
109
- if (!state.callbacks.length) observedNodes["delete"](node); // Stop the loop
110
-
111
- if (!observedNodes.size) cancelAnimationFrame(rafId);
112
- }
113
- }
114
- };
115
- }
116
-
117
47
  function memo(getDeps, fn, opts) {
118
48
  let deps = [];
119
49
  let result;
@@ -168,9 +98,29 @@
168
98
 
169
99
  return arr;
170
100
  };
101
+
102
+ const memoRectCallback = (instance, cb) => {
103
+ let prev = {
104
+ height: -1,
105
+ width: -1
106
+ };
107
+ return rect => {
108
+ if (instance.options.horizontal ? rect.width !== prev.width : rect.height !== prev.height) {
109
+ cb(rect);
110
+ }
111
+
112
+ prev = rect;
113
+ };
114
+ };
115
+
171
116
  const observeElementRect = (instance, cb) => {
172
- const observer = observeRect(instance.scrollElement, rect => {
173
- cb(rect);
117
+ const observer = new ResizeObserver(entries => {
118
+ var _entries$, _entries$2;
119
+
120
+ cb({
121
+ width: (_entries$ = entries[0]) == null ? void 0 : _entries$.contentRect.width,
122
+ height: (_entries$2 = entries[0]) == null ? void 0 : _entries$2.contentRect.height
123
+ });
174
124
  });
175
125
 
176
126
  if (!instance.scrollElement) {
@@ -178,18 +128,18 @@
178
128
  }
179
129
 
180
130
  cb(instance.scrollElement.getBoundingClientRect());
181
- observer.observe();
131
+ observer.observe(instance.scrollElement);
182
132
  return () => {
183
- observer.unobserve();
133
+ observer.unobserve(instance.scrollElement);
184
134
  };
185
135
  };
186
136
  const observeWindowRect = (instance, cb) => {
187
- const onResize = () => {
188
- cb({
189
- width: instance.scrollElement.innerWidth,
190
- height: instance.scrollElement.innerHeight
191
- });
192
- };
137
+ const memoizedCallback = memoRectCallback(instance, cb);
138
+
139
+ const onResize = () => memoizedCallback({
140
+ width: instance.scrollElement.innerWidth,
141
+ height: instance.scrollElement.innerHeight
142
+ });
193
143
 
194
144
  if (!instance.scrollElement) {
195
145
  return;
@@ -204,52 +154,80 @@
204
154
  instance.scrollElement.removeEventListener('resize', onResize);
205
155
  };
206
156
  };
207
- const observeElementOffset = (instance, cb) => {
208
- const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollLeft' : 'scrollTop']);
157
+ const scrollProps = {
158
+ element: ['scrollLeft', 'scrollTop'],
159
+ window: ['scrollX', 'scrollY']
160
+ };
209
161
 
210
- if (!instance.scrollElement) {
211
- return;
212
- }
162
+ const createOffsetObserver = mode => {
163
+ return (instance, cb) => {
164
+ if (!instance.scrollElement) {
165
+ return;
166
+ }
213
167
 
214
- onScroll();
215
- instance.scrollElement.addEventListener('scroll', onScroll, {
216
- capture: false,
217
- passive: true
218
- });
219
- return () => {
220
- instance.scrollElement.removeEventListener('scroll', onScroll);
221
- };
222
- };
223
- const observeWindowOffset = (instance, cb) => {
224
- const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollX' : 'scrollY']);
168
+ const propX = scrollProps[mode][0];
169
+ const propY = scrollProps[mode][1];
170
+ let prevX = instance.scrollElement[propX];
171
+ let prevY = instance.scrollElement[propY];
225
172
 
226
- if (!instance.scrollElement) {
227
- return;
228
- }
173
+ const scroll = () => {
174
+ const offset = instance.scrollElement[instance.options.horizontal ? propX : propY];
175
+ cb(Math.max(0, offset - instance.options.scrollMargin));
176
+ };
229
177
 
230
- onScroll();
231
- instance.scrollElement.addEventListener('scroll', onScroll, {
232
- capture: false,
233
- passive: true
234
- });
235
- return () => {
236
- instance.scrollElement.removeEventListener('scroll', onScroll);
178
+ scroll();
179
+
180
+ const onScroll = e => {
181
+ const target = e.currentTarget;
182
+ const scrollX = target[propX];
183
+ const scrollY = target[propY];
184
+
185
+ if (instance.options.horizontal ? prevX - scrollX : prevY - scrollY) {
186
+ scroll();
187
+ }
188
+
189
+ prevX = scrollX;
190
+ prevY = scrollY;
191
+ };
192
+
193
+ instance.scrollElement.addEventListener('scroll', onScroll, {
194
+ capture: false,
195
+ passive: true
196
+ });
197
+ return () => {
198
+ instance.scrollElement.removeEventListener('scroll', onScroll);
199
+ };
237
200
  };
238
201
  };
202
+
203
+ const observeElementOffset = createOffsetObserver('element');
204
+ const observeWindowOffset = createOffsetObserver('window');
239
205
  const measureElement = (element, instance) => {
240
206
  return element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height'];
241
207
  };
242
- const windowScroll = (offset, canSmooth, instance) => {
208
+ const windowScroll = (offset, _ref, instance) => {
243
209
  var _instance$scrollEleme;
244
- (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo({
245
- [instance.options.horizontal ? 'left' : 'top']: offset,
210
+
211
+ let {
212
+ canSmooth,
213
+ sync
214
+ } = _ref;
215
+ const toOffset = sync ? offset : offset + instance.options.scrollMargin;
216
+ (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo == null ? void 0 : _instance$scrollEleme.scrollTo({
217
+ [instance.options.horizontal ? 'left' : 'top']: toOffset,
246
218
  behavior: canSmooth ? 'smooth' : undefined
247
219
  });
248
220
  };
249
- const elementScroll = (offset, canSmooth, instance) => {
221
+ const elementScroll = (offset, _ref2, instance) => {
250
222
  var _instance$scrollEleme2;
251
- (_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo({
252
- [instance.options.horizontal ? 'left' : 'top']: offset,
223
+
224
+ let {
225
+ canSmooth,
226
+ sync
227
+ } = _ref2;
228
+ const toOffset = sync ? offset : offset + instance.options.scrollMargin;
229
+ (_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo == null ? void 0 : _instance$scrollEleme2.scrollTo({
230
+ [instance.options.horizontal ? 'left' : 'top']: toOffset,
253
231
  behavior: canSmooth ? 'smooth' : undefined
254
232
  });
255
233
  };
@@ -259,13 +237,21 @@
259
237
 
260
238
  this.unsubs = [];
261
239
  this.scrollElement = null;
240
+ this.isScrolling = false;
241
+ this.isScrollingTimeoutId = null;
262
242
  this.measurementsCache = [];
263
243
  this.itemMeasurementsCache = {};
264
244
  this.pendingMeasuredCacheIndexes = [];
245
+ this.scrollDelta = 0;
246
+ this.measureElementCache = {};
247
+ this.range = {
248
+ startIndex: 0,
249
+ endIndex: 0
250
+ };
265
251
 
266
252
  this.setOptions = opts => {
267
- Object.entries(opts).forEach(_ref => {
268
- let [key, value] = _ref;
253
+ Object.entries(opts).forEach(_ref3 => {
254
+ let [key, value] = _ref3;
269
255
  if (typeof value === 'undefined') delete opts[key];
270
256
  });
271
257
  this.options = {
@@ -286,6 +272,8 @@
286
272
  width: 0,
287
273
  height: 0
288
274
  },
275
+ scrollMargin: 0,
276
+ scrollingDelay: 150,
289
277
  ...opts
290
278
  };
291
279
  };
@@ -299,6 +287,7 @@
299
287
  this.cleanup = () => {
300
288
  this.unsubs.filter(Boolean).forEach(d => d());
301
289
  this.unsubs = [];
290
+ this.scrollElement = null;
302
291
  };
303
292
 
304
293
  this._didMount = () => {
@@ -313,14 +302,41 @@
313
302
  if (this.scrollElement !== scrollElement) {
314
303
  this.cleanup();
315
304
  this.scrollElement = scrollElement;
305
+
306
+ this._scrollToOffset(this.scrollOffset, {
307
+ canSmooth: false,
308
+ sync: true,
309
+ requested: false
310
+ });
311
+
316
312
  this.unsubs.push(this.options.observeElementRect(this, rect => {
317
313
  this.scrollRect = rect;
318
- this.notify();
314
+ this.calculateRange();
319
315
  }));
320
316
  this.unsubs.push(this.options.observeElementOffset(this, offset => {
321
- this.scrollOffset = offset;
322
- this.notify();
317
+ if (this.isScrollingTimeoutId !== null) {
318
+ clearTimeout(this.isScrollingTimeoutId);
319
+ this.isScrollingTimeoutId = null;
320
+ }
321
+
322
+ if (this.scrollOffset !== offset) {
323
+ this.scrollOffset = offset;
324
+ this.isScrolling = true;
325
+ this.scrollDelta = 0;
326
+ this.isScrollingTimeoutId = setTimeout(() => {
327
+ this.isScrollingTimeoutId = null;
328
+ this.isScrolling = false;
329
+ this.notify();
330
+ }, this.options.scrollingDelay);
331
+ } else {
332
+ this.isScrolling = false;
333
+ this.scrollDelta = 0;
334
+ }
335
+
336
+ this.calculateRange();
323
337
  }));
338
+ } else if (!this.isScrolling) {
339
+ this.calculateRange();
324
340
  }
325
341
  };
326
342
 
@@ -355,104 +371,130 @@
355
371
  debug: () => this.options.debug
356
372
  });
357
373
  this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
358
- return calculateRange({
374
+ const range = calculateRange({
359
375
  measurements,
360
376
  outerSize,
361
377
  scrollOffset
362
378
  });
379
+
380
+ if (range.startIndex !== this.range.startIndex || range.endIndex !== this.range.endIndex) {
381
+ this.range = range;
382
+ this.notify();
383
+ }
384
+
385
+ return this.range;
363
386
  }, {
364
387
  key: 'calculateRange',
365
388
  debug: () => this.options.debug
366
389
  });
367
- this.getIndexes = memo(() => [this.options.rangeExtractor, this.calculateRange(), this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
390
+ this.getIndexes = memo(() => [this.options.rangeExtractor, this.range, this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
368
391
  return rangeExtractor({ ...range,
369
392
  overscan,
370
393
  count: count
371
394
  });
372
395
  }, {
373
- key: 'getIndexes'
396
+ key: 'getIndexes',
397
+ debug: () => this.options.debug
374
398
  });
375
399
  this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements(), this.options.measureElement], (indexes, measurements, measureElement) => {
400
+ const makeMeasureElement = index => measurableItem => {
401
+ var _this$itemMeasurement;
402
+
403
+ const item = this.measurementsCache[index];
404
+
405
+ if (!measurableItem) {
406
+ return;
407
+ }
408
+
409
+ const measuredItemSize = measureElement(measurableItem, this);
410
+ const itemSize = (_this$itemMeasurement = this.itemMeasurementsCache[item.key]) != null ? _this$itemMeasurement : item.size;
411
+
412
+ if (measuredItemSize !== itemSize) {
413
+ if (item.start < this.scrollOffset) {
414
+ if (this.options.debug) {
415
+ console.info('correction', measuredItemSize - itemSize);
416
+ }
417
+
418
+ if (this.destinationOffset === undefined) {
419
+ this.scrollDelta += measuredItemSize - itemSize;
420
+
421
+ this._scrollToOffset(this.scrollOffset + this.scrollDelta, {
422
+ canSmooth: false,
423
+ sync: false,
424
+ requested: false
425
+ });
426
+ }
427
+ }
428
+
429
+ this.pendingMeasuredCacheIndexes.push(index);
430
+ this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
431
+ [item.key]: measuredItemSize
432
+ };
433
+ this.notify();
434
+ }
435
+ };
436
+
376
437
  const virtualItems = [];
438
+ const currentMeasureElements = {};
377
439
 
378
440
  for (let k = 0, len = indexes.length; k < len; k++) {
441
+ var _this$measureElementC;
442
+
379
443
  const i = indexes[k];
380
444
  const measurement = measurements[i];
381
445
  const item = { ...measurement,
382
- measureElement: measurableItem => {
383
- if (measurableItem) {
384
- const measuredItemSize = measureElement(measurableItem, this);
385
-
386
- if (measuredItemSize !== item.size) {
387
- if (item.start < this.scrollOffset) {
388
- if (this.options.debug) console.info('correction', measuredItemSize - item.size);
389
-
390
- if (!this.destinationOffset) {
391
- this._scrollToOffset(this.scrollOffset + (measuredItemSize - item.size), false);
392
- }
393
- }
394
-
395
- this.pendingMeasuredCacheIndexes.push(i);
396
- this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
397
- [item.key]: measuredItemSize
398
- };
399
- this.notify();
400
- }
401
- }
402
- }
446
+ measureElement: currentMeasureElements[i] = (_this$measureElementC = this.measureElementCache[i]) != null ? _this$measureElementC : makeMeasureElement(i)
403
447
  };
404
448
  virtualItems.push(item);
405
449
  }
406
450
 
451
+ this.measureElementCache = currentMeasureElements;
407
452
  return virtualItems;
408
453
  }, {
409
- key: 'getIndexes'
454
+ key: 'getIndexes',
455
+ debug: () => this.options.debug
410
456
  });
411
457
 
412
458
  this.scrollToOffset = function (toOffset, _temp) {
413
459
  let {
414
- align
415
- } = _temp === void 0 ? {
416
- align: 'start'
417
- } : _temp;
418
-
419
- const attempt = () => {
420
- const offset = _this.scrollOffset;
460
+ align = 'start',
461
+ smoothScroll = _this.options.enableSmoothScroll
462
+ } = _temp === void 0 ? {} : _temp;
463
+ const offset = _this.scrollOffset;
421
464
 
422
- const size = _this.getSize();
465
+ const size = _this.getSize();
423
466
 
424
- if (align === 'auto') {
425
- if (toOffset <= offset) {
426
- align = 'start';
427
- } else if (toOffset >= offset + size) {
428
- align = 'end';
429
- } else {
430
- align = 'start';
431
- }
467
+ if (align === 'auto') {
468
+ if (toOffset <= offset) {
469
+ align = 'start';
470
+ } else if (toOffset >= offset + size) {
471
+ align = 'end';
472
+ } else {
473
+ align = 'start';
432
474
  }
475
+ }
433
476
 
434
- if (align === 'start') {
435
- _this._scrollToOffset(toOffset, true);
436
- } else if (align === 'end') {
437
- _this._scrollToOffset(toOffset - size, true);
438
- } else if (align === 'center') {
439
- _this._scrollToOffset(toOffset - size / 2, true);
440
- }
477
+ const options = {
478
+ canSmooth: smoothScroll,
479
+ sync: false,
480
+ requested: true
441
481
  };
442
482
 
443
- attempt();
444
- requestAnimationFrame(() => {
445
- attempt();
446
- });
483
+ if (align === 'start') {
484
+ _this._scrollToOffset(toOffset, options);
485
+ } else if (align === 'end') {
486
+ _this._scrollToOffset(toOffset - size, options);
487
+ } else if (align === 'center') {
488
+ _this._scrollToOffset(toOffset - size / 2, options);
489
+ }
447
490
  };
448
491
 
449
492
  this.scrollToIndex = function (index, _temp2) {
450
493
  let {
451
- align,
494
+ align = 'auto',
495
+ smoothScroll = _this.options.enableSmoothScroll,
452
496
  ...rest
453
- } = _temp2 === void 0 ? {
454
- align: 'auto'
455
- } : _temp2;
497
+ } = _temp2 === void 0 ? {} : _temp2;
456
498
 
457
499
  const measurements = _this.getMeasurements();
458
500
 
@@ -483,6 +525,7 @@
483
525
 
484
526
  _this.scrollToOffset(toOffset, {
485
527
  align,
528
+ smoothScroll,
486
529
  ...rest
487
530
  });
488
531
  };
@@ -493,10 +536,22 @@
493
536
  return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
494
537
  };
495
538
 
496
- this._scrollToOffset = (offset, canSmooth) => {
539
+ this._scrollToOffset = (offset, _ref4) => {
540
+ let {
541
+ requested,
542
+ canSmooth,
543
+ sync
544
+ } = _ref4;
497
545
  clearTimeout(this.scrollCheckFrame);
498
- this.destinationOffset = offset;
499
- this.options.scrollToFn(offset, this.options.enableSmoothScroll && canSmooth, this);
546
+
547
+ if (requested) {
548
+ this.destinationOffset = offset;
549
+ }
550
+
551
+ this.options.scrollToFn(offset, {
552
+ canSmooth,
553
+ sync
554
+ }, this);
500
555
  let scrollCheckFrame;
501
556
 
502
557
  const check = () => {
@@ -527,6 +582,7 @@
527
582
  this.setOptions(_opts);
528
583
  this.scrollRect = this.options.initialRect;
529
584
  this.scrollOffset = this.options.initialOffset;
585
+ this.calculateRange();
530
586
  }
531
587
 
532
588
  }
@@ -552,12 +608,12 @@
552
608
  }
553
609
  };
554
610
 
555
- function calculateRange(_ref2) {
611
+ function calculateRange(_ref5) {
556
612
  let {
557
613
  measurements,
558
614
  outerSize,
559
615
  scrollOffset
560
- } = _ref2;
616
+ } = _ref5;
561
617
  const count = measurements.length - 1;
562
618
 
563
619
  const getOffset = index => measurements[index].start;