@tanstack/react-virtual 3.0.0-alpha.0 → 3.0.0-beta.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.
Files changed (29) hide show
  1. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +1 -18
  2. package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +1 -1
  3. package/build/cjs/react-virtual/src/index.js +90 -0
  4. package/build/cjs/react-virtual/src/index.js.map +1 -0
  5. package/build/cjs/virtual-core/build/esm/index.js +565 -0
  6. package/build/cjs/virtual-core/build/esm/index.js.map +1 -0
  7. package/build/esm/index.js +432 -350
  8. package/build/esm/index.js.map +1 -1
  9. package/build/stats-html.html +1 -1
  10. package/build/stats-react.json +24 -119
  11. package/build/types/react-virtual/src/index.d.ts +4 -0
  12. package/build/types/virtual-core/src/utils.d.ts +7 -0
  13. package/build/umd/index.development.js +443 -352
  14. package/build/umd/index.development.js.map +1 -1
  15. package/build/umd/index.production.js +11 -1
  16. package/build/umd/index.production.js.map +1 -1
  17. package/package.json +16 -17
  18. package/src/index.tsx +66 -386
  19. package/build/cjs/packages/react-virtual/src/index.js +0 -371
  20. package/build/cjs/packages/react-virtual/src/index.js.map +0 -1
  21. package/build/cjs/packages/react-virtual/src/useIsomorphicLayoutEffect.js +0 -40
  22. package/build/cjs/packages/react-virtual/src/useIsomorphicLayoutEffect.js.map +0 -1
  23. package/build/cjs/packages/react-virtual/src/useRect.js +0 -85
  24. package/build/cjs/packages/react-virtual/src/useRect.js.map +0 -1
  25. package/build/types/index.d.ts +0 -53
  26. package/build/types/useIsomorphicLayoutEffect.d.ts +0 -2
  27. package/build/types/useRect.d.ts +0 -6
  28. package/src/useIsomorphicLayoutEffect.ts +0 -4
  29. package/src/useRect.ts +0 -58
@@ -13,7 +13,7 @@
13
13
  Object.defineProperty(exports, '__esModule', { value: true });
14
14
 
15
15
  function _extends() {
16
- _extends = Object.assign || function (target) {
16
+ _extends = Object.assign ? Object.assign.bind() : function (target) {
17
17
  for (var i = 1; i < arguments.length; i++) {
18
18
  var source = arguments[i];
19
19
 
@@ -26,25 +26,8 @@ function _extends() {
26
26
 
27
27
  return target;
28
28
  };
29
-
30
29
  return _extends.apply(this, arguments);
31
30
  }
32
31
 
33
- function _objectWithoutPropertiesLoose(source, excluded) {
34
- if (source == null) return {};
35
- var target = {};
36
- var sourceKeys = Object.keys(source);
37
- var key, i;
38
-
39
- for (i = 0; i < sourceKeys.length; i++) {
40
- key = sourceKeys[i];
41
- if (excluded.indexOf(key) >= 0) continue;
42
- target[key] = source[key];
43
- }
44
-
45
- return target;
46
- }
47
-
48
32
  exports["extends"] = _extends;
49
- exports.objectWithoutPropertiesLoose = _objectWithoutPropertiesLoose;
50
33
  //# sourceMappingURL=_rollupPluginBabelHelpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"_rollupPluginBabelHelpers.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
1
+ {"version":3,"file":"_rollupPluginBabelHelpers.js","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,90 @@
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
+ 'use strict';
12
+
13
+ Object.defineProperty(exports, '__esModule', { value: true });
14
+
15
+ var React = require('react');
16
+ var index = require('../../virtual-core/build/esm/index.js');
17
+
18
+ function _interopNamespace(e) {
19
+ if (e && e.__esModule) return e;
20
+ var n = Object.create(null);
21
+ if (e) {
22
+ Object.keys(e).forEach(function (k) {
23
+ if (k !== 'default') {
24
+ var d = Object.getOwnPropertyDescriptor(e, k);
25
+ Object.defineProperty(n, k, d.get ? d : {
26
+ enumerable: true,
27
+ get: function () { return e[k]; }
28
+ });
29
+ }
30
+ });
31
+ }
32
+ n["default"] = e;
33
+ return Object.freeze(n);
34
+ }
35
+
36
+ var React__namespace = /*#__PURE__*/_interopNamespace(React);
37
+
38
+ const useIsomorphicLayoutEffect = typeof window !== 'undefined' ? React__namespace.useLayoutEffect : React__namespace.useEffect;
39
+
40
+ function useVirtualizerBase(options) {
41
+ const rerender = React__namespace.useReducer(() => ({}), {})[1];
42
+ const resolvedOptions = { ...options,
43
+ onChange: instance => {
44
+ rerender();
45
+ options.onChange == null ? void 0 : options.onChange(instance);
46
+ }
47
+ };
48
+ const [instance] = React__namespace.useState(() => new index.Virtualizer(resolvedOptions));
49
+ instance.setOptions(resolvedOptions);
50
+ React__namespace.useEffect(() => {
51
+ return instance._didMount();
52
+ }, []);
53
+ useIsomorphicLayoutEffect(() => {
54
+ return instance._willUpdate();
55
+ });
56
+ return instance;
57
+ }
58
+
59
+ function useVirtualizer(options) {
60
+ return useVirtualizerBase({
61
+ observeElementRect: index.observeElementRect,
62
+ observeElementOffset: index.observeElementOffset,
63
+ scrollToFn: index.elementScroll,
64
+ ...options
65
+ });
66
+ }
67
+ function useWindowVirtualizer(options) {
68
+ return useVirtualizerBase({
69
+ getScrollElement: () => typeof window !== 'undefined' ? window : null,
70
+ observeElementRect: index.observeWindowRect,
71
+ observeElementOffset: index.observeWindowOffset,
72
+ scrollToFn: index.windowScroll,
73
+ ...options
74
+ });
75
+ }
76
+
77
+ exports.Virtualizer = index.Virtualizer;
78
+ exports.defaultKeyExtractor = index.defaultKeyExtractor;
79
+ exports.defaultRangeExtractor = index.defaultRangeExtractor;
80
+ exports.elementScroll = index.elementScroll;
81
+ exports.measureElement = index.measureElement;
82
+ exports.memo = index.memo;
83
+ exports.observeElementOffset = index.observeElementOffset;
84
+ exports.observeElementRect = index.observeElementRect;
85
+ exports.observeWindowOffset = index.observeWindowOffset;
86
+ exports.observeWindowRect = index.observeWindowRect;
87
+ exports.windowScroll = index.windowScroll;
88
+ exports.useVirtualizer = useVirtualizer;
89
+ exports.useWindowVirtualizer = useWindowVirtualizer;
90
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sources":["../../../../src/index.tsx"],"sourcesContent":["import * as React from 'react'\nimport {\n elementScroll,\n observeElementOffset,\n observeElementRect,\n observeWindowOffset,\n observeWindowRect,\n PartialKeys,\n Virtualizer,\n VirtualizerOptions,\n windowScroll,\n} from '@tanstack/virtual-core'\nexport * from '@tanstack/virtual-core'\n\n//\n\nconst useIsomorphicLayoutEffect =\n typeof window !== 'undefined' ? React.useLayoutEffect : React.useEffect\n\nfunction useVirtualizerBase<TScrollElement, TItemElement = unknown>(\n options: VirtualizerOptions<TScrollElement, TItemElement>,\n): Virtualizer<TScrollElement, TItemElement> {\n const rerender = React.useReducer(() => ({}), {})[1]\n\n const resolvedOptions: VirtualizerOptions<TScrollElement, TItemElement> = {\n ...options,\n onChange: (instance) => {\n rerender()\n options.onChange?.(instance)\n },\n }\n\n const [instance] = React.useState(\n () => new Virtualizer<TScrollElement, TItemElement>(resolvedOptions),\n )\n\n instance.setOptions(resolvedOptions)\n\n React.useEffect(() => {\n return instance._didMount()\n }, [])\n\n useIsomorphicLayoutEffect(() => {\n return instance._willUpdate()\n })\n\n return instance\n}\n\nexport function useVirtualizer<TScrollElement, TItemElement = unknown>(\n options: PartialKeys<\n VirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): Virtualizer<TScrollElement, TItemElement> {\n return useVirtualizerBase<TScrollElement, TItemElement>({\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n}\n\nexport function useWindowVirtualizer<TItemElement = unknown>(\n options: PartialKeys<\n VirtualizerOptions<Window, TItemElement>,\n | 'getScrollElement'\n | 'observeElementRect'\n | 'observeElementOffset'\n | 'scrollToFn'\n >,\n): Virtualizer<Window, TItemElement> {\n return useVirtualizerBase<Window, TItemElement>({\n getScrollElement: () => (typeof window !== 'undefined' ? window : null!),\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n ...options,\n })\n}\n"],"names":["useIsomorphicLayoutEffect","window","React","useLayoutEffect","useEffect","useVirtualizerBase","options","rerender","useReducer","resolvedOptions","onChange","instance","useState","Virtualizer","setOptions","_didMount","_willUpdate","useVirtualizer","observeElementRect","observeElementOffset","scrollToFn","elementScroll","useWindowVirtualizer","getScrollElement","observeWindowRect","observeWindowOffset","windowScroll"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBA,MAAMA,yBAAyB,GAC7B,OAAOC,MAAP,KAAkB,WAAlB,GAAgCC,gBAAK,CAACC,eAAtC,GAAwDD,gBAAK,CAACE,SADhE,CAAA;;AAGA,SAASC,kBAAT,CACEC,OADF,EAE6C;AAC3C,EAAA,MAAMC,QAAQ,GAAGL,gBAAK,CAACM,UAAN,CAAiB,OAAO,EAAP,CAAjB,EAA6B,EAA7B,CAAA,CAAiC,CAAjC,CAAjB,CAAA;AAEA,EAAA,MAAMC,eAAiE,GAAG,EACxE,GAAGH,OADqE;IAExEI,QAAQ,EAAGC,QAAD,IAAc;MACtBJ,QAAQ,EAAA,CAAA;AACRD,MAAAA,OAAO,CAACI,QAAR,IAAA,IAAA,GAAA,KAAA,CAAA,GAAAJ,OAAO,CAACI,QAAR,CAAmBC,QAAnB,CAAA,CAAA;AACD,KAAA;GALH,CAAA;AAQA,EAAA,MAAM,CAACA,QAAD,CAAaT,GAAAA,gBAAK,CAACU,QAAN,CACjB,MAAM,IAAIC,iBAAJ,CAA8CJ,eAA9C,CADW,CAAnB,CAAA;EAIAE,QAAQ,CAACG,UAAT,CAAoBL,eAApB,CAAA,CAAA;EAEAP,gBAAK,CAACE,SAAN,CAAgB,MAAM;IACpB,OAAOO,QAAQ,CAACI,SAAT,EAAP,CAAA;AACD,GAFD,EAEG,EAFH,CAAA,CAAA;AAIAf,EAAAA,yBAAyB,CAAC,MAAM;IAC9B,OAAOW,QAAQ,CAACK,WAAT,EAAP,CAAA;AACD,GAFwB,CAAzB,CAAA;AAIA,EAAA,OAAOL,QAAP,CAAA;AACD,CAAA;;AAEM,SAASM,cAAT,CACLX,OADK,EAKsC;AAC3C,EAAA,OAAOD,kBAAkB,CAA+B;AACtDa,IAAAA,kBAAkB,EAAEA,wBADkC;AAEtDC,IAAAA,oBAAoB,EAAEA,0BAFgC;AAGtDC,IAAAA,UAAU,EAAEC,mBAH0C;IAItD,GAAGf,OAAAA;AAJmD,GAA/B,CAAzB,CAAA;AAMD,CAAA;AAEM,SAASgB,oBAAT,CACLhB,OADK,EAQ8B;AACnC,EAAA,OAAOD,kBAAkB,CAAuB;IAC9CkB,gBAAgB,EAAE,MAAO,OAAOtB,MAAP,KAAkB,WAAlB,GAAgCA,MAAhC,GAAyC,IADpB;AAE9CiB,IAAAA,kBAAkB,EAAEM,uBAF0B;AAG9CL,IAAAA,oBAAoB,EAAEM,yBAHwB;AAI9CL,IAAAA,UAAU,EAAEM,kBAJkC;IAK9C,GAAGpB,OAAAA;AAL2C,GAAvB,CAAzB,CAAA;AAOD;;;;;;;;;;;;;;;;"}
@@ -0,0 +1,565 @@
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
+ 'use strict';
12
+
13
+ Object.defineProperty(exports, '__esModule', { value: true });
14
+
15
+ /**
16
+ * virtual-core
17
+ *
18
+ * Copyright (c) TanStack
19
+ *
20
+ * This source code is licensed under the MIT license found in the
21
+ * LICENSE.md file in the root directory of this source tree.
22
+ *
23
+ * @license MIT
24
+ */
25
+ var props = ["bottom", "height", "left", "right", "top", "width"];
26
+
27
+ var rectChanged = function rectChanged(a, b) {
28
+ if (a === void 0) {
29
+ a = {};
30
+ }
31
+
32
+ if (b === void 0) {
33
+ b = {};
34
+ }
35
+
36
+ return props.some(function (prop) {
37
+ return a[prop] !== b[prop];
38
+ });
39
+ };
40
+
41
+ var observedNodes = /*#__PURE__*/new Map();
42
+ var rafId;
43
+
44
+ var run = function run() {
45
+ var changedStates = [];
46
+ observedNodes.forEach(function (state, node) {
47
+ var newRect = node.getBoundingClientRect();
48
+
49
+ if (rectChanged(newRect, state.rect)) {
50
+ state.rect = newRect;
51
+ changedStates.push(state);
52
+ }
53
+ });
54
+ changedStates.forEach(function (state) {
55
+ state.callbacks.forEach(function (cb) {
56
+ return cb(state.rect);
57
+ });
58
+ });
59
+ rafId = window.requestAnimationFrame(run);
60
+ };
61
+
62
+ function observeRect(node, cb) {
63
+ return {
64
+ observe: function observe() {
65
+ var wasEmpty = observedNodes.size === 0;
66
+
67
+ if (observedNodes.has(node)) {
68
+ observedNodes.get(node).callbacks.push(cb);
69
+ } else {
70
+ observedNodes.set(node, {
71
+ rect: undefined,
72
+ hasRectChanged: false,
73
+ callbacks: [cb]
74
+ });
75
+ }
76
+
77
+ if (wasEmpty) run();
78
+ },
79
+ unobserve: function unobserve() {
80
+ var state = observedNodes.get(node);
81
+
82
+ if (state) {
83
+ // Remove the callback
84
+ var index = state.callbacks.indexOf(cb);
85
+ if (index >= 0) state.callbacks.splice(index, 1); // Remove the node reference
86
+
87
+ if (!state.callbacks.length) observedNodes["delete"](node); // Stop the loop
88
+
89
+ if (!observedNodes.size) cancelAnimationFrame(rafId);
90
+ }
91
+ }
92
+ };
93
+ }
94
+
95
+ function memo(getDeps, fn, opts) {
96
+ let deps = [];
97
+ let result;
98
+ return () => {
99
+ let depTime;
100
+ if (opts.key && opts.debug != null && opts.debug()) depTime = Date.now();
101
+ const newDeps = getDeps();
102
+ const depsChanged = newDeps.length !== deps.length || newDeps.some((dep, index) => deps[index] !== dep);
103
+
104
+ if (!depsChanged) {
105
+ return result;
106
+ }
107
+
108
+ deps = newDeps;
109
+ let resultTime;
110
+ if (opts.key && opts.debug != null && opts.debug()) resultTime = Date.now();
111
+ result = fn(...newDeps);
112
+ opts == null ? void 0 : opts.onChange == null ? void 0 : opts.onChange(result);
113
+
114
+ if (opts.key && opts.debug != null && opts.debug()) {
115
+ const depEndTime = Math.round((Date.now() - depTime) * 100) / 100;
116
+ const resultEndTime = Math.round((Date.now() - resultTime) * 100) / 100;
117
+ const resultFpsPercentage = resultEndTime / 16;
118
+
119
+ const pad = (str, num) => {
120
+ str = String(str);
121
+
122
+ while (str.length < num) {
123
+ str = ' ' + str;
124
+ }
125
+
126
+ return str;
127
+ };
128
+
129
+ 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);
130
+ }
131
+
132
+ return result;
133
+ };
134
+ }
135
+
136
+ //
137
+ const defaultKeyExtractor = index => index;
138
+ const defaultRangeExtractor = range => {
139
+ const start = Math.max(range.startIndex - range.overscan, 0);
140
+ const end = Math.min(range.endIndex + range.overscan, range.count - 1);
141
+ const arr = [];
142
+
143
+ for (let i = start; i <= end; i++) {
144
+ arr.push(i);
145
+ }
146
+
147
+ return arr;
148
+ };
149
+ const observeElementRect = (instance, cb) => {
150
+ const observer = observeRect(instance.scrollElement, rect => {
151
+ cb(rect);
152
+ });
153
+
154
+ if (!instance.scrollElement) {
155
+ return;
156
+ }
157
+
158
+ cb(instance.scrollElement.getBoundingClientRect());
159
+ observer.observe();
160
+ return () => {
161
+ observer.unobserve();
162
+ };
163
+ };
164
+ const observeWindowRect = (instance, cb) => {
165
+ const onResize = () => {
166
+ cb({
167
+ width: instance.scrollElement.innerWidth,
168
+ height: instance.scrollElement.innerHeight
169
+ });
170
+ };
171
+
172
+ if (!instance.scrollElement) {
173
+ return;
174
+ }
175
+
176
+ onResize();
177
+ instance.scrollElement.addEventListener('resize', onResize, {
178
+ capture: false,
179
+ passive: true
180
+ });
181
+ return () => {
182
+ instance.scrollElement.removeEventListener('resize', onResize);
183
+ };
184
+ };
185
+ const observeElementOffset = (instance, cb) => {
186
+ const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollLeft' : 'scrollTop']);
187
+
188
+ if (!instance.scrollElement) {
189
+ return;
190
+ }
191
+
192
+ onScroll();
193
+ instance.scrollElement.addEventListener('scroll', onScroll, {
194
+ capture: false,
195
+ passive: true
196
+ });
197
+ return () => {
198
+ instance.scrollElement.removeEventListener('scroll', onScroll);
199
+ };
200
+ };
201
+ const observeWindowOffset = (instance, cb) => {
202
+ const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollX' : 'scrollY']);
203
+
204
+ if (!instance.scrollElement) {
205
+ return;
206
+ }
207
+
208
+ onScroll();
209
+ instance.scrollElement.addEventListener('scroll', onScroll, {
210
+ capture: false,
211
+ passive: true
212
+ });
213
+ return () => {
214
+ instance.scrollElement.removeEventListener('scroll', onScroll);
215
+ };
216
+ };
217
+ const measureElement = (element, instance) => {
218
+ return element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height'];
219
+ };
220
+ const windowScroll = (offset, canSmooth, instance) => {
221
+ var _instance$scrollEleme;
222
+ (_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo({
223
+ [instance.options.horizontal ? 'left' : 'top']: offset,
224
+ behavior: canSmooth ? 'smooth' : undefined
225
+ });
226
+ };
227
+ const elementScroll = (offset, canSmooth, instance) => {
228
+ var _instance$scrollEleme2;
229
+ (_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo({
230
+ [instance.options.horizontal ? 'left' : 'top']: offset,
231
+ behavior: canSmooth ? 'smooth' : undefined
232
+ });
233
+ };
234
+ class Virtualizer {
235
+ constructor(_opts) {
236
+ var _this = this;
237
+
238
+ this.unsubs = [];
239
+ this.scrollElement = null;
240
+ this.measurementsCache = [];
241
+ this.itemMeasurementsCache = {};
242
+ this.pendingMeasuredCacheIndexes = [];
243
+
244
+ this.setOptions = opts => {
245
+ Object.entries(opts).forEach(_ref => {
246
+ let [key, value] = _ref;
247
+ if (typeof value === 'undefined') delete opts[key];
248
+ });
249
+ this.options = {
250
+ debug: false,
251
+ initialOffset: 0,
252
+ overscan: 1,
253
+ paddingStart: 0,
254
+ paddingEnd: 0,
255
+ horizontal: false,
256
+ getItemKey: defaultKeyExtractor,
257
+ rangeExtractor: defaultRangeExtractor,
258
+ enableSmoothScroll: false,
259
+ onChange: () => {},
260
+ measureElement,
261
+ initialRect: {
262
+ width: 0,
263
+ height: 0
264
+ },
265
+ ...opts
266
+ };
267
+ };
268
+
269
+ this.notify = () => {
270
+ var _this$options$onChang, _this$options;
271
+
272
+ (_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
273
+ };
274
+
275
+ this.cleanup = () => {
276
+ this.unsubs.filter(Boolean).forEach(d => d());
277
+ this.unsubs = [];
278
+ };
279
+
280
+ this._didMount = () => {
281
+ return () => {
282
+ this.cleanup();
283
+ };
284
+ };
285
+
286
+ this._willUpdate = () => {
287
+ const scrollElement = this.options.getScrollElement();
288
+
289
+ if (this.scrollElement !== scrollElement) {
290
+ this.cleanup();
291
+ this.scrollElement = scrollElement;
292
+ this.unsubs.push(this.options.observeElementRect(this, rect => {
293
+ this.scrollRect = rect;
294
+ this.notify();
295
+ }));
296
+ this.unsubs.push(this.options.observeElementOffset(this, offset => {
297
+ this.scrollOffset = offset;
298
+ this.notify();
299
+ }));
300
+ }
301
+ };
302
+
303
+ this.getSize = () => {
304
+ return this.scrollRect[this.options.horizontal ? 'width' : 'height'];
305
+ };
306
+
307
+ this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.options.getItemKey, this.itemMeasurementsCache], (count, paddingStart, getItemKey, measurementsCache) => {
308
+ const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
309
+ this.pendingMeasuredCacheIndexes = [];
310
+ const measurements = this.measurementsCache.slice(0, min);
311
+
312
+ for (let i = min; i < count; i++) {
313
+ const key = getItemKey(i);
314
+ const measuredSize = measurementsCache[key];
315
+ const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
316
+ const size = typeof measuredSize === 'number' ? measuredSize : this.options.estimateSize(i);
317
+ const end = start + size;
318
+ measurements[i] = {
319
+ index: i,
320
+ start,
321
+ size,
322
+ end,
323
+ key
324
+ };
325
+ }
326
+
327
+ this.measurementsCache = measurements;
328
+ return measurements;
329
+ }, {
330
+ key: process.env.NODE_ENV === 'development' && 'getMeasurements',
331
+ debug: () => this.options.debug
332
+ });
333
+ this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
334
+ return calculateRange({
335
+ measurements,
336
+ outerSize,
337
+ scrollOffset
338
+ });
339
+ }, {
340
+ key: process.env.NODE_ENV === 'development' && 'calculateRange',
341
+ debug: () => this.options.debug
342
+ });
343
+ this.getIndexes = memo(() => [this.options.rangeExtractor, this.calculateRange(), this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
344
+ return rangeExtractor({ ...range,
345
+ overscan,
346
+ count: count
347
+ });
348
+ }, {
349
+ key: process.env.NODE_ENV === 'development' && 'getIndexes'
350
+ });
351
+ this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements(), this.options.measureElement], (indexes, measurements, measureElement) => {
352
+ const virtualItems = [];
353
+
354
+ for (let k = 0, len = indexes.length; k < len; k++) {
355
+ const i = indexes[k];
356
+ const measurement = measurements[i];
357
+ const item = { ...measurement,
358
+ measureElement: measurableItem => {
359
+ if (measurableItem) {
360
+ const measuredItemSize = measureElement(measurableItem, this);
361
+
362
+ if (measuredItemSize !== item.size) {
363
+ if (item.start < this.scrollOffset) {
364
+ if (process.env.NODE_ENV === 'development' && this.options.debug) console.info('correction', measuredItemSize - item.size);
365
+
366
+ if (!this.destinationOffset) {
367
+ this._scrollToOffset(this.scrollOffset + (measuredItemSize - item.size), false);
368
+ }
369
+ }
370
+
371
+ this.pendingMeasuredCacheIndexes.push(i);
372
+ this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
373
+ [item.key]: measuredItemSize
374
+ };
375
+ this.notify();
376
+ }
377
+ }
378
+ }
379
+ };
380
+ virtualItems.push(item);
381
+ }
382
+
383
+ return virtualItems;
384
+ }, {
385
+ key: process.env.NODE_ENV === 'development' && 'getIndexes'
386
+ });
387
+
388
+ this.scrollToOffset = function (toOffset, _temp) {
389
+ let {
390
+ align
391
+ } = _temp === void 0 ? {
392
+ align: 'start'
393
+ } : _temp;
394
+
395
+ const attempt = () => {
396
+ const offset = _this.scrollOffset;
397
+
398
+ const size = _this.getSize();
399
+
400
+ if (align === 'auto') {
401
+ if (toOffset <= offset) {
402
+ align = 'start';
403
+ } else if (toOffset >= offset + size) {
404
+ align = 'end';
405
+ } else {
406
+ align = 'start';
407
+ }
408
+ }
409
+
410
+ if (align === 'start') {
411
+ _this._scrollToOffset(toOffset, true);
412
+ } else if (align === 'end') {
413
+ _this._scrollToOffset(toOffset - size, true);
414
+ } else if (align === 'center') {
415
+ _this._scrollToOffset(toOffset - size / 2, true);
416
+ }
417
+ };
418
+
419
+ attempt();
420
+ requestAnimationFrame(() => {
421
+ attempt();
422
+ });
423
+ };
424
+
425
+ this.scrollToIndex = function (index, _temp2) {
426
+ let {
427
+ align,
428
+ ...rest
429
+ } = _temp2 === void 0 ? {
430
+ align: 'auto'
431
+ } : _temp2;
432
+
433
+ const measurements = _this.getMeasurements();
434
+
435
+ const offset = _this.scrollOffset;
436
+
437
+ const size = _this.getSize();
438
+
439
+ const {
440
+ count
441
+ } = _this.options;
442
+ const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
443
+
444
+ if (!measurement) {
445
+ return;
446
+ }
447
+
448
+ if (align === 'auto') {
449
+ if (measurement.end >= offset + size) {
450
+ align = 'end';
451
+ } else if (measurement.start <= offset) {
452
+ align = 'start';
453
+ } else {
454
+ return;
455
+ }
456
+ }
457
+
458
+ const toOffset = align === 'center' ? measurement.start + measurement.size / 2 : align === 'end' ? measurement.end : measurement.start;
459
+
460
+ _this.scrollToOffset(toOffset, {
461
+ align,
462
+ ...rest
463
+ });
464
+ };
465
+
466
+ this.getTotalSize = () => {
467
+ var _this$getMeasurements;
468
+
469
+ return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
470
+ };
471
+
472
+ this._scrollToOffset = (offset, canSmooth) => {
473
+ clearTimeout(this.scrollCheckFrame);
474
+ this.destinationOffset = offset;
475
+ this.options.scrollToFn(offset, canSmooth, this);
476
+ let scrollCheckFrame;
477
+
478
+ const check = () => {
479
+ let lastOffset = this.scrollOffset;
480
+ this.scrollCheckFrame = scrollCheckFrame = setTimeout(() => {
481
+ if (this.scrollCheckFrame !== scrollCheckFrame) {
482
+ return;
483
+ }
484
+
485
+ if (this.scrollOffset === lastOffset) {
486
+ this.destinationOffset = undefined;
487
+ return;
488
+ }
489
+
490
+ lastOffset = this.scrollOffset;
491
+ check();
492
+ }, 100);
493
+ };
494
+
495
+ check();
496
+ };
497
+
498
+ this.measure = () => {
499
+ this.itemMeasurementsCache = {};
500
+ this.notify();
501
+ };
502
+
503
+ this.setOptions(_opts);
504
+ this.scrollRect = this.options.initialRect;
505
+ this.scrollOffset = this.options.initialOffset;
506
+ }
507
+
508
+ }
509
+
510
+ const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
511
+ while (low <= high) {
512
+ const middle = (low + high) / 2 | 0;
513
+ const currentValue = getCurrentValue(middle);
514
+
515
+ if (currentValue < value) {
516
+ low = middle + 1;
517
+ } else if (currentValue > value) {
518
+ high = middle - 1;
519
+ } else {
520
+ return middle;
521
+ }
522
+ }
523
+
524
+ if (low > 0) {
525
+ return low - 1;
526
+ } else {
527
+ return 0;
528
+ }
529
+ };
530
+
531
+ function calculateRange(_ref2) {
532
+ let {
533
+ measurements,
534
+ outerSize,
535
+ scrollOffset
536
+ } = _ref2;
537
+ const count = measurements.length - 1;
538
+
539
+ const getOffset = index => measurements[index].start;
540
+
541
+ const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
542
+ let endIndex = startIndex;
543
+
544
+ while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
545
+ endIndex++;
546
+ }
547
+
548
+ return {
549
+ startIndex,
550
+ endIndex
551
+ };
552
+ }
553
+
554
+ exports.Virtualizer = Virtualizer;
555
+ exports.defaultKeyExtractor = defaultKeyExtractor;
556
+ exports.defaultRangeExtractor = defaultRangeExtractor;
557
+ exports.elementScroll = elementScroll;
558
+ exports.measureElement = measureElement;
559
+ exports.memo = memo;
560
+ exports.observeElementOffset = observeElementOffset;
561
+ exports.observeElementRect = observeElementRect;
562
+ exports.observeWindowOffset = observeWindowOffset;
563
+ exports.observeWindowRect = observeWindowRect;
564
+ exports.windowScroll = windowScroll;
565
+ //# sourceMappingURL=index.js.map