@tanstack/react-virtual 3.0.0-alpha.0 → 3.0.0-alpha.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.
- package/build/cjs/react-virtual/src/index.js +90 -0
- package/build/cjs/react-virtual/src/index.js.map +1 -0
- package/build/cjs/virtual-core/build/esm/index.js +563 -0
- package/build/cjs/virtual-core/build/esm/index.js.map +1 -0
- package/build/esm/index.js +428 -350
- package/build/esm/index.js.map +1 -1
- package/build/stats-html.html +1 -1
- package/build/stats-react.json +24 -119
- package/build/types/react-virtual/src/index.d.ts +4 -0
- package/build/types/virtual-core/src/utils.d.ts +7 -0
- package/build/umd/index.development.js +439 -351
- package/build/umd/index.development.js.map +1 -1
- package/build/umd/index.production.js +11 -1
- package/build/umd/index.production.js.map +1 -1
- package/package.json +16 -17
- package/src/index.tsx +62 -386
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js +0 -50
- package/build/cjs/_virtual/_rollupPluginBabelHelpers.js.map +0 -1
- package/build/cjs/packages/react-virtual/src/index.js +0 -371
- package/build/cjs/packages/react-virtual/src/index.js.map +0 -1
- package/build/cjs/packages/react-virtual/src/useIsomorphicLayoutEffect.js +0 -40
- package/build/cjs/packages/react-virtual/src/useIsomorphicLayoutEffect.js.map +0 -1
- package/build/cjs/packages/react-virtual/src/useRect.js +0 -85
- package/build/cjs/packages/react-virtual/src/useRect.js.map +0 -1
- package/build/types/index.d.ts +0 -53
- package/build/types/useIsomorphicLayoutEffect.d.ts +0 -2
- package/build/types/useRect.d.ts +0 -6
- package/src/useIsomorphicLayoutEffect.ts +0 -4
- package/src/useRect.ts +0 -58
|
@@ -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 useVirtualBase(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 useVirtual(options) {
|
|
60
|
+
return useVirtualBase({
|
|
61
|
+
observeElementRect: index.observeElementRect,
|
|
62
|
+
observeElementOffset: index.observeElementOffset,
|
|
63
|
+
scrollToFn: index.elementScroll,
|
|
64
|
+
...options
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
function useVirtualWindow(options) {
|
|
68
|
+
return useVirtualBase({
|
|
69
|
+
observeElementRect: index.observeWindowRect,
|
|
70
|
+
observeElementOffset: index.observeWindowOffset,
|
|
71
|
+
scrollToFn: index.windowScroll,
|
|
72
|
+
...options
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
exports.Virtualizer = index.Virtualizer;
|
|
77
|
+
exports.defaultEstimateSize = index.defaultEstimateSize;
|
|
78
|
+
exports.defaultKeyExtractor = index.defaultKeyExtractor;
|
|
79
|
+
exports.defaultMeasureElement = index.defaultMeasureElement;
|
|
80
|
+
exports.defaultRangeExtractor = index.defaultRangeExtractor;
|
|
81
|
+
exports.elementScroll = index.elementScroll;
|
|
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.useVirtual = useVirtual;
|
|
89
|
+
exports.useVirtualWindow = useVirtualWindow;
|
|
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 useVirtualBase<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 useVirtual<TScrollElement, TItemElement = unknown>(\n options: PartialKeys<\n VirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): Virtualizer<TScrollElement, TItemElement> {\n return useVirtualBase<TScrollElement, TItemElement>({\n observeElementRect: observeElementRect,\n observeElementOffset: observeElementOffset,\n scrollToFn: elementScroll,\n ...options,\n })\n}\n\nexport function useVirtualWindow<TScrollElement, TItemElement = unknown>(\n options: PartialKeys<\n VirtualizerOptions<TScrollElement, TItemElement>,\n 'observeElementRect' | 'observeElementOffset' | 'scrollToFn'\n >,\n): Virtualizer<TScrollElement, TItemElement> {\n return useVirtualBase<TScrollElement, TItemElement>({\n observeElementRect: observeWindowRect,\n observeElementOffset: observeWindowOffset,\n scrollToFn: windowScroll,\n ...options,\n })\n}\n"],"names":["useIsomorphicLayoutEffect","window","React","useLayoutEffect","useEffect","useVirtualBase","options","rerender","useReducer","resolvedOptions","onChange","instance","useState","Virtualizer","setOptions","_didMount","_willUpdate","useVirtual","observeElementRect","observeElementOffset","scrollToFn","elementScroll","useVirtualWindow","observeWindowRect","observeWindowOffset","windowScroll"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAgBA,MAAMA,yBAAyB,GAC7B,OAAOC,MAAP,KAAkB,WAAlB,GAAgCC,gBAAK,CAACC,eAAtC,GAAwDD,gBAAK,CAACE,SADhE,CAAA;;AAGA,SAASC,cAAT,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,UAAT,CACLX,OADK,EAKsC;AAC3C,EAAA,OAAOD,cAAc,CAA+B;AAClDa,IAAAA,kBAAkB,EAAEA,wBAD8B;AAElDC,IAAAA,oBAAoB,EAAEA,0BAF4B;AAGlDC,IAAAA,UAAU,EAAEC,mBAHsC;IAIlD,GAAGf,OAAAA;AAJ+C,GAA/B,CAArB,CAAA;AAMD,CAAA;AAEM,SAASgB,gBAAT,CACLhB,OADK,EAKsC;AAC3C,EAAA,OAAOD,cAAc,CAA+B;AAClDa,IAAAA,kBAAkB,EAAEK,uBAD8B;AAElDJ,IAAAA,oBAAoB,EAAEK,yBAF4B;AAGlDJ,IAAAA,UAAU,EAAEK,kBAHsC;IAIlD,GAAGnB,OAAAA;AAJ+C,GAA/B,CAArB,CAAA;AAMD;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,563 @@
|
|
|
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 defaultEstimateSize = () => 50;
|
|
138
|
+
const defaultKeyExtractor = index => index;
|
|
139
|
+
const defaultRangeExtractor = range => {
|
|
140
|
+
const start = Math.max(range.startIndex - range.overscan, 0);
|
|
141
|
+
const end = Math.min(range.endIndex + range.overscan, range.count - 1);
|
|
142
|
+
const arr = [];
|
|
143
|
+
|
|
144
|
+
for (let i = start; i <= end; i++) {
|
|
145
|
+
arr.push(i);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
return arr;
|
|
149
|
+
};
|
|
150
|
+
const observeElementRect = (instance, cb) => {
|
|
151
|
+
const observer = observeRect(instance.scrollElement, rect => {
|
|
152
|
+
cb(rect);
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
if (!instance.scrollElement) {
|
|
156
|
+
return;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
cb(instance.scrollElement.getBoundingClientRect());
|
|
160
|
+
observer.observe();
|
|
161
|
+
return () => {
|
|
162
|
+
observer.unobserve();
|
|
163
|
+
};
|
|
164
|
+
};
|
|
165
|
+
const observeWindowRect = (instance, cb) => {
|
|
166
|
+
const onResize = () => {
|
|
167
|
+
cb({
|
|
168
|
+
width: instance.scrollElement.innerWidth,
|
|
169
|
+
height: instance.scrollElement.innerHeight
|
|
170
|
+
});
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
if (!instance.scrollElement) {
|
|
174
|
+
return;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
onResize();
|
|
178
|
+
instance.scrollElement.addEventListener('resize', onResize, {
|
|
179
|
+
capture: false,
|
|
180
|
+
passive: true
|
|
181
|
+
});
|
|
182
|
+
return () => {
|
|
183
|
+
instance.scrollElement.removeEventListener('resize', onResize);
|
|
184
|
+
};
|
|
185
|
+
};
|
|
186
|
+
const observeElementOffset = (instance, cb) => {
|
|
187
|
+
const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollLeft' : 'scrollTop']);
|
|
188
|
+
|
|
189
|
+
if (!instance.scrollElement) {
|
|
190
|
+
return;
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
onScroll();
|
|
194
|
+
instance.scrollElement.addEventListener('scroll', onScroll, {
|
|
195
|
+
capture: false,
|
|
196
|
+
passive: true
|
|
197
|
+
});
|
|
198
|
+
return () => {
|
|
199
|
+
instance.scrollElement.removeEventListener('scroll', onScroll);
|
|
200
|
+
};
|
|
201
|
+
};
|
|
202
|
+
const observeWindowOffset = (instance, cb) => {
|
|
203
|
+
const onScroll = () => cb(instance.scrollElement[instance.options.horizontal ? 'scrollX' : 'scrollY']);
|
|
204
|
+
|
|
205
|
+
if (!instance.scrollElement) {
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
onScroll();
|
|
210
|
+
instance.scrollElement.addEventListener('scroll', onScroll, {
|
|
211
|
+
capture: false,
|
|
212
|
+
passive: true
|
|
213
|
+
});
|
|
214
|
+
return () => {
|
|
215
|
+
instance.scrollElement.removeEventListener('scroll', onScroll);
|
|
216
|
+
};
|
|
217
|
+
};
|
|
218
|
+
const defaultMeasureElement = (element, instance) => {
|
|
219
|
+
return element.getBoundingClientRect()[instance.getSizeKey()];
|
|
220
|
+
};
|
|
221
|
+
const windowScroll = (offset, canSmooth, instance) => {
|
|
222
|
+
var _instance$scrollEleme;
|
|
223
|
+
(_instance$scrollEleme = instance.scrollElement) == null ? void 0 : _instance$scrollEleme.scrollTo({
|
|
224
|
+
[instance.options.horizontal ? 'left' : 'top']: offset,
|
|
225
|
+
behavior: canSmooth ? 'smooth' : undefined
|
|
226
|
+
});
|
|
227
|
+
};
|
|
228
|
+
const elementScroll = (offset, canSmooth, instance) => {
|
|
229
|
+
var _instance$scrollEleme2;
|
|
230
|
+
(_instance$scrollEleme2 = instance.scrollElement) == null ? void 0 : _instance$scrollEleme2.scrollTo({
|
|
231
|
+
[instance.options.horizontal ? 'left' : 'top']: offset,
|
|
232
|
+
behavior: canSmooth ? 'smooth' : undefined
|
|
233
|
+
});
|
|
234
|
+
};
|
|
235
|
+
class Virtualizer {
|
|
236
|
+
//
|
|
237
|
+
// virtualItems: VirtualItem<TItemElement>[]
|
|
238
|
+
// totalSize: number
|
|
239
|
+
// scrollToOffset: (offset: number, options?: ScrollToOffsetOptions) => void
|
|
240
|
+
// scrollToIndex: (index: number, options?: ScrollToIndexOptions) => void
|
|
241
|
+
// measure: (index: number) => void
|
|
242
|
+
constructor(_opts) {
|
|
243
|
+
var _this = this;
|
|
244
|
+
|
|
245
|
+
this.unsubs = [];
|
|
246
|
+
this.scrollElement = null;
|
|
247
|
+
this.measurementsCache = [];
|
|
248
|
+
this.itemMeasurementsCache = {};
|
|
249
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
250
|
+
|
|
251
|
+
this.setOptions = opts => {
|
|
252
|
+
Object.entries(opts).forEach(_ref => {
|
|
253
|
+
let [key, value] = _ref;
|
|
254
|
+
if (typeof value === 'undefined') delete opts[key];
|
|
255
|
+
});
|
|
256
|
+
this.options = {
|
|
257
|
+
debug: false,
|
|
258
|
+
initialOffset: 0,
|
|
259
|
+
estimateSize: defaultEstimateSize,
|
|
260
|
+
overscan: 1,
|
|
261
|
+
paddingStart: 0,
|
|
262
|
+
paddingEnd: 0,
|
|
263
|
+
horizontal: false,
|
|
264
|
+
keyExtractor: defaultKeyExtractor,
|
|
265
|
+
rangeExtractor: defaultRangeExtractor,
|
|
266
|
+
enableSmoothScroll: false,
|
|
267
|
+
onChange: () => {},
|
|
268
|
+
measureElement: defaultMeasureElement,
|
|
269
|
+
initialRect: {
|
|
270
|
+
width: 0,
|
|
271
|
+
height: 0
|
|
272
|
+
},
|
|
273
|
+
...opts
|
|
274
|
+
};
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
this.notify = () => {
|
|
278
|
+
var _this$options$onChang, _this$options;
|
|
279
|
+
|
|
280
|
+
(_this$options$onChang = (_this$options = this.options).onChange) == null ? void 0 : _this$options$onChang.call(_this$options, this);
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
this.cleanup = () => {
|
|
284
|
+
this.unsubs.filter(Boolean).forEach(d => d());
|
|
285
|
+
this.unsubs = [];
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
this._didMount = () => {
|
|
289
|
+
return () => {
|
|
290
|
+
this.cleanup();
|
|
291
|
+
};
|
|
292
|
+
};
|
|
293
|
+
|
|
294
|
+
this._willUpdate = () => {
|
|
295
|
+
const scrollElement = this.options.getScrollElement();
|
|
296
|
+
|
|
297
|
+
if (this.scrollElement !== scrollElement) {
|
|
298
|
+
this.cleanup();
|
|
299
|
+
this.scrollElement = scrollElement;
|
|
300
|
+
this.unsubs.push(this.options.observeElementRect(this, rect => {
|
|
301
|
+
this.scrollRect = rect;
|
|
302
|
+
this.notify();
|
|
303
|
+
}));
|
|
304
|
+
this.unsubs.push(this.options.observeElementOffset(this, offset => {
|
|
305
|
+
this.scrollOffset = offset;
|
|
306
|
+
this.notify();
|
|
307
|
+
}));
|
|
308
|
+
}
|
|
309
|
+
};
|
|
310
|
+
|
|
311
|
+
this.getSize = () => {
|
|
312
|
+
return this.scrollRect[this.getSizeKey()];
|
|
313
|
+
};
|
|
314
|
+
|
|
315
|
+
this.getMeasurements = memo(() => [this.options.count, this.options.paddingStart, this.getEstimateSizeFn(), this.options.keyExtractor, this.itemMeasurementsCache], (count, paddingStart, estimateSize, keyExtractor, measurementsCache) => {
|
|
316
|
+
const min = this.pendingMeasuredCacheIndexes.length > 0 ? Math.min(...this.pendingMeasuredCacheIndexes) : 0;
|
|
317
|
+
this.pendingMeasuredCacheIndexes = [];
|
|
318
|
+
const measurements = this.measurementsCache.slice(0, min);
|
|
319
|
+
|
|
320
|
+
for (let i = min; i < count; i++) {
|
|
321
|
+
const key = keyExtractor(i);
|
|
322
|
+
const measuredSize = measurementsCache[key];
|
|
323
|
+
const start = measurements[i - 1] ? measurements[i - 1].end : paddingStart;
|
|
324
|
+
const size = typeof measuredSize === 'number' ? measuredSize : estimateSize(i);
|
|
325
|
+
const end = start + size;
|
|
326
|
+
measurements[i] = {
|
|
327
|
+
index: i,
|
|
328
|
+
start,
|
|
329
|
+
size,
|
|
330
|
+
end,
|
|
331
|
+
key
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
this.measurementsCache = measurements;
|
|
336
|
+
return measurements;
|
|
337
|
+
}, {
|
|
338
|
+
key: process.env.NODE_ENV === 'development' && 'getMeasurements',
|
|
339
|
+
debug: () => this.options.debug
|
|
340
|
+
});
|
|
341
|
+
this.calculateRange = memo(() => [this.getMeasurements(), this.getSize(), this.scrollOffset], (measurements, outerSize, scrollOffset) => {
|
|
342
|
+
return calculateRange({
|
|
343
|
+
measurements,
|
|
344
|
+
outerSize,
|
|
345
|
+
scrollOffset
|
|
346
|
+
});
|
|
347
|
+
}, {
|
|
348
|
+
key: process.env.NODE_ENV === 'development' && 'calculateRange',
|
|
349
|
+
debug: () => this.options.debug
|
|
350
|
+
});
|
|
351
|
+
this.getIndexes = memo(() => [this.options.rangeExtractor, this.calculateRange(), this.options.overscan, this.options.count], (rangeExtractor, range, overscan, count) => {
|
|
352
|
+
return rangeExtractor({ ...range,
|
|
353
|
+
overscan,
|
|
354
|
+
count: count
|
|
355
|
+
});
|
|
356
|
+
}, {
|
|
357
|
+
key: process.env.NODE_ENV === 'development' && 'getIndexes'
|
|
358
|
+
});
|
|
359
|
+
this.getVirtualItems = memo(() => [this.getIndexes(), this.getMeasurements(), this.options.measureElement], (indexes, measurements, measureElement) => {
|
|
360
|
+
const virtualItems = [];
|
|
361
|
+
|
|
362
|
+
for (let k = 0, len = indexes.length; k < len; k++) {
|
|
363
|
+
const i = indexes[k];
|
|
364
|
+
const measurement = measurements[i];
|
|
365
|
+
const item = { ...measurement,
|
|
366
|
+
measureElement: measurableItem => {
|
|
367
|
+
if (measurableItem) {
|
|
368
|
+
const measuredItemSize = measureElement(measurableItem, this);
|
|
369
|
+
|
|
370
|
+
if (measuredItemSize !== item.size) {
|
|
371
|
+
if (item.start < this.scrollOffset) {
|
|
372
|
+
if (process.env.NODE_ENV === 'development' && this.options.debug) console.info('correction', measuredItemSize - item.size);
|
|
373
|
+
|
|
374
|
+
this._scrollToOffset(this.scrollOffset + (measuredItemSize - item.size), false);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
this.pendingMeasuredCacheIndexes.push(i);
|
|
378
|
+
this.itemMeasurementsCache = { ...this.itemMeasurementsCache,
|
|
379
|
+
[item.key]: measuredItemSize
|
|
380
|
+
};
|
|
381
|
+
this.notify();
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
};
|
|
386
|
+
virtualItems.push(item);
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
return virtualItems;
|
|
390
|
+
}, {
|
|
391
|
+
key: process.env.NODE_ENV === 'development' && 'getIndexes'
|
|
392
|
+
});
|
|
393
|
+
|
|
394
|
+
this.scrollToOffset = function (toOffset, _temp) {
|
|
395
|
+
let {
|
|
396
|
+
align
|
|
397
|
+
} = _temp === void 0 ? {
|
|
398
|
+
align: 'start'
|
|
399
|
+
} : _temp;
|
|
400
|
+
const offset = _this.scrollOffset;
|
|
401
|
+
|
|
402
|
+
const size = _this.getSize();
|
|
403
|
+
|
|
404
|
+
if (align === 'auto') {
|
|
405
|
+
if (toOffset <= offset) {
|
|
406
|
+
align = 'start';
|
|
407
|
+
} else if (toOffset >= offset + size) {
|
|
408
|
+
align = 'end';
|
|
409
|
+
} else {
|
|
410
|
+
align = 'start';
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
if (align === 'start') {
|
|
415
|
+
_this._scrollToOffset(toOffset, true);
|
|
416
|
+
} else if (align === 'end') {
|
|
417
|
+
_this._scrollToOffset(toOffset - size, true);
|
|
418
|
+
} else if (align === 'center') {
|
|
419
|
+
_this._scrollToOffset(toOffset - size / 2, true);
|
|
420
|
+
}
|
|
421
|
+
};
|
|
422
|
+
|
|
423
|
+
this.tryScrollToIndex = function (index, _temp2) {
|
|
424
|
+
let {
|
|
425
|
+
align,
|
|
426
|
+
...rest
|
|
427
|
+
} = _temp2 === void 0 ? {
|
|
428
|
+
align: 'auto'
|
|
429
|
+
} : _temp2;
|
|
430
|
+
|
|
431
|
+
const measurements = _this.getMeasurements();
|
|
432
|
+
|
|
433
|
+
const offset = _this.scrollOffset;
|
|
434
|
+
|
|
435
|
+
const size = _this.getSize();
|
|
436
|
+
|
|
437
|
+
const {
|
|
438
|
+
count
|
|
439
|
+
} = _this.options;
|
|
440
|
+
const measurement = measurements[Math.max(0, Math.min(index, count - 1))];
|
|
441
|
+
|
|
442
|
+
if (!measurement) {
|
|
443
|
+
return;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
if (align === 'auto') {
|
|
447
|
+
if (measurement.end >= offset + size) {
|
|
448
|
+
align = 'end';
|
|
449
|
+
} else if (measurement.start <= offset) {
|
|
450
|
+
align = 'start';
|
|
451
|
+
} else {
|
|
452
|
+
return;
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
const toOffset = align === 'center' ? measurement.start + measurement.size / 2 : align === 'end' ? measurement.end : measurement.start;
|
|
457
|
+
|
|
458
|
+
_this.scrollToOffset(toOffset, {
|
|
459
|
+
align,
|
|
460
|
+
...rest
|
|
461
|
+
});
|
|
462
|
+
};
|
|
463
|
+
|
|
464
|
+
this.scrollToIndex = (index, options) => {
|
|
465
|
+
// We do a double request here because of
|
|
466
|
+
// dynamic sizes which can cause offset shift
|
|
467
|
+
// and end up in the wrong spot. Unfortunately,
|
|
468
|
+
// we can't know about those dynamic sizes until
|
|
469
|
+
// we try and render them. So double down!
|
|
470
|
+
this.tryScrollToIndex(index, options);
|
|
471
|
+
requestAnimationFrame(() => {
|
|
472
|
+
this.tryScrollToIndex(index, options);
|
|
473
|
+
});
|
|
474
|
+
};
|
|
475
|
+
|
|
476
|
+
this.getTotalSize = () => {
|
|
477
|
+
var _this$getMeasurements;
|
|
478
|
+
|
|
479
|
+
return (((_this$getMeasurements = this.getMeasurements()[this.options.count - 1]) == null ? void 0 : _this$getMeasurements.end) || this.options.paddingStart) + this.options.paddingEnd;
|
|
480
|
+
};
|
|
481
|
+
|
|
482
|
+
this.getSizeKey = () => this.options.horizontal ? 'width' : 'height';
|
|
483
|
+
|
|
484
|
+
this._scrollToOffset = (offset, canSmooth) => {
|
|
485
|
+
this.options.scrollToFn(offset, this.options.enableSmoothScroll && canSmooth, this);
|
|
486
|
+
};
|
|
487
|
+
|
|
488
|
+
this.getEstimateSizeFn = memo(() => [this.options.estimateSize], d => d, {
|
|
489
|
+
key: false,
|
|
490
|
+
onChange: () => {
|
|
491
|
+
this.itemMeasurementsCache = {};
|
|
492
|
+
}
|
|
493
|
+
});
|
|
494
|
+
|
|
495
|
+
this.measure = () => {
|
|
496
|
+
this.itemMeasurementsCache = {};
|
|
497
|
+
this.notify();
|
|
498
|
+
};
|
|
499
|
+
|
|
500
|
+
this.setOptions(_opts);
|
|
501
|
+
this.scrollRect = this.options.initialRect;
|
|
502
|
+
this.scrollOffset = this.options.initialOffset;
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
const findNearestBinarySearch = (low, high, getCurrentValue, value) => {
|
|
508
|
+
while (low <= high) {
|
|
509
|
+
const middle = (low + high) / 2 | 0;
|
|
510
|
+
const currentValue = getCurrentValue(middle);
|
|
511
|
+
|
|
512
|
+
if (currentValue < value) {
|
|
513
|
+
low = middle + 1;
|
|
514
|
+
} else if (currentValue > value) {
|
|
515
|
+
high = middle - 1;
|
|
516
|
+
} else {
|
|
517
|
+
return middle;
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
if (low > 0) {
|
|
522
|
+
return low - 1;
|
|
523
|
+
} else {
|
|
524
|
+
return 0;
|
|
525
|
+
}
|
|
526
|
+
};
|
|
527
|
+
|
|
528
|
+
function calculateRange(_ref2) {
|
|
529
|
+
let {
|
|
530
|
+
measurements,
|
|
531
|
+
outerSize,
|
|
532
|
+
scrollOffset
|
|
533
|
+
} = _ref2;
|
|
534
|
+
const count = measurements.length - 1;
|
|
535
|
+
|
|
536
|
+
const getOffset = index => measurements[index].start;
|
|
537
|
+
|
|
538
|
+
const startIndex = findNearestBinarySearch(0, count, getOffset, scrollOffset);
|
|
539
|
+
let endIndex = startIndex;
|
|
540
|
+
|
|
541
|
+
while (endIndex < count && measurements[endIndex].end < scrollOffset + outerSize) {
|
|
542
|
+
endIndex++;
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
return {
|
|
546
|
+
startIndex,
|
|
547
|
+
endIndex
|
|
548
|
+
};
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
exports.Virtualizer = Virtualizer;
|
|
552
|
+
exports.defaultEstimateSize = defaultEstimateSize;
|
|
553
|
+
exports.defaultKeyExtractor = defaultKeyExtractor;
|
|
554
|
+
exports.defaultMeasureElement = defaultMeasureElement;
|
|
555
|
+
exports.defaultRangeExtractor = defaultRangeExtractor;
|
|
556
|
+
exports.elementScroll = elementScroll;
|
|
557
|
+
exports.memo = memo;
|
|
558
|
+
exports.observeElementOffset = observeElementOffset;
|
|
559
|
+
exports.observeElementRect = observeElementRect;
|
|
560
|
+
exports.observeWindowOffset = observeWindowOffset;
|
|
561
|
+
exports.observeWindowRect = observeWindowRect;
|
|
562
|
+
exports.windowScroll = windowScroll;
|
|
563
|
+
//# sourceMappingURL=index.js.map
|