@tanstack/react-virtual 3.0.0-beta.29 → 3.0.0-beta.32

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];
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];
170
213
  cb(Math.max(0, offset - instance.options.scrollMargin));
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,388 +227,404 @@
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 adjustments = _ref.adjustments,
243
+ behavior = _ref.behavior,
244
+ sync = _ref.sync;
245
+ var toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments != null ? adjustments : 0);
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 adjustments = _ref2.adjustments,
251
+ behavior = _ref2.behavior,
252
+ sync = _ref2.sync;
253
+ var toOffset = (sync ? offset : offset + instance.options.scrollMargin) + (adjustments != null ? adjustments : 0);
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.itemMeasurementsCache = {};
264
+ this.pendingMeasuredCacheIndexes = [];
265
+ this.scrollAdjustments = 0;
266
+ this.measureElementCache = {};
267
+ this.pendingScrollToIndexCallback = null;
268
+ this.getResizeObserver = function () {
269
+ var _ro = null;
270
+ return function () {
271
+ if (_ro) {
272
+ return _ro;
273
+ } else if (typeof ResizeObserver !== 'undefined') {
274
+ return _ro = new ResizeObserver(function (entries) {
275
+ entries.forEach(function (entry) {
276
+ _this._measureElement(entry.target, false);
246
277
  });
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
278
  });
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
- if (this.scrollOffset !== offset) {
323
- this.scrollOffset = offset;
324
- this.isScrolling = true;
325
- this.scrollAdjustments = 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.scrollAdjustments = 0;
334
- }
335
- this.calculateRange();
336
- }));
337
- } else if (!this.isScrolling) {
338
- this.calculateRange();
279
+ } else {
280
+ return null;
339
281
  }
340
282
  };
341
- this.getSize = () => {
342
- return this.scrollRect[this.options.horizontal ? 'width' : 'height'];
343
- };
344
- this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.options.getItemKey, this.itemMeasurementsCache], (count, paddingStart, getItemKey, measurementsCache) => {
345
- const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
346
- this.pendingMeasuredCacheIndexes = [];
347
- const measurements = this.measurementsCache.slice(0, min);
348
- for (let i = min; i < count; i++) {
349
- const key = getItemKey(i);
350
- const measuredSize = measurementsCache[key];
351
- const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
352
- const size = typeof measuredSize === 'number' ? measuredSize : this.options.estimateSize(i);
353
- const end = start + size;
354
- measurements[i] = {
355
- index: i,
356
- start,
357
- size,
358
- end,
359
- key
360
- };
361
- }
362
- this.measurementsCache = measurements;
363
- return measurements;
364
- }, {
365
- key: 'getMeasurements',
366
- debug: () => this.options.debug
283
+ }();
284
+ this.range = {
285
+ startIndex: 0,
286
+ endIndex: 0
287
+ };
288
+ this.setOptions = function (opts) {
289
+ Object.entries(opts).forEach(function (_ref3) {
290
+ var key = _ref3[0],
291
+ value = _ref3[1];
292
+ if (typeof value === 'undefined') delete opts[key];
367
293
  });
368
- this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
369
- const range = calculateRange({
370
- measurements,
371
- outerSize,
372
- scrollOffset
373
- });
374
- if (range.startIndex !== this.range.startIndex || range.endIndex !== this.range.endIndex) {
375
- this.range = range;
376
- this.notify();
377
- }
378
- return this.range;
379
- }, {
380
- key: 'calculateRange',
381
- debug: () => this.options.debug
294
+ _this.options = _extends({
295
+ debug: false,
296
+ initialOffset: 0,
297
+ overscan: 1,
298
+ paddingStart: 0,
299
+ paddingEnd: 0,
300
+ scrollPaddingStart: 0,
301
+ scrollPaddingEnd: 0,
302
+ horizontal: false,
303
+ getItemKey: defaultKeyExtractor,
304
+ rangeExtractor: defaultRangeExtractor,
305
+ onChange: function onChange() {},
306
+ measureElement: measureElement,
307
+ initialRect: {
308
+ width: 0,
309
+ height: 0
310
+ },
311
+ scrollMargin: 0,
312
+ scrollingDelay: 150,
313
+ indexAttribute: 'data-index'
314
+ }, opts);
315
+ };
316
+ this.notify = function () {
317
+ _this.options.onChange == null ? void 0 : _this.options.onChange(_this);
318
+ };
319
+ this.cleanup = function () {
320
+ _this.unsubs.filter(Boolean).forEach(function (d) {
321
+ return d();
382
322
  });
383
- this.getIndexes = memo(() => [this.options.rangeExtractor, this.range, this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
384
- return rangeExtractor({
385
- ...range,
386
- overscan,
387
- count: count
388
- });
389
- }, {
390
- key: 'getIndexes',
391
- debug: () => this.options.debug
323
+ _this.unsubs = [];
324
+ _this.scrollElement = null;
325
+ };
326
+ this._didMount = function () {
327
+ var ro = _this.getResizeObserver();
328
+ Object.values(_this.measureElementCache).forEach(function (node) {
329
+ return ro == null ? void 0 : ro.observe(node);
392
330
  });
393
- this.indexFromElement = node => {
394
- const attributeName = this.options.indexAttribute;
395
- const indexStr = node.getAttribute(attributeName);
396
- if (!indexStr) {
397
- console.warn(`Missing attribute name '${attributeName}={index}' on measured element.`);
398
- return -1;
399
- }
400
- return parseInt(indexStr, 10);
331
+ return function () {
332
+ ro == null ? void 0 : ro.disconnect();
333
+ _this.cleanup();
401
334
  };
402
- this._measureElement = (node, _sync) => {
403
- const index = this.indexFromElement(node);
404
- const item = this.measurementsCache[index];
405
- if (!item) {
406
- return;
407
- }
408
- const prevNode = this.measureElementCache[item.key];
409
- const ro = this.getResizeObserver();
410
- if (!node.isConnected) {
411
- if (prevNode) {
412
- ro == null ? void 0 : ro.unobserve(prevNode);
413
- delete this.measureElementCache[item.key];
414
- }
415
- return;
416
- }
417
- if (!prevNode || prevNode !== node) {
418
- if (prevNode) {
419
- ro == null ? void 0 : ro.unobserve(prevNode);
335
+ };
336
+ this._willUpdate = function () {
337
+ _this.pendingScrollToIndexCallback == null ? void 0 : _this.pendingScrollToIndexCallback();
338
+ var scrollElement = _this.options.getScrollElement();
339
+ if (_this.scrollElement !== scrollElement) {
340
+ _this.cleanup();
341
+ _this.scrollElement = scrollElement;
342
+ _this._scrollToOffset(_this.scrollOffset, {
343
+ adjustments: undefined,
344
+ behavior: undefined,
345
+ sync: true
346
+ });
347
+ _this.unsubs.push(_this.options.observeElementRect(_this, function (rect) {
348
+ _this.scrollRect = rect;
349
+ _this.calculateRange();
350
+ }));
351
+ _this.unsubs.push(_this.options.observeElementOffset(_this, function (offset) {
352
+ if (_this.isScrollingTimeoutId !== null) {
353
+ clearTimeout(_this.isScrollingTimeoutId);
354
+ _this.isScrollingTimeoutId = null;
420
355
  }
421
- this.measureElementCache[item.key] = node;
422
- ro == null ? void 0 : ro.observe(node);
423
- }
424
- const measuredItemSize = this.options.measureElement(node, this);
425
- const itemSize = this.itemMeasurementsCache[item.key] ?? item.size;
426
- const delta = measuredItemSize - itemSize;
427
- if (delta !== 0) {
428
- if (item.start < this.scrollOffset && this.isScrolling) {
429
- if (this.options.debug) {
430
- console.info('correction', delta);
356
+ var onIsScrollingChange = function onIsScrollingChange(isScrolling) {
357
+ if (_this.isScrolling !== isScrolling) {
358
+ _this.isScrolling = isScrolling;
359
+ _this.notify();
431
360
  }
432
- this._scrollToOffset(this.scrollOffset, {
433
- adjustments: this.scrollAdjustments += delta,
434
- behavior: undefined,
435
- sync: false
436
- });
437
- }
438
- this.pendingMeasuredCacheIndexes.push(index);
439
- this.itemMeasurementsCache = {
440
- ...this.itemMeasurementsCache,
441
- [item.key]: measuredItemSize
442
361
  };
443
- this.notify();
444
- }
445
- };
446
- this.measureElement = node => {
447
- if (!node) {
448
- return;
449
- }
450
- this._measureElement(node, true);
451
- };
452
- this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements()], (indexes, measurements) => {
453
- const virtualItems = [];
454
- for (let k = 0, len = indexes.length; k < len; k++) {
455
- const i = indexes[k];
456
- const measurement = measurements[i];
457
- virtualItems.push(measurement);
458
- }
459
- return virtualItems;
460
- }, {
461
- key: 'getIndexes',
462
- debug: () => this.options.debug
463
- });
464
- this.scrollToOffset = function (toOffset, _temp) {
465
- let {
466
- align = 'start',
467
- behavior
468
- } = _temp === void 0 ? {} : _temp;
469
- const offset = _this.scrollOffset;
470
- const size = _this.getSize();
471
- if (align === 'auto') {
472
- if (toOffset <= offset) {
473
- align = 'start';
474
- } else if (toOffset >= offset + size) {
475
- align = 'end';
476
- } else {
477
- align = 'start';
362
+ _this.scrollAdjustments = 0;
363
+ if (_this.scrollOffset !== offset) {
364
+ _this.scrollOffset = offset;
365
+ onIsScrollingChange(true);
478
366
  }
479
- }
480
- const options = {
481
- adjustments: undefined,
482
- behavior,
483
- sync: false
367
+ _this.calculateRange();
368
+ _this.isScrollingTimeoutId = setTimeout(function () {
369
+ _this.isScrollingTimeoutId = null;
370
+ onIsScrollingChange(false);
371
+ }, _this.options.scrollingDelay);
372
+ }));
373
+ } else if (!_this.isScrolling) {
374
+ _this.calculateRange();
375
+ }
376
+ };
377
+ this.getSize = function () {
378
+ return _this.scrollRect[_this.options.horizontal ? 'width' : 'height'];
379
+ };
380
+ this.getMeasurements = memo(function () {
381
+ return [_this.options.count, _this.options.paddingStart, _this.options.getItemKey, _this.itemMeasurementsCache];
382
+ }, function (count, paddingStart, getItemKey, measurementsCache) {
383
+ var min = _this.pendingMeasuredCacheIndexes.length > 0 ? Math.min.apply(Math, _this.pendingMeasuredCacheIndexes) : 0;
384
+ _this.pendingMeasuredCacheIndexes = [];
385
+ var measurements = _this.measurementsCache.slice(0, min);
386
+ for (var _i2 = min; _i2 < count; _i2++) {
387
+ var key = getItemKey(_i2);
388
+ var measuredSize = measurementsCache[key];
389
+ var start = measurements[_i2 - 1] ? measurements[_i2 - 1].end : paddingStart;
390
+ var size = typeof measuredSize === 'number' ? measuredSize : _this.options.estimateSize(_i2);
391
+ var end = start + size;
392
+ measurements[_i2] = {
393
+ index: _i2,
394
+ start: start,
395
+ size: size,
396
+ end: end,
397
+ key: key
484
398
  };
485
- if (align === 'start') {
486
- _this._scrollToOffset(toOffset, options);
487
- } else if (align === 'end') {
488
- _this._scrollToOffset(toOffset - size, options);
489
- } else if (align === 'center') {
490
- _this._scrollToOffset(toOffset - size / 2, options);
399
+ }
400
+ _this.measurementsCache = measurements;
401
+ return measurements;
402
+ }, {
403
+ key: 'getMeasurements',
404
+ debug: function debug() {
405
+ return _this.options.debug;
406
+ }
407
+ });
408
+ this.calculateRange = memo(function () {
409
+ return [_this.getMeasurements(), _this.getSize(), _this.scrollOffset];
410
+ }, function (measurements, outerSize, scrollOffset) {
411
+ var range = calculateRange({
412
+ measurements: measurements,
413
+ outerSize: outerSize,
414
+ scrollOffset: scrollOffset
415
+ });
416
+ if (range.startIndex !== _this.range.startIndex || range.endIndex !== _this.range.endIndex) {
417
+ _this.range = range;
418
+ _this.notify();
419
+ }
420
+ return _this.range;
421
+ }, {
422
+ key: 'calculateRange',
423
+ debug: function debug() {
424
+ return _this.options.debug;
425
+ }
426
+ });
427
+ this.getIndexes = memo(function () {
428
+ return [_this.options.rangeExtractor, _this.range, _this.options.overscan, _this.options.count];
429
+ }, function (rangeExtractor, range, overscan, count) {
430
+ return rangeExtractor(_extends({}, range, {
431
+ overscan: overscan,
432
+ count: count
433
+ }));
434
+ }, {
435
+ key: 'getIndexes',
436
+ debug: function debug() {
437
+ return _this.options.debug;
438
+ }
439
+ });
440
+ this.indexFromElement = function (node) {
441
+ var attributeName = _this.options.indexAttribute;
442
+ var indexStr = node.getAttribute(attributeName);
443
+ if (!indexStr) {
444
+ console.warn("Missing attribute name '" + attributeName + "={index}' on measured element.");
445
+ return -1;
446
+ }
447
+ return parseInt(indexStr, 10);
448
+ };
449
+ this._measureElement = function (node, _sync) {
450
+ var _this$itemMeasurement;
451
+ var index = _this.indexFromElement(node);
452
+ var item = _this.measurementsCache[index];
453
+ if (!item) {
454
+ return;
455
+ }
456
+ var prevNode = _this.measureElementCache[item.key];
457
+ var ro = _this.getResizeObserver();
458
+ if (!node.isConnected) {
459
+ if (prevNode) {
460
+ ro == null ? void 0 : ro.unobserve(prevNode);
461
+ delete _this.measureElementCache[item.key];
491
462
  }
492
- };
493
- this.scrollToIndex = function (index, _temp2) {
494
- let {
495
- align = 'auto',
496
- ...rest
497
- } = _temp2 === void 0 ? {} : _temp2;
498
- _this.pendingScrollToIndexCallback = null;
499
- const measurements = _this.getMeasurements();
500
- const offset = _this.scrollOffset;
501
- const size = _this.getSize();
502
- const {
503
- count
504
- } = _this.options;
505
- const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
506
- if (!measurement) {
507
- return;
463
+ return;
464
+ }
465
+ if (!prevNode || prevNode !== node) {
466
+ if (prevNode) {
467
+ ro == null ? void 0 : ro.unobserve(prevNode);
508
468
  }
509
- if (align === 'auto') {
510
- if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
511
- align = 'end';
512
- } else if (measurement.start <= offset + _this.options.scrollPaddingStart) {
513
- align = 'start';
514
- } else {
515
- return;
469
+ _this.measureElementCache[item.key] = node;
470
+ ro == null ? void 0 : ro.observe(node);
471
+ }
472
+ var measuredItemSize = _this.options.measureElement(node, _this);
473
+ var itemSize = (_this$itemMeasurement = _this.itemMeasurementsCache[item.key]) != null ? _this$itemMeasurement : item.size;
474
+ var delta = measuredItemSize - itemSize;
475
+ if (delta !== 0) {
476
+ var _extends2;
477
+ if (item.start < _this.scrollOffset && _this.isScrolling) {
478
+ if (_this.options.debug) {
479
+ console.info('correction', delta);
516
480
  }
481
+ _this._scrollToOffset(_this.scrollOffset, {
482
+ adjustments: _this.scrollAdjustments += delta,
483
+ behavior: undefined,
484
+ sync: false
485
+ });
517
486
  }
518
- const toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
519
- _this.scrollToOffset(toOffset, {
520
- align,
521
- ...rest
522
- });
523
- const isDynamic = Object.keys(_this.measureElementCache).length > 0;
524
- if (isDynamic) {
525
- const didSeen = () => typeof _this.itemMeasurementsCache[_this.options.getItemKey(index)] === 'number';
526
- if (!didSeen()) {
527
- _this.pendingScrollToIndexCallback = () => {
528
- if (didSeen()) {
529
- _this.pendingScrollToIndexCallback = null;
530
- _this.scrollToIndex(index, {
531
- align,
532
- ...rest
533
- });
534
- }
535
- };
536
- }
487
+ _this.pendingMeasuredCacheIndexes.push(index);
488
+ _this.itemMeasurementsCache = _extends({}, _this.itemMeasurementsCache, (_extends2 = {}, _extends2[item.key] = measuredItemSize, _extends2));
489
+ _this.notify();
490
+ }
491
+ };
492
+ this.measureElement = function (node) {
493
+ if (!node) {
494
+ return;
495
+ }
496
+ _this._measureElement(node, true);
497
+ };
498
+ this.getVirtualItems = memo(function () {
499
+ return [_this.getIndexes(), _this.getMeasurements()];
500
+ }, function (indexes, measurements) {
501
+ var virtualItems = [];
502
+ for (var k = 0, len = indexes.length; k < len; k++) {
503
+ var _i3 = indexes[k];
504
+ var measurement = measurements[_i3];
505
+ virtualItems.push(measurement);
506
+ }
507
+ return virtualItems;
508
+ }, {
509
+ key: 'getIndexes',
510
+ debug: function debug() {
511
+ return _this.options.debug;
512
+ }
513
+ });
514
+ this.getOffsetForAlignment = function (toOffset, align) {
515
+ var offset = _this.scrollOffset;
516
+ var size = _this.getSize();
517
+ if (align === 'auto') {
518
+ if (toOffset <= offset) {
519
+ align = 'start';
520
+ } else if (toOffset >= offset + size) {
521
+ align = 'end';
522
+ } else {
523
+ align = 'start';
537
524
  }
525
+ }
526
+ if (align === 'start') {
527
+ return toOffset;
528
+ } else if (align === 'end') {
529
+ return toOffset - size;
530
+ } else if (align === 'center') {
531
+ return toOffset - size / 2;
532
+ }
533
+ return toOffset;
534
+ };
535
+ this.scrollToOffset = function (toOffset, _temp) {
536
+ var _ref4 = _temp === void 0 ? {} : _temp,
537
+ _ref4$align = _ref4.align,
538
+ align = _ref4$align === void 0 ? 'start' : _ref4$align,
539
+ behavior = _ref4.behavior;
540
+ var options = {
541
+ adjustments: undefined,
542
+ behavior: behavior,
543
+ sync: false
538
544
  };
539
- this.getTotalSize = () => {
540
- var _this$getMeasurements;
541
- return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
542
- };
543
- this._scrollToOffset = (offset, _ref4) => {
544
- let {
545
- adjustments,
546
- behavior,
547
- sync
548
- } = _ref4;
549
- this.options.scrollToFn(offset, {
550
- behavior,
551
- sync,
552
- adjustments
553
- }, this);
545
+ _this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), options);
546
+ };
547
+ this.scrollToIndex = function (index, _temp2) {
548
+ var _ref5 = _temp2 === void 0 ? {} : _temp2,
549
+ _ref5$align = _ref5.align,
550
+ align = _ref5$align === void 0 ? 'auto' : _ref5$align,
551
+ behavior = _ref5.behavior;
552
+ _this.pendingScrollToIndexCallback = null;
553
+ var offset = _this.scrollOffset;
554
+ var size = _this.getSize();
555
+ var count = _this.options.count;
556
+ var measurements = _this.getMeasurements();
557
+ var measurement = measurements[Math.max(0, Math.min(index, count - 1))];
558
+ if (!measurement) {
559
+ throw new Error("VirtualItem not found for index = " + index);
560
+ }
561
+ if (align === 'auto') {
562
+ if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
563
+ align = 'end';
564
+ } else if (measurement.start <= offset + _this.options.scrollPaddingStart) {
565
+ align = 'start';
566
+ } else {
567
+ return;
568
+ }
569
+ }
570
+ var getOffsetForIndexAndAlignment = function getOffsetForIndexAndAlignment(measurement) {
571
+ var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
572
+ return _this.getOffsetForAlignment(toOffset, align);
554
573
  };
555
- this.measure = () => {
556
- this.itemMeasurementsCache = {};
557
- this.notify();
574
+ var toOffset = getOffsetForIndexAndAlignment(measurement);
575
+ if (toOffset === offset) {
576
+ return;
577
+ }
578
+ var options = {
579
+ adjustments: undefined,
580
+ behavior: behavior,
581
+ sync: false
558
582
  };
559
- this.setOptions(_opts);
560
- this.scrollRect = this.options.initialRect;
561
- this.scrollOffset = this.options.initialOffset;
562
- this.calculateRange();
563
- }
564
- }
565
- const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
583
+ _this._scrollToOffset(toOffset, options);
584
+ var isDynamic = Object.keys(_this.measureElementCache).length > 0;
585
+ if (isDynamic) {
586
+ _this.pendingScrollToIndexCallback = function () {
587
+ _this.scrollToIndex(index, {
588
+ align: align,
589
+ behavior: behavior
590
+ });
591
+ };
592
+ }
593
+ };
594
+ this.scrollBy = function (adjustments, options) {
595
+ _this._scrollToOffset(_this.scrollOffset, {
596
+ adjustments: adjustments,
597
+ behavior: options == null ? void 0 : options.behavior,
598
+ sync: false
599
+ });
600
+ };
601
+ this.getTotalSize = function () {
602
+ var _this$getMeasurements;
603
+ return (((_this$getMeasurements = _this.getMeasurements()[_this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || _this.options.paddingStart) + _this.options.paddingEnd;
604
+ };
605
+ this._scrollToOffset = function (offset, _ref6) {
606
+ var adjustments = _ref6.adjustments,
607
+ behavior = _ref6.behavior,
608
+ sync = _ref6.sync;
609
+ _this.options.scrollToFn(offset, {
610
+ behavior: behavior,
611
+ sync: sync,
612
+ adjustments: adjustments
613
+ }, _this);
614
+ };
615
+ this.measure = function () {
616
+ _this.itemMeasurementsCache = {};
617
+ _this.notify();
618
+ };
619
+ this.setOptions(_opts);
620
+ this.scrollRect = this.options.initialRect;
621
+ this.scrollOffset = this.options.initialOffset;
622
+ this.calculateRange();
623
+ };
624
+ var findNearestBinarySearch = function findNearestBinarySearch(low, high, getCurrentValue, value) {
566
625
  while (low <= high) {
567
- const middle = (low + high) / 2 | 0;
568
- const currentValue = getCurrentValue(middle);
626
+ var middle = (low + high) / 2 | 0;
627
+ var currentValue = getCurrentValue(middle);
569
628
  if (currentValue < value) {
570
629
  low = middle + 1;
571
630
  } else if (currentValue > value) {
@@ -580,63 +639,67 @@
580
639
  return 0;
581
640
  }
582
641
  };
583
- function calculateRange(_ref5) {
584
- let {
585
- measurements,
586
- outerSize,
587
- scrollOffset
588
- } = _ref5;
589
- const count = measurements.length - 1;
590
- const getOffset = index => measurements[index].start;
591
- const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
592
- let endIndex = startIndex;
642
+ function calculateRange(_ref7) {
643
+ var measurements = _ref7.measurements,
644
+ outerSize = _ref7.outerSize,
645
+ scrollOffset = _ref7.scrollOffset;
646
+ var count = measurements.length - 1;
647
+ var getOffset = function getOffset(index) {
648
+ return measurements[index].start;
649
+ };
650
+ var startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
651
+ var endIndex = startIndex;
593
652
  while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
594
653
  endIndex++;
595
654
  }
596
655
  return {
597
- startIndex,
598
- endIndex
656
+ startIndex: startIndex,
657
+ endIndex: endIndex
599
658
  };
600
659
  }
601
660
 
602
661
  //
603
662
 
604
- const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
663
+ var useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
605
664
  function useVirtualizerBase(options) {
606
- const rerender = React__namespace.useReducer(() => ({}), {})[1];
607
- const resolvedOptions = {
608
- ...options,
609
- onChange: instance => {
665
+ var rerender = React__namespace.useReducer(function () {
666
+ return {};
667
+ }, {})[1];
668
+ var resolvedOptions = _extends$1({}, options, {
669
+ onChange: function onChange(instance) {
610
670
  rerender();
611
671
  options.onChange == null ? void 0 : options.onChange(instance);
612
672
  }
613
- };
614
- const [instance] = React__namespace.useState(() => new Virtualizer(resolvedOptions));
673
+ });
674
+ var _React$useState = React__namespace.useState(function () {
675
+ return new Virtualizer(resolvedOptions);
676
+ }),
677
+ instance = _React$useState[0];
615
678
  instance.setOptions(resolvedOptions);
616
- React__namespace.useEffect(() => {
679
+ React__namespace.useEffect(function () {
617
680
  return instance._didMount();
618
681
  }, []);
619
- useIsomorphicLayoutEffect(() => {
682
+ useIsomorphicLayoutEffect(function () {
620
683
  return instance._willUpdate();
621
684
  });
622
685
  return instance;
623
686
  }
624
687
  function useVirtualizer(options) {
625
- return useVirtualizerBase({
688
+ return useVirtualizerBase(_extends$1({
626
689
  observeElementRect: observeElementRect,
627
690
  observeElementOffset: observeElementOffset,
628
- scrollToFn: elementScroll,
629
- ...options
630
- });
691
+ scrollToFn: elementScroll
692
+ }, options));
631
693
  }
632
694
  function useWindowVirtualizer(options) {
633
- return useVirtualizerBase({
634
- getScrollElement: () => typeof window !== 'undefined' ? window : null,
695
+ return useVirtualizerBase(_extends$1({
696
+ getScrollElement: function getScrollElement() {
697
+ return typeof window !== 'undefined' ? window : null;
698
+ },
635
699
  observeElementRect: observeWindowRect,
636
700
  observeElementOffset: observeWindowOffset,
637
- scrollToFn: windowScroll,
638
- ...options
639
- });
701
+ scrollToFn: windowScroll
702
+ }, options));
640
703
  }
641
704
 
642
705
  exports.Virtualizer = Virtualizer;