@tanstack/react-virtual 3.0.0-beta.30 → 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,390 +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
- 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();
279
+ } else {
280
+ return null;
341
281
  }
342
282
  };
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
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];
369
293
  });
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
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();
384
322
  });
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
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);
394
330
  });
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);
331
+ return function () {
332
+ ro == null ? void 0 : ro.disconnect();
333
+ _this.cleanup();
403
334
  };
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];
416
- }
417
- return;
418
- }
419
- if (!prevNode || prevNode !== node) {
420
- if (prevNode) {
421
- 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;
422
355
  }
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);
356
+ var onIsScrollingChange = function onIsScrollingChange(isScrolling) {
357
+ if (_this.isScrolling !== isScrolling) {
358
+ _this.isScrolling = isScrolling;
359
+ _this.notify();
433
360
  }
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
361
  };
445
- this.notify();
446
- }
447
- };
448
- this.measureElement = node => {
449
- if (!node) {
450
- return;
451
- }
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);
460
- }
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';
362
+ _this.scrollAdjustments = 0;
363
+ if (_this.scrollOffset !== offset) {
364
+ _this.scrollOffset = offset;
365
+ onIsScrollingChange(true);
480
366
  }
481
- }
482
- const options = {
483
- adjustments: undefined,
484
- behavior,
485
- 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
486
398
  };
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);
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];
493
462
  }
494
- };
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) {
509
- return;
463
+ return;
464
+ }
465
+ if (!prevNode || prevNode !== node) {
466
+ if (prevNode) {
467
+ ro == null ? void 0 : ro.unobserve(prevNode);
510
468
  }
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;
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);
518
480
  }
481
+ _this._scrollToOffset(_this.scrollOffset, {
482
+ adjustments: _this.scrollAdjustments += delta,
483
+ behavior: undefined,
484
+ sync: false
485
+ });
519
486
  }
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
- }
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';
539
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
540
544
  };
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);
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);
556
573
  };
557
- this.measure = () => {
558
- this.itemMeasurementsCache = {};
559
- 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
560
582
  };
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) => {
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) {
568
625
  while (low <= high) {
569
- const middle = (low + high) / 2 | 0;
570
- const currentValue = getCurrentValue(middle);
626
+ var middle = (low + high) / 2 | 0;
627
+ var currentValue = getCurrentValue(middle);
571
628
  if (currentValue < value) {
572
629
  low = middle + 1;
573
630
  } else if (currentValue > value) {
@@ -582,63 +639,67 @@
582
639
  return 0;
583
640
  }
584
641
  };
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;
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;
595
652
  while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
596
653
  endIndex++;
597
654
  }
598
655
  return {
599
- startIndex,
600
- endIndex
656
+ startIndex: startIndex,
657
+ endIndex: endIndex
601
658
  };
602
659
  }
603
660
 
604
661
  //
605
662
 
606
- const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
663
+ var useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
607
664
  function useVirtualizerBase(options) {
608
- const rerender = React__namespace.useReducer(() => ({}), {})[1];
609
- const resolvedOptions = {
610
- ...options,
611
- onChange: instance => {
665
+ var rerender = React__namespace.useReducer(function () {
666
+ return {};
667
+ }, {})[1];
668
+ var resolvedOptions = _extends$1({}, options, {
669
+ onChange: function onChange(instance) {
612
670
  rerender();
613
671
  options.onChange == null ? void 0 : options.onChange(instance);
614
672
  }
615
- };
616
- 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];
617
678
  instance.setOptions(resolvedOptions);
618
- React__namespace.useEffect(() => {
679
+ React__namespace.useEffect(function () {
619
680
  return instance._didMount();
620
681
  }, []);
621
- useIsomorphicLayoutEffect(() => {
682
+ useIsomorphicLayoutEffect(function () {
622
683
  return instance._willUpdate();
623
684
  });
624
685
  return instance;
625
686
  }
626
687
  function useVirtualizer(options) {
627
- return useVirtualizerBase({
688
+ return useVirtualizerBase(_extends$1({
628
689
  observeElementRect: observeElementRect,
629
690
  observeElementOffset: observeElementOffset,
630
- scrollToFn: elementScroll,
631
- ...options
632
- });
691
+ scrollToFn: elementScroll
692
+ }, options));
633
693
  }
634
694
  function useWindowVirtualizer(options) {
635
- return useVirtualizerBase({
636
- getScrollElement: () => typeof window !== 'undefined' ? window : null,
695
+ return useVirtualizerBase(_extends$1({
696
+ getScrollElement: function getScrollElement() {
697
+ return typeof window !== 'undefined' ? window : null;
698
+ },
637
699
  observeElementRect: observeWindowRect,
638
700
  observeElementOffset: observeWindowOffset,
639
- scrollToFn: windowScroll,
640
- ...options
641
- });
701
+ scrollToFn: windowScroll
702
+ }, options));
642
703
  }
643
704
 
644
705
  exports.Virtualizer = Virtualizer;