@tanstack/react-virtual 3.0.0-beta.2 → 3.0.0-beta.21
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.
- package/build/cjs/react-virtual/src/index.js.map +1 -1
- package/build/cjs/virtual-core/build/esm/index.js +285 -185
- package/build/cjs/virtual-core/build/esm/index.js.map +1 -1
- package/build/esm/index.js +285 -185
- package/build/esm/index.js.map +1 -1
- package/build/{stats-html.html → stats.html} +1 -1
- package/build/{stats-react.json → stats.json} +17 -17
- package/build/types/index.d.ts +17 -0
- package/build/umd/index.development.js +283 -183
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +2 -2
- package/build/umd/index.production.js.map +1 -1
- package/package.json +7 -4
- package/src/index.tsx +3 -3
- package/build/types/react-virtual/src/index.d.ts +0 -4
- package/build/types/virtual-core/src/utils.d.ts +0 -7
|
@@ -1 +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
|
|
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 extends Element>(\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 extends Element>(\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 extends Element>(\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;;;;;;;;;;;;;;;;"}
|
|
@@ -22,76 +22,6 @@ Object.defineProperty(exports, '__esModule', { value: true });
|
|
|
22
22
|
*
|
|
23
23
|
* @license MIT
|
|
24
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
25
|
function memo(getDeps, fn, opts) {
|
|
96
26
|
let deps = [];
|
|
97
27
|
let result;
|
|
@@ -146,9 +76,29 @@ const defaultRangeExtractor = range => {
|
|
|
146
76
|
|
|
147
77
|
return arr;
|
|
148
78
|
};
|
|
79
|
+
|
|
80
|
+
const memoRectCallback = (instance, cb) => {
|
|
81
|
+
let prev = {
|
|
82
|
+
height: -1,
|
|
83
|
+
width: -1
|
|
84
|
+
};
|
|
85
|
+
return rect => {
|
|
86
|
+
if (instance.options.horizontal ? rect.width !== prev.width : rect.height !== prev.height) {
|
|
87
|
+
cb(rect);
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
prev = rect;
|
|
91
|
+
};
|
|
92
|
+
};
|
|
93
|
+
|
|
149
94
|
const observeElementRect = (instance, cb) => {
|
|
150
|
-
const observer =
|
|
151
|
-
|
|
95
|
+
const observer = new ResizeObserver(entries => {
|
|
96
|
+
var _entries$, _entries$2;
|
|
97
|
+
|
|
98
|
+
cb({
|
|
99
|
+
width: (_entries$ = entries[0]) == null ? void 0 : _entries$.contentRect.width,
|
|
100
|
+
height: (_entries$2 = entries[0]) == null ? void 0 : _entries$2.contentRect.height
|
|
101
|
+
});
|
|
152
102
|
});
|
|
153
103
|
|
|
154
104
|
if (!instance.scrollElement) {
|
|
@@ -156,18 +106,18 @@ const observeElementRect = (instance, cb) => {
|
|
|
156
106
|
}
|
|
157
107
|
|
|
158
108
|
cb(instance.scrollElement.getBoundingClientRect());
|
|
159
|
-
observer.observe();
|
|
109
|
+
observer.observe(instance.scrollElement);
|
|
160
110
|
return () => {
|
|
161
|
-
observer.unobserve();
|
|
111
|
+
observer.unobserve(instance.scrollElement);
|
|
162
112
|
};
|
|
163
113
|
};
|
|
164
114
|
const observeWindowRect = (instance, cb) => {
|
|
165
|
-
const
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
};
|
|
115
|
+
const memoizedCallback = memoRectCallback(instance, cb);
|
|
116
|
+
|
|
117
|
+
const onResize = () => memoizedCallback({
|
|
118
|
+
width: instance.scrollElement.innerWidth,
|
|
119
|
+
height: instance.scrollElement.innerHeight
|
|
120
|
+
});
|
|
171
121
|
|
|
172
122
|
if (!instance.scrollElement) {
|
|
173
123
|
return;
|
|
@@ -182,52 +132,80 @@ const observeWindowRect = (instance, cb) => {
|
|
|
182
132
|
instance.scrollElement.removeEventListener('resize', onResize);
|
|
183
133
|
};
|
|
184
134
|
};
|
|
185
|
-
const
|
|
186
|
-
|
|
135
|
+
const scrollProps = {
|
|
136
|
+
element: ['scrollLeft', 'scrollTop'],
|
|
137
|
+
window: ['scrollX', 'scrollY']
|
|
138
|
+
};
|
|
187
139
|
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
140
|
+
const createOffsetObserver = mode => {
|
|
141
|
+
return (instance, cb) => {
|
|
142
|
+
if (!instance.scrollElement) {
|
|
143
|
+
return;
|
|
144
|
+
}
|
|
191
145
|
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
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']);
|
|
146
|
+
const propX = scrollProps[mode][0];
|
|
147
|
+
const propY = scrollProps[mode][1];
|
|
148
|
+
let prevX = instance.scrollElement[propX];
|
|
149
|
+
let prevY = instance.scrollElement[propY];
|
|
203
150
|
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
151
|
+
const scroll = () => {
|
|
152
|
+
const offset = instance.scrollElement[instance.options.horizontal ? propX : propY];
|
|
153
|
+
cb(Math.max(0, offset - instance.options.scrollMargin));
|
|
154
|
+
};
|
|
207
155
|
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
156
|
+
scroll();
|
|
157
|
+
|
|
158
|
+
const onScroll = e => {
|
|
159
|
+
const target = e.currentTarget;
|
|
160
|
+
const scrollX = target[propX];
|
|
161
|
+
const scrollY = target[propY];
|
|
162
|
+
|
|
163
|
+
if (instance.options.horizontal ? prevX - scrollX : prevY - scrollY) {
|
|
164
|
+
scroll();
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
prevX = scrollX;
|
|
168
|
+
prevY = scrollY;
|
|
169
|
+
};
|
|
170
|
+
|
|
171
|
+
instance.scrollElement.addEventListener('scroll', onScroll, {
|
|
172
|
+
capture: false,
|
|
173
|
+
passive: true
|
|
174
|
+
});
|
|
175
|
+
return () => {
|
|
176
|
+
instance.scrollElement.removeEventListener('scroll', onScroll);
|
|
177
|
+
};
|
|
215
178
|
};
|
|
216
179
|
};
|
|
180
|
+
|
|
181
|
+
const observeElementOffset = createOffsetObserver('element');
|
|
182
|
+
const observeWindowOffset = createOffsetObserver('window');
|
|
217
183
|
const measureElement = (element, instance) => {
|
|
218
|
-
return element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height'];
|
|
184
|
+
return Math.round(element.getBoundingClientRect()[instance.options.horizontal ? 'width' : 'height']);
|
|
219
185
|
};
|
|
220
|
-
const windowScroll = (offset,
|
|
186
|
+
const windowScroll = (offset, _ref, instance) => {
|
|
221
187
|
var _instance$scrollEleme;
|
|
222
|
-
|
|
223
|
-
|
|
188
|
+
|
|
189
|
+
let {
|
|
190
|
+
canSmooth,
|
|
191
|
+
sync
|
|
192
|
+
} = _ref;
|
|
193
|
+
const toOffset = sync ? offset : offset + instance.options.scrollMargin;
|
|
194
|
+
(_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo == null ? void 0 : _instance$scrollEleme.scrollTo({
|
|
195
|
+
[instance.options.horizontal ? 'left' : 'top']: toOffset,
|
|
224
196
|
behavior: canSmooth ? 'smooth' : undefined
|
|
225
197
|
});
|
|
226
198
|
};
|
|
227
|
-
const elementScroll = (offset,
|
|
199
|
+
const elementScroll = (offset, _ref2, instance) => {
|
|
228
200
|
var _instance$scrollEleme2;
|
|
229
|
-
|
|
230
|
-
|
|
201
|
+
|
|
202
|
+
let {
|
|
203
|
+
canSmooth,
|
|
204
|
+
sync
|
|
205
|
+
} = _ref2;
|
|
206
|
+
const toOffset = sync ? offset : offset + instance.options.scrollMargin;
|
|
207
|
+
(_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo == null ? void 0 : _instance$scrollEleme2.scrollTo({
|
|
208
|
+
[instance.options.horizontal ? 'left' : 'top']: toOffset,
|
|
231
209
|
behavior: canSmooth ? 'smooth' : undefined
|
|
232
210
|
});
|
|
233
211
|
};
|
|
@@ -237,13 +215,26 @@ class Virtualizer {
|
|
|
237
215
|
|
|
238
216
|
this.unsubs = [];
|
|
239
217
|
this.scrollElement = null;
|
|
218
|
+
this.isScrolling = false;
|
|
219
|
+
this.isScrollingTimeoutId = null;
|
|
240
220
|
this.measurementsCache = [];
|
|
241
221
|
this.itemMeasurementsCache = {};
|
|
242
222
|
this.pendingMeasuredCacheIndexes = [];
|
|
223
|
+
this.scrollDelta = 0;
|
|
224
|
+
this.measureElementCache = {};
|
|
225
|
+
this.ro = new ResizeObserver(entries => {
|
|
226
|
+
entries.forEach(entry => {
|
|
227
|
+
this._measureElement(entry.target, false);
|
|
228
|
+
});
|
|
229
|
+
});
|
|
230
|
+
this.range = {
|
|
231
|
+
startIndex: 0,
|
|
232
|
+
endIndex: 0
|
|
233
|
+
};
|
|
243
234
|
|
|
244
235
|
this.setOptions = opts => {
|
|
245
|
-
Object.entries(opts).forEach(
|
|
246
|
-
let [key, value] =
|
|
236
|
+
Object.entries(opts).forEach(_ref3 => {
|
|
237
|
+
let [key, value] = _ref3;
|
|
247
238
|
if (typeof value === 'undefined') delete opts[key];
|
|
248
239
|
});
|
|
249
240
|
this.options = {
|
|
@@ -264,6 +255,9 @@ class Virtualizer {
|
|
|
264
255
|
width: 0,
|
|
265
256
|
height: 0
|
|
266
257
|
},
|
|
258
|
+
scrollMargin: 0,
|
|
259
|
+
scrollingDelay: 150,
|
|
260
|
+
indexAttribute: 'data-index',
|
|
267
261
|
...opts
|
|
268
262
|
};
|
|
269
263
|
};
|
|
@@ -277,10 +271,13 @@ class Virtualizer {
|
|
|
277
271
|
this.cleanup = () => {
|
|
278
272
|
this.unsubs.filter(Boolean).forEach(d => d());
|
|
279
273
|
this.unsubs = [];
|
|
274
|
+
this.scrollElement = null;
|
|
280
275
|
};
|
|
281
276
|
|
|
282
277
|
this._didMount = () => {
|
|
283
278
|
return () => {
|
|
279
|
+
this.ro.disconnect();
|
|
280
|
+
this.measureElementCache = {};
|
|
284
281
|
this.cleanup();
|
|
285
282
|
};
|
|
286
283
|
};
|
|
@@ -291,14 +288,41 @@ class Virtualizer {
|
|
|
291
288
|
if (this.scrollElement !== scrollElement) {
|
|
292
289
|
this.cleanup();
|
|
293
290
|
this.scrollElement = scrollElement;
|
|
291
|
+
|
|
292
|
+
this._scrollToOffset(this.scrollOffset, {
|
|
293
|
+
canSmooth: false,
|
|
294
|
+
sync: true,
|
|
295
|
+
requested: false
|
|
296
|
+
});
|
|
297
|
+
|
|
294
298
|
this.unsubs.push(this.options.observeElementRect(this, rect => {
|
|
295
299
|
this.scrollRect = rect;
|
|
296
|
-
this.
|
|
300
|
+
this.calculateRange();
|
|
297
301
|
}));
|
|
298
302
|
this.unsubs.push(this.options.observeElementOffset(this, offset => {
|
|
299
|
-
this.
|
|
300
|
-
|
|
303
|
+
if (this.isScrollingTimeoutId !== null) {
|
|
304
|
+
clearTimeout(this.isScrollingTimeoutId);
|
|
305
|
+
this.isScrollingTimeoutId = null;
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
if (this.scrollOffset !== offset) {
|
|
309
|
+
this.scrollOffset = offset;
|
|
310
|
+
this.isScrolling = true;
|
|
311
|
+
this.scrollDelta = 0;
|
|
312
|
+
this.isScrollingTimeoutId = setTimeout(() => {
|
|
313
|
+
this.isScrollingTimeoutId = null;
|
|
314
|
+
this.isScrolling = false;
|
|
315
|
+
this.notify();
|
|
316
|
+
}, this.options.scrollingDelay);
|
|
317
|
+
} else {
|
|
318
|
+
this.isScrolling = false;
|
|
319
|
+
this.scrollDelta = 0;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
this.calculateRange();
|
|
301
323
|
}));
|
|
324
|
+
} else if (!this.isScrolling) {
|
|
325
|
+
this.calculateRange();
|
|
302
326
|
}
|
|
303
327
|
};
|
|
304
328
|
|
|
@@ -329,108 +353,170 @@ class Virtualizer {
|
|
|
329
353
|
this.measurementsCache = measurements;
|
|
330
354
|
return measurements;
|
|
331
355
|
}, {
|
|
332
|
-
key: process.env.NODE_ENV
|
|
356
|
+
key: process.env.NODE_ENV !== 'production' && 'getMeasurements',
|
|
333
357
|
debug: () => this.options.debug
|
|
334
358
|
});
|
|
335
359
|
this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
|
|
336
|
-
|
|
360
|
+
const range = calculateRange({
|
|
337
361
|
measurements,
|
|
338
362
|
outerSize,
|
|
339
363
|
scrollOffset
|
|
340
364
|
});
|
|
365
|
+
|
|
366
|
+
if (range.startIndex !== this.range.startIndex || range.endIndex !== this.range.endIndex) {
|
|
367
|
+
this.range = range;
|
|
368
|
+
this.notify();
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return this.range;
|
|
341
372
|
}, {
|
|
342
|
-
key: process.env.NODE_ENV
|
|
373
|
+
key: process.env.NODE_ENV !== 'production' && 'calculateRange',
|
|
343
374
|
debug: () => this.options.debug
|
|
344
375
|
});
|
|
345
|
-
this.getIndexes = memo(() => [this.options.rangeExtractor, this.
|
|
376
|
+
this.getIndexes = memo(() => [this.options.rangeExtractor, this.range, this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
|
|
346
377
|
return rangeExtractor({ ...range,
|
|
347
378
|
overscan,
|
|
348
379
|
count: count
|
|
349
380
|
});
|
|
350
381
|
}, {
|
|
351
|
-
key: process.env.NODE_ENV
|
|
382
|
+
key: process.env.NODE_ENV !== 'production' && 'getIndexes',
|
|
383
|
+
debug: () => this.options.debug
|
|
352
384
|
});
|
|
353
|
-
|
|
385
|
+
|
|
386
|
+
this.indexFromElement = node => {
|
|
387
|
+
const attributeName = this.options.indexAttribute;
|
|
388
|
+
const indexStr = node.getAttribute(attributeName);
|
|
389
|
+
|
|
390
|
+
if (!indexStr) {
|
|
391
|
+
console.warn("Missing attribute name '" + attributeName + "={index}' on measured element.");
|
|
392
|
+
return -1;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
return parseInt(indexStr, 10);
|
|
396
|
+
};
|
|
397
|
+
|
|
398
|
+
this._measureElement = (node, _sync) => {
|
|
399
|
+
var _this$itemMeasurement;
|
|
400
|
+
|
|
401
|
+
const index = this.indexFromElement(node);
|
|
402
|
+
const item = this.measurementsCache[index];
|
|
403
|
+
|
|
404
|
+
if (!item) {
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
const key = String(item.key);
|
|
409
|
+
const prevNode = this.measureElementCache[key];
|
|
410
|
+
|
|
411
|
+
if (!node.isConnected) {
|
|
412
|
+
if (prevNode) {
|
|
413
|
+
this.ro.unobserve(prevNode);
|
|
414
|
+
delete this.measureElementCache[key];
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
return;
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (!prevNode || prevNode !== node) {
|
|
421
|
+
if (prevNode) {
|
|
422
|
+
this.ro.unobserve(prevNode);
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
this.measureElementCache[key] = node;
|
|
426
|
+
this.ro.observe(node);
|
|
427
|
+
}
|
|
428
|
+
|
|
429
|
+
const measuredItemSize = this.options.measureElement(node, this);
|
|
430
|
+
const itemSize = (_this$itemMeasurement = this.itemMeasurementsCache[item.key]) != null ? _this$itemMeasurement : item.size;
|
|
431
|
+
|
|
432
|
+
if (measuredItemSize !== itemSize) {
|
|
433
|
+
if (item.start < this.scrollOffset) {
|
|
434
|
+
if (process.env.NODE_ENV !== 'production' && this.options.debug) {
|
|
435
|
+
console.info('correction', measuredItemSize - itemSize);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
if (this.destinationOffset === undefined) {
|
|
439
|
+
this.scrollDelta += measuredItemSize - itemSize;
|
|
440
|
+
|
|
441
|
+
this._scrollToOffset(this.scrollOffset + this.scrollDelta, {
|
|
442
|
+
canSmooth: false,
|
|
443
|
+
sync: false,
|
|
444
|
+
requested: false
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
this.pendingMeasuredCacheIndexes.push(index);
|
|
450
|
+
this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
|
|
451
|
+
[item.key]: measuredItemSize
|
|
452
|
+
};
|
|
453
|
+
this.notify();
|
|
454
|
+
}
|
|
455
|
+
};
|
|
456
|
+
|
|
457
|
+
this.measureElement = node => {
|
|
458
|
+
if (!node) {
|
|
459
|
+
return;
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
this._measureElement(node, true);
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements()], (indexes, measurements) => {
|
|
354
466
|
const virtualItems = [];
|
|
355
467
|
|
|
356
468
|
for (let k = 0, len = indexes.length; k < len; k++) {
|
|
357
469
|
const i = indexes[k];
|
|
358
470
|
const measurement = measurements[i];
|
|
359
|
-
|
|
360
|
-
measureElement: measurableItem => {
|
|
361
|
-
if (measurableItem) {
|
|
362
|
-
const measuredItemSize = measureElement(measurableItem, this);
|
|
363
|
-
|
|
364
|
-
if (measuredItemSize !== item.size) {
|
|
365
|
-
if (item.start < this.scrollOffset) {
|
|
366
|
-
if (process.env.NODE_ENV === 'development' && this.options.debug) console.info('correction', measuredItemSize - item.size);
|
|
367
|
-
|
|
368
|
-
if (!this.destinationOffset) {
|
|
369
|
-
this._scrollToOffset(this.scrollOffset + (measuredItemSize - item.size), false);
|
|
370
|
-
}
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
this.pendingMeasuredCacheIndexes.push(i);
|
|
374
|
-
this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
|
|
375
|
-
[item.key]: measuredItemSize
|
|
376
|
-
};
|
|
377
|
-
this.notify();
|
|
378
|
-
}
|
|
379
|
-
}
|
|
380
|
-
}
|
|
381
|
-
};
|
|
382
|
-
virtualItems.push(item);
|
|
471
|
+
virtualItems.push(measurement);
|
|
383
472
|
}
|
|
384
473
|
|
|
385
474
|
return virtualItems;
|
|
386
475
|
}, {
|
|
387
|
-
key: process.env.NODE_ENV
|
|
476
|
+
key: process.env.NODE_ENV !== 'production' && 'getIndexes',
|
|
477
|
+
debug: () => this.options.debug
|
|
388
478
|
});
|
|
389
479
|
|
|
390
480
|
this.scrollToOffset = function (toOffset, _temp) {
|
|
391
481
|
let {
|
|
392
|
-
align
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
const attempt = () => {
|
|
398
|
-
const offset = _this.scrollOffset;
|
|
482
|
+
align = 'start',
|
|
483
|
+
smoothScroll = _this.options.enableSmoothScroll
|
|
484
|
+
} = _temp === void 0 ? {} : _temp;
|
|
485
|
+
const offset = _this.scrollOffset;
|
|
399
486
|
|
|
400
|
-
|
|
487
|
+
const size = _this.getSize();
|
|
401
488
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
}
|
|
489
|
+
if (align === 'auto') {
|
|
490
|
+
if (toOffset <= offset) {
|
|
491
|
+
align = 'start';
|
|
492
|
+
} else if (toOffset >= offset + size) {
|
|
493
|
+
align = 'end';
|
|
494
|
+
} else {
|
|
495
|
+
align = 'start';
|
|
410
496
|
}
|
|
497
|
+
}
|
|
411
498
|
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
} else if (align === 'center') {
|
|
417
|
-
_this._scrollToOffset(toOffset - size / 2, true);
|
|
418
|
-
}
|
|
499
|
+
const options = {
|
|
500
|
+
canSmooth: smoothScroll,
|
|
501
|
+
sync: false,
|
|
502
|
+
requested: true
|
|
419
503
|
};
|
|
420
504
|
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
505
|
+
if (align === 'start') {
|
|
506
|
+
_this._scrollToOffset(toOffset, options);
|
|
507
|
+
} else if (align === 'end') {
|
|
508
|
+
_this._scrollToOffset(toOffset - size, options);
|
|
509
|
+
} else if (align === 'center') {
|
|
510
|
+
_this._scrollToOffset(toOffset - size / 2, options);
|
|
511
|
+
}
|
|
425
512
|
};
|
|
426
513
|
|
|
427
514
|
this.scrollToIndex = function (index, _temp2) {
|
|
428
515
|
let {
|
|
429
|
-
align,
|
|
516
|
+
align = 'auto',
|
|
517
|
+
smoothScroll = _this.options.enableSmoothScroll,
|
|
430
518
|
...rest
|
|
431
|
-
} = _temp2 === void 0 ? {
|
|
432
|
-
align: 'auto'
|
|
433
|
-
} : _temp2;
|
|
519
|
+
} = _temp2 === void 0 ? {} : _temp2;
|
|
434
520
|
|
|
435
521
|
const measurements = _this.getMeasurements();
|
|
436
522
|
|
|
@@ -461,6 +547,7 @@ class Virtualizer {
|
|
|
461
547
|
|
|
462
548
|
_this.scrollToOffset(toOffset, {
|
|
463
549
|
align,
|
|
550
|
+
smoothScroll,
|
|
464
551
|
...rest
|
|
465
552
|
});
|
|
466
553
|
};
|
|
@@ -471,10 +558,22 @@ class Virtualizer {
|
|
|
471
558
|
return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
|
|
472
559
|
};
|
|
473
560
|
|
|
474
|
-
this._scrollToOffset = (offset,
|
|
561
|
+
this._scrollToOffset = (offset, _ref4) => {
|
|
562
|
+
let {
|
|
563
|
+
requested,
|
|
564
|
+
canSmooth,
|
|
565
|
+
sync
|
|
566
|
+
} = _ref4;
|
|
475
567
|
clearTimeout(this.scrollCheckFrame);
|
|
476
|
-
|
|
477
|
-
|
|
568
|
+
|
|
569
|
+
if (requested) {
|
|
570
|
+
this.destinationOffset = offset;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
this.options.scrollToFn(offset, {
|
|
574
|
+
canSmooth,
|
|
575
|
+
sync
|
|
576
|
+
}, this);
|
|
478
577
|
let scrollCheckFrame;
|
|
479
578
|
|
|
480
579
|
const check = () => {
|
|
@@ -505,6 +604,7 @@ class Virtualizer {
|
|
|
505
604
|
this.setOptions(_opts);
|
|
506
605
|
this.scrollRect = this.options.initialRect;
|
|
507
606
|
this.scrollOffset = this.options.initialOffset;
|
|
607
|
+
this.calculateRange();
|
|
508
608
|
}
|
|
509
609
|
|
|
510
610
|
}
|
|
@@ -530,12 +630,12 @@ const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
|
|
|
530
630
|
}
|
|
531
631
|
};
|
|
532
632
|
|
|
533
|
-
function calculateRange(
|
|
633
|
+
function calculateRange(_ref5) {
|
|
534
634
|
let {
|
|
535
635
|
measurements,
|
|
536
636
|
outerSize,
|
|
537
637
|
scrollOffset
|
|
538
|
-
} =
|
|
638
|
+
} = _ref5;
|
|
539
639
|
const count = measurements.length - 1;
|
|
540
640
|
|
|
541
641
|
const getOffset = index => measurements[index].start;
|