@tanstack/react-virtual 3.0.0-beta.30 → 3.0.0-beta.33

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