@tanstack/react-virtual 3.0.0-beta.9 → 3.0.1

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.
@@ -9,10 +9,10 @@
9
9
  * @license MIT
10
10
  */
11
11
  (function (global, factory) {
12
- typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react')) :
13
- typeof define === 'function' && define.amd ? define(['exports', 'react'], factory) :
14
- (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactVirtual = {}, global.React));
15
- })(this, (function (exports, React) { 'use strict';
12
+ typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('react'), require('react-dom')) :
13
+ typeof define === 'function' && define.amd ? define(['exports', 'react', 'react-dom'], factory) :
14
+ (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.ReactVirtual = {}, global.React, global.ReactDOM));
15
+ })(this, (function (exports, React, reactDom) { 'use strict';
16
16
 
17
17
  function _interopNamespace(e) {
18
18
  if (e && e.__esModule) return e;
@@ -34,6 +34,21 @@
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
+
37
52
  /**
38
53
  * virtual-core
39
54
  *
@@ -44,546 +59,678 @@
44
59
  *
45
60
  * @license MIT
46
61
  */
47
- var props = ["bottom", "height", "left", "right", "top", "width"];
48
-
49
- var rectChanged = function rectChanged(a, b) {
50
- if (a === void 0) {
51
- a = {};
52
- }
53
-
54
- if (b === void 0) {
55
- b = {};
56
- }
57
-
58
- return props.some(function (prop) {
59
- return a[prop] !== b[prop];
60
- });
61
- };
62
-
63
- var observedNodes = /*#__PURE__*/new Map();
64
- var rafId;
65
-
66
- var run = function run() {
67
- var changedStates = [];
68
- observedNodes.forEach(function (state, node) {
69
- var newRect = node.getBoundingClientRect();
70
-
71
- if (rectChanged(newRect, state.rect)) {
72
- state.rect = newRect;
73
- changedStates.push(state);
74
- }
75
- });
76
- changedStates.forEach(function (state) {
77
- state.callbacks.forEach(function (cb) {
78
- return cb(state.rect);
79
- });
80
- });
81
- rafId = window.requestAnimationFrame(run);
82
- };
83
-
84
- function observeRect(node, cb) {
85
- return {
86
- observe: function observe() {
87
- var wasEmpty = observedNodes.size === 0;
88
-
89
- if (observedNodes.has(node)) {
90
- observedNodes.get(node).callbacks.push(cb);
91
- } else {
92
- observedNodes.set(node, {
93
- rect: undefined,
94
- hasRectChanged: false,
95
- callbacks: [cb]
96
- });
97
- }
98
-
99
- if (wasEmpty) run();
100
- },
101
- unobserve: function unobserve() {
102
- var state = observedNodes.get(node);
103
-
104
- if (state) {
105
- // Remove the callback
106
- var index = state.callbacks.indexOf(cb);
107
- if (index >= 0) state.callbacks.splice(index, 1); // Remove the node reference
108
-
109
- if (!state.callbacks.length) observedNodes["delete"](node); // Stop the loop
110
-
111
- if (!observedNodes.size) cancelAnimationFrame(rafId);
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
+ }
112
70
  }
113
71
  }
72
+ return target;
114
73
  };
74
+ return _extends.apply(this, arguments);
115
75
  }
116
76
 
77
+ /**
78
+ * virtual-core
79
+ *
80
+ * Copyright (c) TanStack
81
+ *
82
+ * This source code is licensed under the MIT license found in the
83
+ * LICENSE.md file in the root directory of this source tree.
84
+ *
85
+ * @license MIT
86
+ */
117
87
  function memo(getDeps, fn, opts) {
118
- let deps = [];
119
- let result;
120
- return () => {
121
- let depTime;
88
+ var _opts$initialDeps;
89
+ var deps = (_opts$initialDeps = opts.initialDeps) != null ? _opts$initialDeps : [];
90
+ var result;
91
+ return function () {
92
+ var depTime;
122
93
  if (opts.key && opts.debug != null && opts.debug()) depTime = Date.now();
123
- const newDeps = getDeps();
124
- const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => deps[index] !== dep);
125
-
94
+ var newDeps = getDeps();
95
+ var depsChanged = newDeps.length !== deps.length || newDeps.some(function (dep, index) {
96
+ return deps[index] !== dep;
97
+ });
126
98
  if (!depsChanged) {
127
99
  return result;
128
100
  }
129
-
130
101
  deps = newDeps;
131
- let resultTime;
102
+ var resultTime;
132
103
  if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now();
133
- result = fn(...newDeps);
134
- opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result);
135
-
104
+ result = fn.apply(void 0, newDeps);
136
105
  if (opts.key && opts.debug != null && opts.debug()) {
137
- const depEndTime = Math.round((Date.now() - depTime) * 100) / 100;
138
- const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100;
139
- const resultFpsPercentage = resultEndTime / 16;
140
-
141
- 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) {
142
110
  str = String(str);
143
-
144
111
  while (str.length < num) {
145
112
  str = ' ' + str;
146
113
  }
147
-
148
114
  return str;
149
115
  };
150
-
151
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);
152
117
  }
153
-
118
+ opts == null || opts.onChange == null || opts.onChange(result);
154
119
  return result;
155
120
  };
156
121
  }
122
+ function notUndefined(value, msg) {
123
+ if (value === undefined) {
124
+ throw new Error("Unexpected undefined" + (msg ? ": " + msg : ''));
125
+ } else {
126
+ return value;
127
+ }
128
+ }
129
+ var approxEqual = function approxEqual(a, b) {
130
+ return Math.abs(a - b) < 1;
131
+ };
132
+
133
+ /**
134
+ * virtual-core
135
+ *
136
+ * Copyright (c) TanStack
137
+ *
138
+ * This source code is licensed under the MIT license found in the
139
+ * LICENSE.md file in the root directory of this source tree.
140
+ *
141
+ * @license MIT
142
+ */
157
143
 
158
144
  //
159
- const defaultKeyExtractor = index => index;
160
- const defaultRangeExtractor = range => {
161
- const start = Math.max(range.startIndex - range.overscan, 0);
162
- const end = Math.min(range.endIndex + range.overscan, range.count - 1);
163
- const arr = [];
164
145
 
165
- for (let i = start; i <= end; i++) {
166
- arr.push(i);
167
- }
146
+ //
168
147
 
148
+ var defaultKeyExtractor = function defaultKeyExtractor(index) {
149
+ return index;
150
+ };
151
+ var defaultRangeExtractor = function defaultRangeExtractor(range) {
152
+ var start = Math.max(range.startIndex - range.overscan, 0);
153
+ var end = Math.min(range.endIndex + range.overscan, range.count - 1);
154
+ var arr = [];
155
+ for (var _i = start; _i <= end; _i++) {
156
+ arr.push(_i);
157
+ }
169
158
  return arr;
170
159
  };
171
-
172
- const memoRectCallback = (instance, cb) => {
173
- let prev = {
174
- height: -1,
175
- width: -1
176
- };
177
- return rect => {
178
- if (instance.options.horizontal ? rect.width !== prev.width : rect.height !== prev.height) {
179
- cb(rect);
160
+ var observeElementRect = function observeElementRect(instance, cb) {
161
+ var element = instance.scrollElement;
162
+ if (!element) {
163
+ return;
164
+ }
165
+ var handler = function handler(rect) {
166
+ var width = rect.width,
167
+ height = rect.height;
168
+ cb({
169
+ width: Math.round(width),
170
+ height: Math.round(height)
171
+ });
172
+ };
173
+ handler(element.getBoundingClientRect());
174
+ var observer = new ResizeObserver(function (entries) {
175
+ var entry = entries[0];
176
+ if (entry != null && entry.borderBoxSize) {
177
+ var box = entry.borderBoxSize[0];
178
+ if (box) {
179
+ handler({
180
+ width: box.inlineSize,
181
+ height: box.blockSize
182
+ });
183
+ return;
184
+ }
180
185
  }
181
-
182
- prev = rect;
186
+ handler(element.getBoundingClientRect());
187
+ });
188
+ observer.observe(element, {
189
+ box: 'border-box'
190
+ });
191
+ return function () {
192
+ observer.unobserve(element);
183
193
  };
184
194
  };
185
-
186
- const observeElementRect = (instance, cb) => {
187
- const onResize = memoRectCallback(instance, cb);
188
- const observer = observeRect(instance.scrollElement, rect => {
189
- onResize(rect);
190
- });
191
-
192
- if (!instance.scrollElement) {
195
+ var observeWindowRect = function observeWindowRect(instance, cb) {
196
+ var element = instance.scrollElement;
197
+ if (!element) {
193
198
  return;
194
199
  }
195
-
196
- onResize(instance.scrollElement.getBoundingClientRect());
197
- observer.observe();
198
- return () => {
199
- observer.unobserve();
200
+ var handler = function handler() {
201
+ cb({
202
+ width: element.innerWidth,
203
+ height: element.innerHeight
204
+ });
200
205
  };
201
- };
202
- const observeWindowRect = (instance, cb) => {
203
- const memoizedCallback = memoRectCallback(instance, cb);
204
-
205
- const onResize = () => memoizedCallback({
206
- width: instance.scrollElement.innerWidth,
207
- height: instance.scrollElement.innerHeight
206
+ handler();
207
+ element.addEventListener('resize', handler, {
208
+ passive: true
208
209
  });
209
-
210
- if (!instance.scrollElement) {
210
+ return function () {
211
+ element.removeEventListener('resize', handler);
212
+ };
213
+ };
214
+ var observeElementOffset = function observeElementOffset(instance, cb) {
215
+ var element = instance.scrollElement;
216
+ if (!element) {
211
217
  return;
212
218
  }
213
-
214
- onResize();
215
- instance.scrollElement.addEventListener('resize', onResize, {
216
- capture: false,
219
+ var handler = function handler() {
220
+ cb(element[instance.options.horizontal ? 'scrollLeft' : 'scrollTop']);
221
+ };
222
+ handler();
223
+ element.addEventListener('scroll', handler, {
217
224
  passive: true
218
225
  });
219
- return () => {
220
- instance.scrollElement.removeEventListener('resize', onResize);
226
+ return function () {
227
+ element.removeEventListener('scroll', handler);
221
228
  };
222
229
  };
223
- const scrollProps = {
224
- element: ['scrollLeft', 'scrollTop'],
225
- window: ['scrollX', 'scrollY']
226
- };
227
-
228
- const createOffsetObserver = mode => {
229
- return (instance, cb) => {
230
- if (!instance.scrollElement) {
231
- return;
232
- }
233
-
234
- const propX = scrollProps[mode][0];
235
- const propY = scrollProps[mode][1];
236
- let prevX = instance.scrollElement[propX];
237
- let prevY = instance.scrollElement[propY];
238
-
239
- const scroll = () => {
240
- cb(instance.scrollElement[instance.options.horizontal ? propX : propY]);
241
- };
242
-
243
- scroll();
244
-
245
- const onScroll = e => {
246
- const target = e.currentTarget;
247
- const scrollX = target[propX];
248
- const scrollY = target[propY];
249
-
250
- if (instance.options.horizontal ? prevX - scrollX : prevY - scrollY) {
251
- scroll();
252
- }
253
-
254
- prevX = scrollX;
255
- prevY = scrollY;
256
- };
257
-
258
- instance.scrollElement.addEventListener('scroll', onScroll, {
259
- capture: false,
260
- passive: true
261
- });
262
- return () => {
263
- instance.scrollElement.removeEventListener('scroll', onScroll);
264
- };
230
+ var observeWindowOffset = function observeWindowOffset(instance, cb) {
231
+ var element = instance.scrollElement;
232
+ if (!element) {
233
+ return;
234
+ }
235
+ var handler = function handler() {
236
+ cb(element[instance.options.horizontal ? 'scrollX' : 'scrollY']);
237
+ };
238
+ handler();
239
+ element.addEventListener('scroll', handler, {
240
+ passive: true
241
+ });
242
+ return function () {
243
+ element.removeEventListener('scroll', handler);
265
244
  };
266
245
  };
267
-
268
- const observeElementOffset = createOffsetObserver('element');
269
- const observeWindowOffset = createOffsetObserver('window');
270
- const measureElement = (element, instance) => {
271
- return element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height'];
246
+ var measureElement = function measureElement(element, entry, instance) {
247
+ if (entry != null && entry.borderBoxSize) {
248
+ var box = entry.borderBoxSize[0];
249
+ if (box) {
250
+ var size = Math.round(box[instance.options.horizontal ? 'inlineSize' : 'blockSize']);
251
+ return size;
252
+ }
253
+ }
254
+ return Math.round(element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height']);
272
255
  };
273
- const windowScroll = (offset, canSmooth, instance) => {
274
- var _instance$scrollEleme;
275
- (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo({
276
- [instance.options.horizontal ? 'left' : 'top']: offset,
277
- behavior: canSmooth ? 'smooth' : undefined
278
- });
256
+ var windowScroll = function windowScroll(offset, _ref, instance) {
257
+ var _instance$scrollEleme, _instance$scrollEleme2;
258
+ var _ref$adjustments = _ref.adjustments,
259
+ adjustments = _ref$adjustments === void 0 ? 0 : _ref$adjustments,
260
+ behavior = _ref.behavior;
261
+ var toOffset = offset + adjustments;
262
+ (_instance$scrollEleme = instance.scrollElement) == null || _instance$scrollEleme.scrollTo == null || _instance$scrollEleme.scrollTo((_instance$scrollEleme2 = {}, _instance$scrollEleme2[instance.options.horizontal ? 'left' : 'top'] = toOffset, _instance$scrollEleme2.behavior = behavior, _instance$scrollEleme2));
279
263
  };
280
- const elementScroll = (offset, canSmooth, instance) => {
281
- var _instance$scrollEleme2;
282
- (_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo({
283
- [instance.options.horizontal ? 'left' : 'top']: offset,
284
- behavior: canSmooth ? 'smooth' : undefined
285
- });
264
+ var elementScroll = function elementScroll(offset, _ref2, instance) {
265
+ var _instance$scrollEleme3, _instance$scrollEleme4;
266
+ var _ref2$adjustments = _ref2.adjustments,
267
+ adjustments = _ref2$adjustments === void 0 ? 0 : _ref2$adjustments,
268
+ behavior = _ref2.behavior;
269
+ var toOffset = offset + adjustments;
270
+ (_instance$scrollEleme3 = instance.scrollElement) == null || _instance$scrollEleme3.scrollTo == null || _instance$scrollEleme3.scrollTo((_instance$scrollEleme4 = {}, _instance$scrollEleme4[instance.options.horizontal ? 'left' : 'top'] = toOffset, _instance$scrollEleme4.behavior = behavior, _instance$scrollEleme4));
286
271
  };
287
- class Virtualizer {
288
- constructor(_opts) {
289
- var _this = this;
290
-
291
- this.unsubs = [];
292
- this.scrollElement = null;
293
- this.measurementsCache = [];
294
- this.itemMeasurementsCache = {};
295
- this.pendingMeasuredCacheIndexes = [];
296
- this.measureElementCache = {};
297
-
298
- this.setOptions = opts => {
299
- Object.entries(opts).forEach(_ref => {
300
- let [key, value] = _ref;
301
- if (typeof value === 'undefined') delete opts[key];
302
- });
303
- this.options = {
304
- debug: false,
305
- initialOffset: 0,
306
- overscan: 1,
307
- paddingStart: 0,
308
- paddingEnd: 0,
309
- scrollPaddingStart: 0,
310
- scrollPaddingEnd: 0,
311
- horizontal: false,
312
- getItemKey: defaultKeyExtractor,
313
- rangeExtractor: defaultRangeExtractor,
314
- enableSmoothScroll: true,
315
- onChange: () => {},
316
- measureElement,
317
- initialRect: {
318
- width: 0,
319
- height: 0
320
- },
321
- ...opts
322
- };
323
- };
324
-
325
- this.notify = () => {
326
- var _this$options$onChang, _this$options;
327
-
328
- (_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
329
- };
330
-
331
- this.cleanup = () => {
332
- this.unsubs.filter(Boolean).forEach(d => d());
333
- this.unsubs = [];
334
- this.scrollElement = null;
335
- };
336
-
337
- this._didMount = () => {
338
- return () => {
339
- this.cleanup();
340
- };
341
- };
342
-
343
- this._willUpdate = () => {
344
- const scrollElement = this.options.getScrollElement();
345
-
346
- if (this.scrollElement !== scrollElement) {
347
- this.cleanup();
348
- this.scrollElement = scrollElement;
349
- this.unsubs.push(this.options.observeElementRect(this, rect => {
350
- this.scrollRect = rect;
351
- this.notify();
352
- }));
353
- this.unsubs.push(this.options.observeElementOffset(this, offset => {
354
- this.scrollOffset = offset;
355
- this.notify();
356
- }));
272
+ var Virtualizer = function Virtualizer(_opts) {
273
+ var _this = this;
274
+ this.unsubs = [];
275
+ this.scrollElement = null;
276
+ this.isScrolling = false;
277
+ this.isScrollingTimeoutId = null;
278
+ this.scrollToIndexTimeoutId = null;
279
+ this.measurementsCache = [];
280
+ this.itemSizeCache = new Map();
281
+ this.pendingMeasuredCacheIndexes = [];
282
+ this.scrollDirection = null;
283
+ this.scrollAdjustments = 0;
284
+ this.measureElementCache = new Map();
285
+ this.observer = function () {
286
+ var _ro = null;
287
+ var get = function get() {
288
+ if (_ro) {
289
+ return _ro;
290
+ } else if (typeof ResizeObserver !== 'undefined') {
291
+ return _ro = new ResizeObserver(function (entries) {
292
+ entries.forEach(function (entry) {
293
+ _this._measureElement(entry.target, entry);
294
+ });
295
+ });
296
+ } else {
297
+ return null;
357
298
  }
358
299
  };
359
-
360
- this.getSize = () => {
361
- return this.scrollRect[this.options.horizontal ? 'width' : 'height'];
362
- };
363
-
364
- this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.options.getItemKey, this.itemMeasurementsCache], (count, paddingStart, getItemKey, measurementsCache) => {
365
- const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
366
- this.pendingMeasuredCacheIndexes = [];
367
- const measurements = this.measurementsCache.slice(0, min);
368
-
369
- for (let i = min; i < count; i++) {
370
- const key = getItemKey(i);
371
- const measuredSize = measurementsCache[key];
372
- const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
373
- const size = typeof measuredSize === 'number' ? measuredSize : this.options.estimateSize(i);
374
- const end = start + size;
375
- measurements[i] = {
376
- index: i,
377
- start,
378
- size,
379
- end,
380
- key
381
- };
300
+ return {
301
+ disconnect: function disconnect() {
302
+ var _get;
303
+ return (_get = get()) == null ? void 0 : _get.disconnect();
304
+ },
305
+ observe: function observe(target) {
306
+ var _get2;
307
+ return (_get2 = get()) == null ? void 0 : _get2.observe(target, {
308
+ box: 'border-box'
309
+ });
310
+ },
311
+ unobserve: function unobserve(target) {
312
+ var _get3;
313
+ return (_get3 = get()) == null ? void 0 : _get3.unobserve(target);
382
314
  }
383
-
384
- this.measurementsCache = measurements;
385
- return measurements;
386
- }, {
387
- key: 'getMeasurements',
388
- debug: () => this.options.debug
315
+ };
316
+ }();
317
+ this.range = null;
318
+ this.setOptions = function (opts) {
319
+ Object.entries(opts).forEach(function (_ref3) {
320
+ var key = _ref3[0],
321
+ value = _ref3[1];
322
+ if (typeof value === 'undefined') delete opts[key];
389
323
  });
390
- this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
391
- return calculateRange({
392
- measurements,
393
- outerSize,
394
- scrollOffset
395
- });
396
- }, {
397
- key: 'calculateRange',
398
- debug: () => this.options.debug
324
+ _this.options = _extends({
325
+ debug: false,
326
+ initialOffset: 0,
327
+ overscan: 1,
328
+ paddingStart: 0,
329
+ paddingEnd: 0,
330
+ scrollPaddingStart: 0,
331
+ scrollPaddingEnd: 0,
332
+ horizontal: false,
333
+ getItemKey: defaultKeyExtractor,
334
+ rangeExtractor: defaultRangeExtractor,
335
+ onChange: function onChange() {},
336
+ measureElement: measureElement,
337
+ initialRect: {
338
+ width: 0,
339
+ height: 0
340
+ },
341
+ scrollMargin: 0,
342
+ scrollingDelay: 150,
343
+ indexAttribute: 'data-index',
344
+ initialMeasurementsCache: [],
345
+ lanes: 1
346
+ }, opts);
347
+ };
348
+ this.notify = function (sync) {
349
+ _this.options.onChange == null || _this.options.onChange(_this, sync);
350
+ };
351
+ this.maybeNotify = memo(function () {
352
+ _this.calculateRange();
353
+ return [_this.isScrolling, _this.range ? _this.range.startIndex : null, _this.range ? _this.range.endIndex : null];
354
+ }, function (isScrolling) {
355
+ _this.notify(isScrolling);
356
+ }, {
357
+ key: 'maybeNotify',
358
+ debug: function debug() {
359
+ return _this.options.debug;
360
+ },
361
+ initialDeps: [this.isScrolling, this.range ? this.range.startIndex : null, this.range ? this.range.endIndex : null]
362
+ });
363
+ this.cleanup = function () {
364
+ _this.unsubs.filter(Boolean).forEach(function (d) {
365
+ return d();
399
366
  });
400
- this.getIndexes = memo(() => [this.options.rangeExtractor, this.calculateRange(), this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
401
- return rangeExtractor({ ...range,
402
- overscan,
403
- count: count
367
+ _this.unsubs = [];
368
+ _this.scrollElement = null;
369
+ };
370
+ this._didMount = function () {
371
+ _this.measureElementCache.forEach(_this.observer.observe);
372
+ return function () {
373
+ _this.observer.disconnect();
374
+ _this.cleanup();
375
+ };
376
+ };
377
+ this._willUpdate = function () {
378
+ var scrollElement = _this.options.getScrollElement();
379
+ if (_this.scrollElement !== scrollElement) {
380
+ _this.cleanup();
381
+ _this.scrollElement = scrollElement;
382
+ _this._scrollToOffset(_this.scrollOffset, {
383
+ adjustments: undefined,
384
+ behavior: undefined
404
385
  });
405
- }, {
406
- key: 'getIndexes'
407
- });
408
- this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements(), this.options.measureElement], (indexes, measurements, measureElement) => {
409
- const makeMeasureElement = index => measurableItem => {
410
- var _this$itemMeasurement;
411
-
412
- const item = this.measurementsCache[index];
413
-
414
- if (!measurableItem) {
386
+ _this.unsubs.push(_this.options.observeElementRect(_this, function (rect) {
387
+ _this.scrollRect = rect;
388
+ _this.maybeNotify();
389
+ }));
390
+ _this.unsubs.push(_this.options.observeElementOffset(_this, function (offset) {
391
+ _this.scrollAdjustments = 0;
392
+ if (_this.scrollOffset === offset) {
415
393
  return;
416
394
  }
417
-
418
- const measuredItemSize = measureElement(measurableItem, this);
419
- const itemSize = (_this$itemMeasurement = this.itemMeasurementsCache[item.key]) != null ? _this$itemMeasurement : item.size;
420
-
421
- if (measuredItemSize !== itemSize) {
422
- if (item.start < this.scrollOffset) {
423
- if (this.options.debug) {
424
- console.info('correction', measuredItemSize - itemSize);
425
- }
426
-
427
- if (!this.destinationOffset) {
428
- this._scrollToOffset(this.scrollOffset + (measuredItemSize - itemSize), false);
429
- }
430
- }
431
-
432
- this.pendingMeasuredCacheIndexes.push(index);
433
- this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
434
- [item.key]: measuredItemSize
435
- };
436
- this.notify();
395
+ if (_this.isScrollingTimeoutId !== null) {
396
+ clearTimeout(_this.isScrollingTimeoutId);
397
+ _this.isScrollingTimeoutId = null;
437
398
  }
438
- };
439
-
440
- const virtualItems = [];
441
- const currentMeasureElements = {};
442
-
443
- for (let k = 0, len = indexes.length; k < len; k++) {
444
- var _this$measureElementC;
445
-
446
- const i = indexes[k];
447
- const measurement = measurements[i];
448
- const item = { ...measurement,
449
- measureElement: currentMeasureElements[i] = (_this$measureElementC = this.measureElementCache[i]) != null ? _this$measureElementC : makeMeasureElement(i)
450
- };
451
- virtualItems.push(item);
399
+ _this.isScrolling = true;
400
+ _this.scrollDirection = _this.scrollOffset < offset ? 'forward' : 'backward';
401
+ _this.scrollOffset = offset;
402
+ _this.maybeNotify();
403
+ _this.isScrollingTimeoutId = setTimeout(function () {
404
+ _this.isScrollingTimeoutId = null;
405
+ _this.isScrolling = false;
406
+ _this.scrollDirection = null;
407
+ _this.maybeNotify();
408
+ }, _this.options.scrollingDelay);
409
+ }));
410
+ }
411
+ };
412
+ this.getSize = function () {
413
+ return _this.scrollRect[_this.options.horizontal ? 'width' : 'height'];
414
+ };
415
+ this.memoOptions = memo(function () {
416
+ return [_this.options.count, _this.options.paddingStart, _this.options.scrollMargin, _this.options.getItemKey];
417
+ }, function (count, paddingStart, scrollMargin, getItemKey) {
418
+ _this.pendingMeasuredCacheIndexes = [];
419
+ return {
420
+ count: count,
421
+ paddingStart: paddingStart,
422
+ scrollMargin: scrollMargin,
423
+ getItemKey: getItemKey
424
+ };
425
+ }, {
426
+ key: false
427
+ });
428
+ this.getFurthestMeasurement = function (measurements, index) {
429
+ var furthestMeasurementsFound = new Map();
430
+ var furthestMeasurements = new Map();
431
+ for (var m = index - 1; m >= 0; m--) {
432
+ var measurement = measurements[m];
433
+ if (furthestMeasurementsFound.has(measurement.lane)) {
434
+ continue;
452
435
  }
453
-
454
- this.measureElementCache = currentMeasureElements;
455
- return virtualItems;
456
- }, {
457
- key: 'getIndexes'
458
- });
459
-
460
- this.scrollToOffset = function (toOffset, _temp) {
461
- let {
462
- align
463
- } = _temp === void 0 ? {
464
- align: 'start'
465
- } : _temp;
466
-
467
- const attempt = () => {
468
- const offset = _this.scrollOffset;
469
-
470
- const size = _this.getSize();
471
-
472
- if (align === 'auto') {
473
- if (toOffset <= offset) {
474
- align = 'start';
475
- } else if (toOffset >= offset + size) {
476
- align = 'end';
477
- } else {
478
- align = 'start';
479
- }
480
- }
481
-
482
- if (align === 'start') {
483
- _this._scrollToOffset(toOffset, true);
484
- } else if (align === 'end') {
485
- _this._scrollToOffset(toOffset - size, true);
486
- } else if (align === 'center') {
487
- _this._scrollToOffset(toOffset - size / 2, true);
488
- }
436
+ var previousFurthestMeasurement = furthestMeasurements.get(measurement.lane);
437
+ if (previousFurthestMeasurement == null || measurement.end > previousFurthestMeasurement.end) {
438
+ furthestMeasurements.set(measurement.lane, measurement);
439
+ } else if (measurement.end < previousFurthestMeasurement.end) {
440
+ furthestMeasurementsFound.set(measurement.lane, true);
441
+ }
442
+ if (furthestMeasurementsFound.size === _this.options.lanes) {
443
+ break;
444
+ }
445
+ }
446
+ return furthestMeasurements.size === _this.options.lanes ? Array.from(furthestMeasurements.values()).sort(function (a, b) {
447
+ return a.end - b.end;
448
+ })[0] : undefined;
449
+ };
450
+ this.getMeasurements = memo(function () {
451
+ return [_this.memoOptions(), _this.itemSizeCache];
452
+ }, function (_ref4, itemSizeCache) {
453
+ var count = _ref4.count,
454
+ paddingStart = _ref4.paddingStart,
455
+ scrollMargin = _ref4.scrollMargin,
456
+ getItemKey = _ref4.getItemKey;
457
+ var min = _this.pendingMeasuredCacheIndexes.length > 0 ? Math.min.apply(Math, _this.pendingMeasuredCacheIndexes) : 0;
458
+ _this.pendingMeasuredCacheIndexes = [];
459
+ var measurements = _this.measurementsCache.slice(0, min);
460
+ for (var _i2 = min; _i2 < count; _i2++) {
461
+ var key = getItemKey(_i2);
462
+ var furthestMeasurement = _this.options.lanes === 1 ? measurements[_i2 - 1] : _this.getFurthestMeasurement(measurements, _i2);
463
+ var start = furthestMeasurement ? furthestMeasurement.end : paddingStart + scrollMargin;
464
+ var measuredSize = itemSizeCache.get(key);
465
+ var size = typeof measuredSize === 'number' ? measuredSize : _this.options.estimateSize(_i2);
466
+ var end = start + size;
467
+ var lane = furthestMeasurement ? furthestMeasurement.lane : _i2 % _this.options.lanes;
468
+ measurements[_i2] = {
469
+ index: _i2,
470
+ start: start,
471
+ size: size,
472
+ end: end,
473
+ key: key,
474
+ lane: lane
489
475
  };
490
-
491
- attempt();
492
- requestAnimationFrame(() => {
493
- attempt();
476
+ }
477
+ _this.measurementsCache = measurements;
478
+ return measurements;
479
+ }, {
480
+ key: 'getMeasurements',
481
+ debug: function debug() {
482
+ return _this.options.debug;
483
+ }
484
+ });
485
+ this.calculateRange = memo(function () {
486
+ return [_this.getMeasurements(), _this.getSize(), _this.scrollOffset];
487
+ }, function (measurements, outerSize, scrollOffset) {
488
+ return _this.range = measurements.length > 0 && outerSize > 0 ? calculateRange({
489
+ measurements: measurements,
490
+ outerSize: outerSize,
491
+ scrollOffset: scrollOffset
492
+ }) : null;
493
+ }, {
494
+ key: 'calculateRange',
495
+ debug: function debug() {
496
+ return _this.options.debug;
497
+ }
498
+ });
499
+ this.getIndexes = memo(function () {
500
+ return [_this.options.rangeExtractor, _this.calculateRange(), _this.options.overscan, _this.options.count];
501
+ }, function (rangeExtractor, range, overscan, count) {
502
+ return range === null ? [] : rangeExtractor(_extends({}, range, {
503
+ overscan: overscan,
504
+ count: count
505
+ }));
506
+ }, {
507
+ key: 'getIndexes',
508
+ debug: function debug() {
509
+ return _this.options.debug;
510
+ }
511
+ });
512
+ this.indexFromElement = function (node) {
513
+ var attributeName = _this.options.indexAttribute;
514
+ var indexStr = node.getAttribute(attributeName);
515
+ if (!indexStr) {
516
+ console.warn("Missing attribute name '" + attributeName + "={index}' on measured element.");
517
+ return -1;
518
+ }
519
+ return parseInt(indexStr, 10);
520
+ };
521
+ this._measureElement = function (node, entry) {
522
+ var item = _this.measurementsCache[_this.indexFromElement(node)];
523
+ if (!item || !node.isConnected) {
524
+ _this.measureElementCache.forEach(function (cached, key) {
525
+ if (cached === node) {
526
+ _this.observer.unobserve(node);
527
+ _this.measureElementCache["delete"](key);
528
+ }
494
529
  });
495
- };
496
-
497
- this.scrollToIndex = function (index, _temp2) {
498
- let {
499
- align,
500
- ...rest
501
- } = _temp2 === void 0 ? {
502
- align: 'auto'
503
- } : _temp2;
504
-
505
- const measurements = _this.getMeasurements();
506
-
507
- const offset = _this.scrollOffset;
508
-
509
- const size = _this.getSize();
510
-
511
- const {
512
- count
513
- } = _this.options;
514
- const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
515
-
516
- if (!measurement) {
517
- return;
530
+ return;
531
+ }
532
+ var prevNode = _this.measureElementCache.get(item.key);
533
+ if (prevNode !== node) {
534
+ if (prevNode) {
535
+ _this.observer.unobserve(prevNode);
518
536
  }
519
-
520
- if (align === 'auto') {
521
- if (measurement.end >= offset + size - _this.options.scrollPaddingEnd) {
522
- align = 'end';
523
- } else if (measurement.start <= offset + _this.options.scrollPaddingStart) {
524
- align = 'start';
525
- } else {
526
- return;
537
+ _this.observer.observe(node);
538
+ _this.measureElementCache.set(item.key, node);
539
+ }
540
+ var measuredItemSize = _this.options.measureElement(node, entry, _this);
541
+ _this.resizeItem(item, measuredItemSize);
542
+ };
543
+ this.resizeItem = function (item, size) {
544
+ var _this$itemSizeCache$g;
545
+ var itemSize = (_this$itemSizeCache$g = _this.itemSizeCache.get(item.key)) != null ? _this$itemSizeCache$g : item.size;
546
+ var delta = size - itemSize;
547
+ if (delta !== 0) {
548
+ if (item.start < _this.scrollOffset) {
549
+ if (_this.options.debug) {
550
+ console.info('correction', delta);
527
551
  }
552
+ _this._scrollToOffset(_this.scrollOffset, {
553
+ adjustments: _this.scrollAdjustments += delta,
554
+ behavior: undefined
555
+ });
528
556
  }
529
-
530
- const toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
531
-
532
- _this.scrollToOffset(toOffset, {
533
- align,
534
- ...rest
535
- });
536
- };
537
-
538
- this.getTotalSize = () => {
539
- var _this$getMeasurements;
540
-
541
- return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
542
- };
543
-
544
- this._scrollToOffset = (offset, canSmooth) => {
545
- clearTimeout(this.scrollCheckFrame);
546
- this.destinationOffset = offset;
547
- this.options.scrollToFn(offset, this.options.enableSmoothScroll && canSmooth, this);
548
- let scrollCheckFrame;
549
-
550
- const check = () => {
551
- let lastOffset = this.scrollOffset;
552
- this.scrollCheckFrame = scrollCheckFrame = setTimeout(() => {
553
- if (this.scrollCheckFrame !== scrollCheckFrame) {
554
- return;
555
- }
556
-
557
- if (this.scrollOffset === lastOffset) {
558
- this.destinationOffset = undefined;
559
- return;
557
+ _this.pendingMeasuredCacheIndexes.push(item.index);
558
+ _this.itemSizeCache = new Map(_this.itemSizeCache.set(item.key, size));
559
+ _this.notify(false);
560
+ }
561
+ };
562
+ this.measureElement = function (node) {
563
+ if (!node) {
564
+ return;
565
+ }
566
+ _this._measureElement(node, undefined);
567
+ };
568
+ this.getVirtualItems = memo(function () {
569
+ return [_this.getIndexes(), _this.getMeasurements()];
570
+ }, function (indexes, measurements) {
571
+ var virtualItems = [];
572
+ for (var k = 0, len = indexes.length; k < len; k++) {
573
+ var _i3 = indexes[k];
574
+ var measurement = measurements[_i3];
575
+ virtualItems.push(measurement);
576
+ }
577
+ return virtualItems;
578
+ }, {
579
+ key: 'getIndexes',
580
+ debug: function debug() {
581
+ return _this.options.debug;
582
+ }
583
+ });
584
+ this.getVirtualItemForOffset = function (offset) {
585
+ var measurements = _this.getMeasurements();
586
+ return notUndefined(measurements[findNearestBinarySearch(0, measurements.length - 1, function (index) {
587
+ return notUndefined(measurements[index]).start;
588
+ }, offset)]);
589
+ };
590
+ this.getOffsetForAlignment = function (toOffset, align) {
591
+ var size = _this.getSize();
592
+ if (align === 'auto') {
593
+ if (toOffset <= _this.scrollOffset) {
594
+ align = 'start';
595
+ } else if (toOffset >= _this.scrollOffset + size) {
596
+ align = 'end';
597
+ } else {
598
+ align = 'start';
599
+ }
600
+ }
601
+ if (align === 'start') {
602
+ toOffset = toOffset;
603
+ } else if (align === 'end') {
604
+ toOffset = toOffset - size;
605
+ } else if (align === 'center') {
606
+ toOffset = toOffset - size / 2;
607
+ }
608
+ var scrollSizeProp = _this.options.horizontal ? 'scrollWidth' : 'scrollHeight';
609
+ var scrollSize = _this.scrollElement ? 'document' in _this.scrollElement ? _this.scrollElement.document.documentElement[scrollSizeProp] : _this.scrollElement[scrollSizeProp] : 0;
610
+ var maxOffset = scrollSize - _this.getSize();
611
+ return Math.max(Math.min(maxOffset, toOffset), 0);
612
+ };
613
+ this.getOffsetForIndex = function (index, align) {
614
+ if (align === void 0) {
615
+ align = 'auto';
616
+ }
617
+ index = Math.max(0, Math.min(index, _this.options.count - 1));
618
+ var measurement = notUndefined(_this.getMeasurements()[index]);
619
+ if (align === 'auto') {
620
+ if (measurement.end >= _this.scrollOffset + _this.getSize() - _this.options.scrollPaddingEnd) {
621
+ align = 'end';
622
+ } else if (measurement.start <= _this.scrollOffset + _this.options.scrollPaddingStart) {
623
+ align = 'start';
624
+ } else {
625
+ return [_this.scrollOffset, align];
626
+ }
627
+ }
628
+ var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
629
+ return [_this.getOffsetForAlignment(toOffset, align), align];
630
+ };
631
+ this.isDynamicMode = function () {
632
+ return _this.measureElementCache.size > 0;
633
+ };
634
+ this.cancelScrollToIndex = function () {
635
+ if (_this.scrollToIndexTimeoutId !== null) {
636
+ clearTimeout(_this.scrollToIndexTimeoutId);
637
+ _this.scrollToIndexTimeoutId = null;
638
+ }
639
+ };
640
+ this.scrollToOffset = function (toOffset, _temp) {
641
+ var _ref5 = _temp === void 0 ? {} : _temp,
642
+ _ref5$align = _ref5.align,
643
+ align = _ref5$align === void 0 ? 'start' : _ref5$align,
644
+ behavior = _ref5.behavior;
645
+ _this.cancelScrollToIndex();
646
+ if (behavior === 'smooth' && _this.isDynamicMode()) {
647
+ console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
648
+ }
649
+ _this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), {
650
+ adjustments: undefined,
651
+ behavior: behavior
652
+ });
653
+ };
654
+ this.scrollToIndex = function (index, _temp2) {
655
+ var _ref6 = _temp2 === void 0 ? {} : _temp2,
656
+ _ref6$align = _ref6.align,
657
+ initialAlign = _ref6$align === void 0 ? 'auto' : _ref6$align,
658
+ behavior = _ref6.behavior;
659
+ index = Math.max(0, Math.min(index, _this.options.count - 1));
660
+ _this.cancelScrollToIndex();
661
+ if (behavior === 'smooth' && _this.isDynamicMode()) {
662
+ console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
663
+ }
664
+ var _this$getOffsetForInd = _this.getOffsetForIndex(index, initialAlign),
665
+ toOffset = _this$getOffsetForInd[0],
666
+ align = _this$getOffsetForInd[1];
667
+ _this._scrollToOffset(toOffset, {
668
+ adjustments: undefined,
669
+ behavior: behavior
670
+ });
671
+ if (behavior !== 'smooth' && _this.isDynamicMode()) {
672
+ _this.scrollToIndexTimeoutId = setTimeout(function () {
673
+ _this.scrollToIndexTimeoutId = null;
674
+ var elementInDOM = _this.measureElementCache.has(_this.options.getItemKey(index));
675
+ if (elementInDOM) {
676
+ var _this$getOffsetForInd2 = _this.getOffsetForIndex(index, align),
677
+ _toOffset = _this$getOffsetForInd2[0];
678
+ if (!approxEqual(_toOffset, _this.scrollOffset)) {
679
+ _this.scrollToIndex(index, {
680
+ align: align,
681
+ behavior: behavior
682
+ });
560
683
  }
561
-
562
- lastOffset = this.scrollOffset;
563
- check();
564
- }, 100);
565
- };
566
-
567
- check();
568
- };
569
-
570
- this.measure = () => {
571
- this.itemMeasurementsCache = {};
572
- this.notify();
573
- };
574
-
575
- this.setOptions(_opts);
576
- this.scrollRect = this.options.initialRect;
577
- this.scrollOffset = this.options.initialOffset;
578
- }
579
-
580
- }
581
-
582
- const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
684
+ } else {
685
+ _this.scrollToIndex(index, {
686
+ align: align,
687
+ behavior: behavior
688
+ });
689
+ }
690
+ });
691
+ }
692
+ };
693
+ this.scrollBy = function (delta, _temp3) {
694
+ var _ref7 = _temp3 === void 0 ? {} : _temp3,
695
+ behavior = _ref7.behavior;
696
+ _this.cancelScrollToIndex();
697
+ if (behavior === 'smooth' && _this.isDynamicMode()) {
698
+ console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
699
+ }
700
+ _this._scrollToOffset(_this.scrollOffset + delta, {
701
+ adjustments: undefined,
702
+ behavior: behavior
703
+ });
704
+ };
705
+ this.getTotalSize = function () {
706
+ var _this$getMeasurements;
707
+ return (((_this$getMeasurements = _this.getMeasurements()[_this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || _this.options.paddingStart) - _this.options.scrollMargin + _this.options.paddingEnd;
708
+ };
709
+ this._scrollToOffset = function (offset, _ref8) {
710
+ var adjustments = _ref8.adjustments,
711
+ behavior = _ref8.behavior;
712
+ _this.options.scrollToFn(offset, {
713
+ behavior: behavior,
714
+ adjustments: adjustments
715
+ }, _this);
716
+ };
717
+ this.measure = function () {
718
+ _this.itemSizeCache = new Map();
719
+ _this.notify(false);
720
+ };
721
+ this.setOptions(_opts);
722
+ this.scrollRect = this.options.initialRect;
723
+ this.scrollOffset = this.options.initialOffset;
724
+ this.measurementsCache = this.options.initialMeasurementsCache;
725
+ this.measurementsCache.forEach(function (item) {
726
+ _this.itemSizeCache.set(item.key, item.size);
727
+ });
728
+ this.maybeNotify();
729
+ };
730
+ var findNearestBinarySearch = function findNearestBinarySearch(low, high, getCurrentValue, value) {
583
731
  while (low <= high) {
584
- const middle = (low + high) / 2 | 0;
585
- const currentValue = getCurrentValue(middle);
586
-
732
+ var middle = (low + high) / 2 | 0;
733
+ var currentValue = getCurrentValue(middle);
587
734
  if (currentValue < value) {
588
735
  low = middle + 1;
589
736
  } else if (currentValue > value) {
@@ -592,82 +739,88 @@
592
739
  return middle;
593
740
  }
594
741
  }
595
-
596
742
  if (low > 0) {
597
743
  return low - 1;
598
744
  } else {
599
745
  return 0;
600
746
  }
601
747
  };
602
-
603
- function calculateRange(_ref2) {
604
- let {
605
- measurements,
606
- outerSize,
607
- scrollOffset
608
- } = _ref2;
609
- const count = measurements.length - 1;
610
-
611
- const getOffset = index => measurements[index].start;
612
-
613
- const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
614
- let endIndex = startIndex;
615
-
748
+ function calculateRange(_ref9) {
749
+ var measurements = _ref9.measurements,
750
+ outerSize = _ref9.outerSize,
751
+ scrollOffset = _ref9.scrollOffset;
752
+ var count = measurements.length - 1;
753
+ var getOffset = function getOffset(index) {
754
+ return measurements[index].start;
755
+ };
756
+ var startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
757
+ var endIndex = startIndex;
616
758
  while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
617
759
  endIndex++;
618
760
  }
619
-
620
761
  return {
621
- startIndex,
622
- endIndex
762
+ startIndex: startIndex,
763
+ endIndex: endIndex
623
764
  };
624
765
  }
625
766
 
626
- const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
767
+ //
627
768
 
769
+ var useIsomorphicLayoutEffect = typeof document !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
628
770
  function useVirtualizerBase(options) {
629
- const rerender = React__namespace.useReducer(() => ({}), {})[1];
630
- const resolvedOptions = { ...options,
631
- onChange: instance => {
632
- rerender();
633
- options.onChange == null ? void 0 : options.onChange(instance);
771
+ var rerender = React__namespace.useReducer(function () {
772
+ return {};
773
+ }, {})[1];
774
+ var resolvedOptions = _extends$1({}, options, {
775
+ onChange: function onChange(instance, sync) {
776
+ if (sync) {
777
+ reactDom.flushSync(rerender);
778
+ } else {
779
+ rerender();
780
+ }
781
+ options.onChange == null || options.onChange(instance, sync);
634
782
  }
635
- };
636
- const [instance] = React__namespace.useState(() => new Virtualizer(resolvedOptions));
783
+ });
784
+ var _React$useState = React__namespace.useState(function () {
785
+ return new Virtualizer(resolvedOptions);
786
+ }),
787
+ instance = _React$useState[0];
637
788
  instance.setOptions(resolvedOptions);
638
- React__namespace.useEffect(() => {
789
+ React__namespace.useEffect(function () {
639
790
  return instance._didMount();
640
791
  }, []);
641
- useIsomorphicLayoutEffect(() => {
792
+ useIsomorphicLayoutEffect(function () {
642
793
  return instance._willUpdate();
643
794
  });
644
795
  return instance;
645
796
  }
646
-
647
797
  function useVirtualizer(options) {
648
- return useVirtualizerBase({
798
+ return useVirtualizerBase(_extends$1({
649
799
  observeElementRect: observeElementRect,
650
800
  observeElementOffset: observeElementOffset,
651
- scrollToFn: elementScroll,
652
- ...options
653
- });
801
+ scrollToFn: elementScroll
802
+ }, options));
654
803
  }
655
804
  function useWindowVirtualizer(options) {
656
- return useVirtualizerBase({
657
- getScrollElement: () => typeof window !== 'undefined' ? window : null,
805
+ return useVirtualizerBase(_extends$1({
806
+ getScrollElement: function getScrollElement() {
807
+ return typeof document !== 'undefined' ? window : null;
808
+ },
658
809
  observeElementRect: observeWindowRect,
659
810
  observeElementOffset: observeWindowOffset,
660
811
  scrollToFn: windowScroll,
661
- ...options
662
- });
812
+ initialOffset: typeof document !== 'undefined' ? window.scrollY : undefined
813
+ }, options));
663
814
  }
664
815
 
665
816
  exports.Virtualizer = Virtualizer;
817
+ exports.approxEqual = approxEqual;
666
818
  exports.defaultKeyExtractor = defaultKeyExtractor;
667
819
  exports.defaultRangeExtractor = defaultRangeExtractor;
668
820
  exports.elementScroll = elementScroll;
669
821
  exports.measureElement = measureElement;
670
822
  exports.memo = memo;
823
+ exports.notUndefined = notUndefined;
671
824
  exports.observeElementOffset = observeElementOffset;
672
825
  exports.observeElementRect = observeElementRect;
673
826
  exports.observeWindowOffset = observeWindowOffset;