@tanstack/react-virtual 3.0.4 → 3.1.0

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.
@@ -1,849 +0,0 @@
1
- /**
2
- * react-virtual
3
- *
4
- * Copyright (c) TanStack
5
- *
6
- * This source code is licensed under the MIT license found in the
7
- * LICENSE.md file in the root directory of this source tree.
8
- *
9
- * @license MIT
10
- */
11
- (function (global, factory) {
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
-
17
- function _interopNamespace(e) {
18
- if (e && e.__esModule) return e;
19
- var n = Object.create(null);
20
- if (e) {
21
- Object.keys(e).forEach(function (k) {
22
- if (k !== 'default') {
23
- var d = Object.getOwnPropertyDescriptor(e, k);
24
- Object.defineProperty(n, k, d.get ? d : {
25
- enumerable: true,
26
- get: function () { return e[k]; }
27
- });
28
- }
29
- });
30
- }
31
- n["default"] = e;
32
- return Object.freeze(n);
33
- }
34
-
35
- var React__namespace = /*#__PURE__*/_interopNamespace(React);
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
-
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
- */
87
- function memo(getDeps, fn, opts) {
88
- var _opts$initialDeps;
89
- var deps = (_opts$initialDeps = opts.initialDeps) != null ? _opts$initialDeps : [];
90
- var result;
91
- return function () {
92
- var depTime;
93
- if (opts.key && opts.debug != null && opts.debug()) depTime = Date.now();
94
- var newDeps = getDeps();
95
- var depsChanged = newDeps.length !== deps.length || newDeps.some(function (dep, index) {
96
- return deps[index] !== dep;
97
- });
98
- if (!depsChanged) {
99
- return result;
100
- }
101
- deps = newDeps;
102
- var resultTime;
103
- if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now();
104
- result = fn.apply(void 0, newDeps);
105
- if (opts.key && opts.debug != null && opts.debug()) {
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) {
110
- str = String(str);
111
- while (str.length < num) {
112
- str = ' ' + str;
113
- }
114
- return str;
115
- };
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);
117
- }
118
- opts == null || opts.onChange == null || opts.onChange(result);
119
- return result;
120
- };
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
- */
143
-
144
- //
145
-
146
- //
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
- }
158
- return arr;
159
- };
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
- }
185
- }
186
- handler(element.getBoundingClientRect());
187
- });
188
- observer.observe(element, {
189
- box: 'border-box'
190
- });
191
- return function () {
192
- observer.unobserve(element);
193
- };
194
- };
195
- var observeWindowRect = function observeWindowRect(instance, cb) {
196
- var element = instance.scrollElement;
197
- if (!element) {
198
- return;
199
- }
200
- var handler = function handler() {
201
- cb({
202
- width: element.innerWidth,
203
- height: element.innerHeight
204
- });
205
- };
206
- handler();
207
- element.addEventListener('resize', handler, {
208
- passive: true
209
- });
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) {
217
- return;
218
- }
219
- var handler = function handler() {
220
- cb(element[instance.options.horizontal ? 'scrollLeft' : 'scrollTop']);
221
- };
222
- handler();
223
- element.addEventListener('scroll', handler, {
224
- passive: true
225
- });
226
- return function () {
227
- element.removeEventListener('scroll', handler);
228
- };
229
- };
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);
244
- };
245
- };
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']);
255
- };
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));
263
- };
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));
271
- };
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;
298
- }
299
- };
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);
314
- }
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];
323
- });
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();
366
- });
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
385
- });
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) {
393
- return;
394
- }
395
- if (_this.isScrollingTimeoutId !== null) {
396
- clearTimeout(_this.isScrollingTimeoutId);
397
- _this.isScrollingTimeoutId = null;
398
- }
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;
435
- }
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
- if (a.end === b.end) {
448
- return a.index - b.index;
449
- }
450
- return a.end - b.end;
451
- })[0] : undefined;
452
- };
453
- this.getMeasurements = memo(function () {
454
- return [_this.memoOptions(), _this.itemSizeCache];
455
- }, function (_ref4, itemSizeCache) {
456
- var count = _ref4.count,
457
- paddingStart = _ref4.paddingStart,
458
- scrollMargin = _ref4.scrollMargin,
459
- getItemKey = _ref4.getItemKey;
460
- var min = _this.pendingMeasuredCacheIndexes.length > 0 ? Math.min.apply(Math, _this.pendingMeasuredCacheIndexes) : 0;
461
- _this.pendingMeasuredCacheIndexes = [];
462
- var measurements = _this.measurementsCache.slice(0, min);
463
- for (var _i2 = min; _i2 < count; _i2++) {
464
- var key = getItemKey(_i2);
465
- var furthestMeasurement = _this.options.lanes === 1 ? measurements[_i2 - 1] : _this.getFurthestMeasurement(measurements, _i2);
466
- var start = furthestMeasurement ? furthestMeasurement.end : paddingStart + scrollMargin;
467
- var measuredSize = itemSizeCache.get(key);
468
- var size = typeof measuredSize === 'number' ? measuredSize : _this.options.estimateSize(_i2);
469
- var end = start + size;
470
- var lane = furthestMeasurement ? furthestMeasurement.lane : _i2 % _this.options.lanes;
471
- measurements[_i2] = {
472
- index: _i2,
473
- start: start,
474
- size: size,
475
- end: end,
476
- key: key,
477
- lane: lane
478
- };
479
- }
480
- _this.measurementsCache = measurements;
481
- return measurements;
482
- }, {
483
- key: 'getMeasurements',
484
- debug: function debug() {
485
- return _this.options.debug;
486
- }
487
- });
488
- this.calculateRange = memo(function () {
489
- return [_this.getMeasurements(), _this.getSize(), _this.scrollOffset];
490
- }, function (measurements, outerSize, scrollOffset) {
491
- return _this.range = measurements.length > 0 && outerSize > 0 ? calculateRange({
492
- measurements: measurements,
493
- outerSize: outerSize,
494
- scrollOffset: scrollOffset
495
- }) : null;
496
- }, {
497
- key: 'calculateRange',
498
- debug: function debug() {
499
- return _this.options.debug;
500
- }
501
- });
502
- this.getIndexes = memo(function () {
503
- return [_this.options.rangeExtractor, _this.calculateRange(), _this.options.overscan, _this.options.count];
504
- }, function (rangeExtractor, range, overscan, count) {
505
- return range === null ? [] : rangeExtractor(_extends({}, range, {
506
- overscan: overscan,
507
- count: count
508
- }));
509
- }, {
510
- key: 'getIndexes',
511
- debug: function debug() {
512
- return _this.options.debug;
513
- }
514
- });
515
- this.indexFromElement = function (node) {
516
- var attributeName = _this.options.indexAttribute;
517
- var indexStr = node.getAttribute(attributeName);
518
- if (!indexStr) {
519
- console.warn("Missing attribute name '" + attributeName + "={index}' on measured element.");
520
- return -1;
521
- }
522
- return parseInt(indexStr, 10);
523
- };
524
- this._measureElement = function (node, entry) {
525
- var item = _this.measurementsCache[_this.indexFromElement(node)];
526
- if (!item || !node.isConnected) {
527
- _this.measureElementCache.forEach(function (cached, key) {
528
- if (cached === node) {
529
- _this.observer.unobserve(node);
530
- _this.measureElementCache["delete"](key);
531
- }
532
- });
533
- return;
534
- }
535
- var prevNode = _this.measureElementCache.get(item.key);
536
- if (prevNode !== node) {
537
- if (prevNode) {
538
- _this.observer.unobserve(prevNode);
539
- }
540
- _this.observer.observe(node);
541
- _this.measureElementCache.set(item.key, node);
542
- }
543
- var measuredItemSize = _this.options.measureElement(node, entry, _this);
544
- _this.resizeItem(item, measuredItemSize);
545
- };
546
- this.resizeItem = function (item, size) {
547
- var _this$itemSizeCache$g;
548
- var itemSize = (_this$itemSizeCache$g = _this.itemSizeCache.get(item.key)) != null ? _this$itemSizeCache$g : item.size;
549
- var delta = size - itemSize;
550
- if (delta !== 0) {
551
- if (item.start < _this.scrollOffset + _this.scrollAdjustments) {
552
- if (_this.options.debug) {
553
- console.info('correction', delta);
554
- }
555
- _this._scrollToOffset(_this.scrollOffset, {
556
- adjustments: _this.scrollAdjustments += delta,
557
- behavior: undefined
558
- });
559
- }
560
- _this.pendingMeasuredCacheIndexes.push(item.index);
561
- _this.itemSizeCache = new Map(_this.itemSizeCache.set(item.key, size));
562
- _this.notify(false);
563
- }
564
- };
565
- this.measureElement = function (node) {
566
- if (!node) {
567
- return;
568
- }
569
- _this._measureElement(node, undefined);
570
- };
571
- this.getVirtualItems = memo(function () {
572
- return [_this.getIndexes(), _this.getMeasurements()];
573
- }, function (indexes, measurements) {
574
- var virtualItems = [];
575
- for (var k = 0, len = indexes.length; k < len; k++) {
576
- var _i3 = indexes[k];
577
- var measurement = measurements[_i3];
578
- virtualItems.push(measurement);
579
- }
580
- return virtualItems;
581
- }, {
582
- key: 'getIndexes',
583
- debug: function debug() {
584
- return _this.options.debug;
585
- }
586
- });
587
- this.getVirtualItemForOffset = function (offset) {
588
- var measurements = _this.getMeasurements();
589
- return notUndefined(measurements[findNearestBinarySearch(0, measurements.length - 1, function (index) {
590
- return notUndefined(measurements[index]).start;
591
- }, offset)]);
592
- };
593
- this.getOffsetForAlignment = function (toOffset, align) {
594
- var size = _this.getSize();
595
- if (align === 'auto') {
596
- if (toOffset <= _this.scrollOffset) {
597
- align = 'start';
598
- } else if (toOffset >= _this.scrollOffset + size) {
599
- align = 'end';
600
- } else {
601
- align = 'start';
602
- }
603
- }
604
- if (align === 'start') {
605
- toOffset = toOffset;
606
- } else if (align === 'end') {
607
- toOffset = toOffset - size;
608
- } else if (align === 'center') {
609
- toOffset = toOffset - size / 2;
610
- }
611
- var scrollSizeProp = _this.options.horizontal ? 'scrollWidth' : 'scrollHeight';
612
- var scrollSize = _this.scrollElement ? 'document' in _this.scrollElement ? _this.scrollElement.document.documentElement[scrollSizeProp] : _this.scrollElement[scrollSizeProp] : 0;
613
- var maxOffset = scrollSize - _this.getSize();
614
- return Math.max(Math.min(maxOffset, toOffset), 0);
615
- };
616
- this.getOffsetForIndex = function (index, align) {
617
- if (align === void 0) {
618
- align = 'auto';
619
- }
620
- index = Math.max(0, Math.min(index, _this.options.count - 1));
621
- var measurement = notUndefined(_this.getMeasurements()[index]);
622
- if (align === 'auto') {
623
- if (measurement.end >= _this.scrollOffset + _this.getSize() - _this.options.scrollPaddingEnd) {
624
- align = 'end';
625
- } else if (measurement.start <= _this.scrollOffset + _this.options.scrollPaddingStart) {
626
- align = 'start';
627
- } else {
628
- return [_this.scrollOffset, align];
629
- }
630
- }
631
- var toOffset = align === 'end' ? measurement.end + _this.options.scrollPaddingEnd : measurement.start - _this.options.scrollPaddingStart;
632
- return [_this.getOffsetForAlignment(toOffset, align), align];
633
- };
634
- this.isDynamicMode = function () {
635
- return _this.measureElementCache.size > 0;
636
- };
637
- this.cancelScrollToIndex = function () {
638
- if (_this.scrollToIndexTimeoutId !== null) {
639
- clearTimeout(_this.scrollToIndexTimeoutId);
640
- _this.scrollToIndexTimeoutId = null;
641
- }
642
- };
643
- this.scrollToOffset = function (toOffset, _temp) {
644
- var _ref5 = _temp === void 0 ? {} : _temp,
645
- _ref5$align = _ref5.align,
646
- align = _ref5$align === void 0 ? 'start' : _ref5$align,
647
- behavior = _ref5.behavior;
648
- _this.cancelScrollToIndex();
649
- if (behavior === 'smooth' && _this.isDynamicMode()) {
650
- console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
651
- }
652
- _this._scrollToOffset(_this.getOffsetForAlignment(toOffset, align), {
653
- adjustments: undefined,
654
- behavior: behavior
655
- });
656
- };
657
- this.scrollToIndex = function (index, _temp2) {
658
- var _ref6 = _temp2 === void 0 ? {} : _temp2,
659
- _ref6$align = _ref6.align,
660
- initialAlign = _ref6$align === void 0 ? 'auto' : _ref6$align,
661
- behavior = _ref6.behavior;
662
- index = Math.max(0, Math.min(index, _this.options.count - 1));
663
- _this.cancelScrollToIndex();
664
- if (behavior === 'smooth' && _this.isDynamicMode()) {
665
- console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
666
- }
667
- var _this$getOffsetForInd = _this.getOffsetForIndex(index, initialAlign),
668
- toOffset = _this$getOffsetForInd[0],
669
- align = _this$getOffsetForInd[1];
670
- _this._scrollToOffset(toOffset, {
671
- adjustments: undefined,
672
- behavior: behavior
673
- });
674
- if (behavior !== 'smooth' && _this.isDynamicMode()) {
675
- _this.scrollToIndexTimeoutId = setTimeout(function () {
676
- _this.scrollToIndexTimeoutId = null;
677
- var elementInDOM = _this.measureElementCache.has(_this.options.getItemKey(index));
678
- if (elementInDOM) {
679
- var _this$getOffsetForInd2 = _this.getOffsetForIndex(index, align),
680
- _toOffset = _this$getOffsetForInd2[0];
681
- if (!approxEqual(_toOffset, _this.scrollOffset)) {
682
- _this.scrollToIndex(index, {
683
- align: align,
684
- behavior: behavior
685
- });
686
- }
687
- } else {
688
- _this.scrollToIndex(index, {
689
- align: align,
690
- behavior: behavior
691
- });
692
- }
693
- });
694
- }
695
- };
696
- this.scrollBy = function (delta, _temp3) {
697
- var _ref7 = _temp3 === void 0 ? {} : _temp3,
698
- behavior = _ref7.behavior;
699
- _this.cancelScrollToIndex();
700
- if (behavior === 'smooth' && _this.isDynamicMode()) {
701
- console.warn('The `smooth` scroll behavior is not fully supported with dynamic size.');
702
- }
703
- _this._scrollToOffset(_this.scrollOffset + delta, {
704
- adjustments: undefined,
705
- behavior: behavior
706
- });
707
- };
708
- this.getTotalSize = function () {
709
- var measurements = _this.getMeasurements();
710
- var end;
711
- // If there are no measurements, set the end to paddingStart
712
- if (measurements.length === 0) {
713
- end = _this.options.paddingStart;
714
- } else {
715
- var _measurements$end, _measurements;
716
- // If lanes is 1, use the last measurement's end, otherwise find the maximum end value among all measurements
717
- end = _this.options.lanes === 1 ? (_measurements$end = (_measurements = measurements[measurements.length - 1]) == null ? void 0 : _measurements.end) != null ? _measurements$end : 0 : Math.max.apply(Math, measurements.slice(-_this.options.lanes).map(function (m) {
718
- return m.end;
719
- }));
720
- }
721
- return end - _this.options.scrollMargin + _this.options.paddingEnd;
722
- };
723
- this._scrollToOffset = function (offset, _ref8) {
724
- var adjustments = _ref8.adjustments,
725
- behavior = _ref8.behavior;
726
- _this.options.scrollToFn(offset, {
727
- behavior: behavior,
728
- adjustments: adjustments
729
- }, _this);
730
- };
731
- this.measure = function () {
732
- _this.itemSizeCache = new Map();
733
- _this.notify(false);
734
- };
735
- this.setOptions(_opts);
736
- this.scrollRect = this.options.initialRect;
737
- this.scrollOffset = this.options.initialOffset;
738
- this.measurementsCache = this.options.initialMeasurementsCache;
739
- this.measurementsCache.forEach(function (item) {
740
- _this.itemSizeCache.set(item.key, item.size);
741
- });
742
- this.maybeNotify();
743
- };
744
- var findNearestBinarySearch = function findNearestBinarySearch(low, high, getCurrentValue, value) {
745
- while (low <= high) {
746
- var middle = (low + high) / 2 | 0;
747
- var currentValue = getCurrentValue(middle);
748
- if (currentValue < value) {
749
- low = middle + 1;
750
- } else if (currentValue > value) {
751
- high = middle - 1;
752
- } else {
753
- return middle;
754
- }
755
- }
756
- if (low > 0) {
757
- return low - 1;
758
- } else {
759
- return 0;
760
- }
761
- };
762
- function calculateRange(_ref9) {
763
- var measurements = _ref9.measurements,
764
- outerSize = _ref9.outerSize,
765
- scrollOffset = _ref9.scrollOffset;
766
- var count = measurements.length - 1;
767
- var getOffset = function getOffset(index) {
768
- return measurements[index].start;
769
- };
770
- var startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
771
- var endIndex = startIndex;
772
- while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
773
- endIndex++;
774
- }
775
- return {
776
- startIndex: startIndex,
777
- endIndex: endIndex
778
- };
779
- }
780
-
781
- //
782
-
783
- var useIsomorphicLayoutEffect = typeof document !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
784
- function useVirtualizerBase(options) {
785
- var rerender = React__namespace.useReducer(function () {
786
- return {};
787
- }, {})[1];
788
- var resolvedOptions = _extends$1({}, options, {
789
- onChange: function onChange(instance, sync) {
790
- if (sync) {
791
- reactDom.flushSync(rerender);
792
- } else {
793
- rerender();
794
- }
795
- options.onChange == null || options.onChange(instance, sync);
796
- }
797
- });
798
- var _React$useState = React__namespace.useState(function () {
799
- return new Virtualizer(resolvedOptions);
800
- }),
801
- instance = _React$useState[0];
802
- instance.setOptions(resolvedOptions);
803
- React__namespace.useEffect(function () {
804
- return instance._didMount();
805
- }, []);
806
- useIsomorphicLayoutEffect(function () {
807
- return instance._willUpdate();
808
- });
809
- return instance;
810
- }
811
- function useVirtualizer(options) {
812
- return useVirtualizerBase(_extends$1({
813
- observeElementRect: observeElementRect,
814
- observeElementOffset: observeElementOffset,
815
- scrollToFn: elementScroll
816
- }, options));
817
- }
818
- function useWindowVirtualizer(options) {
819
- return useVirtualizerBase(_extends$1({
820
- getScrollElement: function getScrollElement() {
821
- return typeof document !== 'undefined' ? window : null;
822
- },
823
- observeElementRect: observeWindowRect,
824
- observeElementOffset: observeWindowOffset,
825
- scrollToFn: windowScroll,
826
- initialOffset: typeof document !== 'undefined' ? window.scrollY : undefined
827
- }, options));
828
- }
829
-
830
- exports.Virtualizer = Virtualizer;
831
- exports.approxEqual = approxEqual;
832
- exports.defaultKeyExtractor = defaultKeyExtractor;
833
- exports.defaultRangeExtractor = defaultRangeExtractor;
834
- exports.elementScroll = elementScroll;
835
- exports.measureElement = measureElement;
836
- exports.memo = memo;
837
- exports.notUndefined = notUndefined;
838
- exports.observeElementOffset = observeElementOffset;
839
- exports.observeElementRect = observeElementRect;
840
- exports.observeWindowOffset = observeWindowOffset;
841
- exports.observeWindowRect = observeWindowRect;
842
- exports.useVirtualizer = useVirtualizer;
843
- exports.useWindowVirtualizer = useWindowVirtualizer;
844
- exports.windowScroll = windowScroll;
845
-
846
- Object.defineProperty(exports, '__esModule', { value: true });
847
-
848
- }));
849
- //# sourceMappingURL=index.development.js.map