@fluentui/react-virtualizer 9.0.0-alpha.11 → 9.0.0-alpha.110

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 (196) hide show
  1. package/CHANGELOG.md +1036 -11
  2. package/README.md +2 -0
  3. package/dist/index.d.ts +417 -34
  4. package/lib/Hooks.js +1 -2
  5. package/lib/Hooks.js.map +1 -1
  6. package/lib/Utilities.js +1 -0
  7. package/lib/Utilities.js.map +1 -0
  8. package/lib/Virtualizer.js +1 -2
  9. package/lib/Virtualizer.js.map +1 -1
  10. package/lib/VirtualizerScrollView.js +1 -0
  11. package/lib/VirtualizerScrollView.js.map +1 -0
  12. package/lib/VirtualizerScrollViewDynamic.js +1 -0
  13. package/lib/VirtualizerScrollViewDynamic.js.map +1 -0
  14. package/lib/components/Virtualizer/Virtualizer.js +10 -7
  15. package/lib/components/Virtualizer/Virtualizer.js.map +1 -1
  16. package/lib/components/Virtualizer/Virtualizer.types.js +1 -2
  17. package/lib/components/Virtualizer/Virtualizer.types.js.map +1 -1
  18. package/lib/components/Virtualizer/index.js +4 -6
  19. package/lib/components/Virtualizer/index.js.map +1 -1
  20. package/lib/components/Virtualizer/renderVirtualizer.js +25 -16
  21. package/lib/components/Virtualizer/renderVirtualizer.js.map +1 -1
  22. package/lib/components/Virtualizer/useVirtualizer.js +529 -357
  23. package/lib/components/Virtualizer/useVirtualizer.js.map +1 -1
  24. package/lib/components/Virtualizer/{useVirtualizerStyles.js → useVirtualizerStyles.styles.js} +11 -4
  25. package/lib/components/Virtualizer/useVirtualizerStyles.styles.js.map +1 -0
  26. package/lib/components/Virtualizer/useVirtualizerStyles.styles.raw.js +103 -0
  27. package/lib/components/Virtualizer/useVirtualizerStyles.styles.raw.js.map +1 -0
  28. package/lib/components/VirtualizerScrollView/VirtualizerScrollView.js +16 -0
  29. package/lib/components/VirtualizerScrollView/VirtualizerScrollView.js.map +1 -0
  30. package/lib/components/VirtualizerScrollView/VirtualizerScrollView.types.js +1 -0
  31. package/lib/components/VirtualizerScrollView/VirtualizerScrollView.types.js.map +1 -0
  32. package/lib/components/VirtualizerScrollView/index.js +4 -0
  33. package/lib/components/VirtualizerScrollView/index.js.map +1 -0
  34. package/lib/components/VirtualizerScrollView/renderVirtualizerScrollView.js +11 -0
  35. package/lib/components/VirtualizerScrollView/renderVirtualizerScrollView.js.map +1 -0
  36. package/lib/components/VirtualizerScrollView/useVirtualizerScrollView.js +83 -0
  37. package/lib/components/VirtualizerScrollView/useVirtualizerScrollView.js.map +1 -0
  38. package/lib/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.js +52 -0
  39. package/lib/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.js.map +1 -0
  40. package/lib/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.raw.js +46 -0
  41. package/lib/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.raw.js.map +1 -0
  42. package/lib/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.js +18 -0
  43. package/lib/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.js.map +1 -0
  44. package/lib/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.types.js +1 -0
  45. package/lib/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.types.js.map +1 -0
  46. package/lib/components/VirtualizerScrollViewDynamic/index.js +4 -0
  47. package/lib/components/VirtualizerScrollViewDynamic/index.js.map +1 -0
  48. package/lib/components/VirtualizerScrollViewDynamic/renderVirtualizerScrollViewDynamic.js +11 -0
  49. package/lib/components/VirtualizerScrollViewDynamic/renderVirtualizerScrollViewDynamic.js.map +1 -0
  50. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js +155 -0
  51. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js.map +1 -0
  52. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js +52 -0
  53. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js.map +1 -0
  54. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.raw.js +46 -0
  55. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.raw.js.map +1 -0
  56. package/lib/hooks/hooks.types.js +1 -0
  57. package/lib/hooks/hooks.types.js.map +1 -0
  58. package/lib/hooks/index.js +5 -2
  59. package/lib/hooks/index.js.map +1 -1
  60. package/lib/hooks/useDynamicPagination.js +128 -0
  61. package/lib/hooks/useDynamicPagination.js.map +1 -0
  62. package/lib/hooks/useDynamicVirtualizerMeasure.js +129 -0
  63. package/lib/hooks/useDynamicVirtualizerMeasure.js.map +1 -0
  64. package/lib/hooks/useIntersectionObserver.js +119 -40
  65. package/lib/hooks/useIntersectionObserver.js.map +1 -1
  66. package/lib/hooks/useMeasureList.js +130 -0
  67. package/lib/hooks/useMeasureList.js.map +1 -0
  68. package/lib/hooks/useMutationObserver.js +40 -0
  69. package/lib/hooks/useMutationObserver.js.map +1 -0
  70. package/lib/hooks/useResizeObserverRef.js +61 -0
  71. package/lib/hooks/useResizeObserverRef.js.map +1 -0
  72. package/lib/hooks/useStaticPagination.js +104 -0
  73. package/lib/hooks/useStaticPagination.js.map +1 -0
  74. package/lib/hooks/useVirtualizerMeasure.js +64 -0
  75. package/lib/hooks/useVirtualizerMeasure.js.map +1 -0
  76. package/lib/index.js +4 -2
  77. package/lib/index.js.map +1 -1
  78. package/lib/utilities/ImperativeScrolling/imperativeScrolling.js +32 -0
  79. package/lib/utilities/ImperativeScrolling/imperativeScrolling.js.map +1 -0
  80. package/lib/utilities/ImperativeScrolling/imperativeScrolling.types.js +1 -0
  81. package/lib/utilities/ImperativeScrolling/imperativeScrolling.types.js.map +1 -0
  82. package/lib/utilities/ImperativeScrolling/imperativeScrollingDynamic.js +45 -0
  83. package/lib/utilities/ImperativeScrolling/imperativeScrollingDynamic.js.map +1 -0
  84. package/lib/utilities/ImperativeScrolling/index.js +2 -0
  85. package/lib/utilities/ImperativeScrolling/index.js.map +1 -0
  86. package/lib/utilities/VirtualizerContext/VirtualizerContext.js +33 -0
  87. package/lib/utilities/VirtualizerContext/VirtualizerContext.js.map +1 -0
  88. package/lib/utilities/VirtualizerContext/index.js +1 -0
  89. package/lib/utilities/VirtualizerContext/index.js.map +1 -0
  90. package/lib/utilities/VirtualizerContext/types.js +1 -0
  91. package/lib/utilities/VirtualizerContext/types.js.map +1 -0
  92. package/lib/utilities/createResizeObserverFromDocument.js +14 -0
  93. package/lib/utilities/createResizeObserverFromDocument.js.map +1 -0
  94. package/lib/utilities/debounce.js +20 -0
  95. package/lib/utilities/debounce.js.map +1 -0
  96. package/lib/utilities/index.js +2 -0
  97. package/lib/utilities/index.js.map +1 -0
  98. package/lib-commonjs/Hooks.js +31 -5
  99. package/lib-commonjs/Hooks.js.map +1 -1
  100. package/lib-commonjs/Utilities.js +28 -0
  101. package/lib-commonjs/Utilities.js.map +1 -0
  102. package/lib-commonjs/Virtualizer.js +28 -5
  103. package/lib-commonjs/Virtualizer.js.map +1 -1
  104. package/lib-commonjs/VirtualizerScrollView.js +28 -0
  105. package/lib-commonjs/VirtualizerScrollView.js.map +1 -0
  106. package/lib-commonjs/VirtualizerScrollViewDynamic.js +28 -0
  107. package/lib-commonjs/VirtualizerScrollViewDynamic.js.map +1 -0
  108. package/lib-commonjs/components/Virtualizer/Virtualizer.js +20 -18
  109. package/lib-commonjs/components/Virtualizer/Virtualizer.js.map +1 -1
  110. package/lib-commonjs/components/Virtualizer/Virtualizer.types.js +3 -3
  111. package/lib-commonjs/components/Virtualizer/Virtualizer.types.js.map +1 -1
  112. package/lib-commonjs/components/Virtualizer/index.js +31 -9
  113. package/lib-commonjs/components/Virtualizer/index.js.map +1 -1
  114. package/lib-commonjs/components/Virtualizer/renderVirtualizer.js +38 -21
  115. package/lib-commonjs/components/Virtualizer/renderVirtualizer.js.map +1 -1
  116. package/lib-commonjs/components/Virtualizer/useVirtualizer.js +535 -362
  117. package/lib-commonjs/components/Virtualizer/useVirtualizer.js.map +1 -1
  118. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.styles.js +124 -0
  119. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.styles.js.map +1 -0
  120. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.styles.raw.js +116 -0
  121. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.styles.raw.js.map +1 -0
  122. package/lib-commonjs/components/VirtualizerScrollView/VirtualizerScrollView.js +24 -0
  123. package/lib-commonjs/components/VirtualizerScrollView/VirtualizerScrollView.js.map +1 -0
  124. package/lib-commonjs/components/VirtualizerScrollView/VirtualizerScrollView.types.js +6 -0
  125. package/lib-commonjs/components/VirtualizerScrollView/VirtualizerScrollView.types.js.map +1 -0
  126. package/lib-commonjs/components/VirtualizerScrollView/index.js +31 -0
  127. package/lib-commonjs/components/VirtualizerScrollView/index.js.map +1 -0
  128. package/lib-commonjs/components/VirtualizerScrollView/renderVirtualizerScrollView.js +19 -0
  129. package/lib-commonjs/components/VirtualizerScrollView/renderVirtualizerScrollView.js.map +1 -0
  130. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollView.js +92 -0
  131. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollView.js.map +1 -0
  132. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.js +71 -0
  133. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.js.map +1 -0
  134. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.raw.js +59 -0
  135. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.raw.js.map +1 -0
  136. package/lib-commonjs/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.js +25 -0
  137. package/lib-commonjs/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.js.map +1 -0
  138. package/lib-commonjs/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.types.js +6 -0
  139. package/lib-commonjs/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.types.js.map +1 -0
  140. package/lib-commonjs/components/VirtualizerScrollViewDynamic/index.js +31 -0
  141. package/lib-commonjs/components/VirtualizerScrollViewDynamic/index.js.map +1 -0
  142. package/lib-commonjs/components/VirtualizerScrollViewDynamic/renderVirtualizerScrollViewDynamic.js +19 -0
  143. package/lib-commonjs/components/VirtualizerScrollViewDynamic/renderVirtualizerScrollViewDynamic.js.map +1 -0
  144. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js +163 -0
  145. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js.map +1 -0
  146. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js +71 -0
  147. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js.map +1 -0
  148. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.raw.js +59 -0
  149. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.raw.js.map +1 -0
  150. package/lib-commonjs/hooks/hooks.types.js +6 -0
  151. package/lib-commonjs/hooks/hooks.types.js.map +1 -0
  152. package/lib-commonjs/hooks/index.js +35 -5
  153. package/lib-commonjs/hooks/index.js.map +1 -1
  154. package/lib-commonjs/hooks/useDynamicPagination.js +132 -0
  155. package/lib-commonjs/hooks/useDynamicPagination.js.map +1 -0
  156. package/lib-commonjs/hooks/useDynamicVirtualizerMeasure.js +134 -0
  157. package/lib-commonjs/hooks/useDynamicVirtualizerMeasure.js.map +1 -0
  158. package/lib-commonjs/hooks/useIntersectionObserver.js +129 -53
  159. package/lib-commonjs/hooks/useIntersectionObserver.js.map +1 -1
  160. package/lib-commonjs/hooks/useMeasureList.js +135 -0
  161. package/lib-commonjs/hooks/useMeasureList.js.map +1 -0
  162. package/lib-commonjs/hooks/useMutationObserver.js +48 -0
  163. package/lib-commonjs/hooks/useMutationObserver.js.map +1 -0
  164. package/lib-commonjs/hooks/useResizeObserverRef.js +69 -0
  165. package/lib-commonjs/hooks/useResizeObserverRef.js.map +1 -0
  166. package/lib-commonjs/hooks/useStaticPagination.js +108 -0
  167. package/lib-commonjs/hooks/useStaticPagination.js.map +1 -0
  168. package/lib-commonjs/hooks/useVirtualizerMeasure.js +70 -0
  169. package/lib-commonjs/hooks/useVirtualizerMeasure.js.map +1 -0
  170. package/lib-commonjs/index.js +85 -41
  171. package/lib-commonjs/index.js.map +1 -1
  172. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrolling.js +42 -0
  173. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrolling.js.map +1 -0
  174. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrolling.types.js +6 -0
  175. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrolling.types.js.map +1 -0
  176. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrollingDynamic.js +55 -0
  177. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrollingDynamic.js.map +1 -0
  178. package/lib-commonjs/utilities/ImperativeScrolling/index.js +20 -0
  179. package/lib-commonjs/utilities/ImperativeScrolling/index.js.map +1 -0
  180. package/lib-commonjs/utilities/VirtualizerContext/VirtualizerContext.js +49 -0
  181. package/lib-commonjs/utilities/VirtualizerContext/VirtualizerContext.js.map +1 -0
  182. package/lib-commonjs/utilities/VirtualizerContext/index.js +22 -0
  183. package/lib-commonjs/utilities/VirtualizerContext/index.js.map +1 -0
  184. package/lib-commonjs/utilities/VirtualizerContext/types.js +6 -0
  185. package/lib-commonjs/utilities/VirtualizerContext/types.js.map +1 -0
  186. package/lib-commonjs/utilities/createResizeObserverFromDocument.js +24 -0
  187. package/lib-commonjs/utilities/createResizeObserverFromDocument.js.map +1 -0
  188. package/lib-commonjs/utilities/debounce.js +30 -0
  189. package/lib-commonjs/utilities/debounce.js.map +1 -0
  190. package/lib-commonjs/utilities/index.js +29 -0
  191. package/lib-commonjs/utilities/index.js.map +1 -0
  192. package/package.json +18 -28
  193. package/CHANGELOG.json +0 -170
  194. package/lib/components/Virtualizer/useVirtualizerStyles.js.map +0 -1
  195. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.js +0 -115
  196. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.js.map +0 -1
@@ -1,370 +1,543 @@
1
+ 'use client';
1
2
  "use strict";
2
-
3
3
  Object.defineProperty(exports, "__esModule", {
4
- value: true
4
+ value: true
5
5
  });
6
- exports.useVirtualizer_unstable = void 0;
7
- const useIntersectionObserver_1 = /*#__PURE__*/require("../../hooks/useIntersectionObserver");
8
- const react_1 = /*#__PURE__*/require("react");
9
- const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
10
- const react_dom_1 = /*#__PURE__*/require("react-dom");
11
- function useVirtualizer_unstable(props) {
12
- const {
13
- itemSize,
14
- numItems,
15
- virtualizerLength,
16
- children: renderChild,
17
- getItemSize,
18
- bufferItems = Math.round(virtualizerLength / 4.0),
19
- bufferSize = Math.floor(bufferItems / 2.0) * itemSize,
20
- intersectionObserverRoot,
21
- axis = 'vertical',
22
- reversed = false,
23
- onUpdateIndex,
24
- onCalculateIndex
25
- } = props;
26
- // Tracks the initial item to start virtualizer at, -1 implies first render cycle
27
- const [virtualizerStartIndex, setVirtualizerStartIndex] = react_1.useState(-1);
28
- // Store ref to before padding element
29
- const beforeElementRef = react_1.useRef(null);
30
- // Store ref to before padding element
31
- const afterElementRef = react_1.useRef(null);
32
- // We need to store an array to track dynamic sizes, we can use this to incrementally update changes
33
- const childSizes = react_1.useRef(new Array(getItemSize ? numItems : 0));
34
- /* We keep track of the progressive sizing/placement down the list,
35
- this helps us skip re-calculations unless children/size changes */
36
- const childProgressiveSizes = react_1.useRef(new Array(getItemSize ? numItems : 0));
37
- // The internal tracking REF for child array (updates often).
38
- const childArray = react_1.useRef(new Array(virtualizerLength));
39
- // We want to be methodical about updating the render with child reference array
40
- const forceUpdate = react_1.useReducer(() => ({}), {})[1];
41
- const horizontal = axis === 'horizontal';
42
- const populateSizeArrays = () => {
43
- if (!getItemSize) {
44
- // Static sizes, never mind!
45
- return;
46
- }
47
- if (numItems !== childSizes.current.length) {
48
- childSizes.current = new Array(numItems);
49
- }
50
- if (numItems !== childProgressiveSizes.current.length) {
51
- childProgressiveSizes.current = new Array(numItems);
52
- }
53
- for (let index = 0; index < numItems; index++) {
54
- childSizes.current[index] = getItemSize(index);
55
- if (index === 0) {
56
- childProgressiveSizes.current[index] = childSizes.current[index];
57
- } else {
58
- childProgressiveSizes.current[index] = childProgressiveSizes.current[index - 1] + childSizes.current[index];
59
- }
60
- }
61
- };
62
- const batchUpdateNewIndex = index => {
63
- // Local updates
64
- onUpdateIndex === null || onUpdateIndex === void 0 ? void 0 : onUpdateIndex(index, virtualizerStartIndex);
65
- updateChildRows(index);
66
- updateCurrentItemSizes(index);
67
- // State setters
68
- setVirtualizerStartIndex(index);
69
- };
70
- // Observe intersections of virtualized components
71
- const {
72
- setObserverList
73
- } = useIntersectionObserver_1.useIntersectionObserver((entries, observer) => {
74
- /* Sanity check - do we even need virtualization? */
75
- if (virtualizerLength > numItems) {
76
- if (virtualizerStartIndex !== 0) {
77
- batchUpdateNewIndex(0);
78
- }
79
- // No-op
80
- return;
6
+ Object.defineProperty(exports, "useVirtualizer_unstable", {
7
+ enumerable: true,
8
+ get: function() {
9
+ return useVirtualizer_unstable;
81
10
  }
82
- /* IO initiates this function when needed (bookend entering view) */
83
- let measurementPos = 0;
84
- let bufferCount = bufferItems;
85
- // Grab latest entry that is intersecting
86
- const latestEntry = entries.length === 1 ? entries[0] : entries.sort((entry1, entry2) => entry2.time - entry1.time).find(entry => {
87
- return entry.intersectionRatio > 0;
11
+ });
12
+ const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
13
+ const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
14
+ const _useIntersectionObserver = require("../../hooks/useIntersectionObserver");
15
+ const _Utilities = require("../../Utilities");
16
+ const _reactutilities = require("@fluentui/react-utilities");
17
+ const _reactdom = require("react-dom");
18
+ function useVirtualizer_unstable(props) {
19
+ 'use no memo';
20
+ const { itemSize, numItems, virtualizerLength, children: renderChild, getItemSize, bufferItems = Math.round(virtualizerLength / 4.0), bufferSize = Math.floor(bufferItems / 2.0) * itemSize, axis = 'vertical', reversed = false, virtualizerContext, onRenderedFlaggedIndex, imperativeVirtualizerRef, containerSizeRef, scrollViewRef, enableScrollLoad, updateScrollPosition, gap = 0 } = props;
21
+ /* The context is optional, it's useful for injecting additional index logic, or performing uniform state updates*/ const _virtualizerContext = (0, _Utilities.useVirtualizerContextState_unstable)(virtualizerContext);
22
+ // We use this ref as a constant source to access the virtualizer's state imperatively
23
+ const actualIndexRef = _react.useRef(_virtualizerContext.contextIndex);
24
+ const flaggedIndex = _react.useRef(null);
25
+ const actualIndex = _virtualizerContext.contextIndex;
26
+ // Just in case our ref gets out of date vs the context during a re-render
27
+ if (_virtualizerContext.contextIndex !== actualIndexRef.current) {
28
+ actualIndexRef.current = _virtualizerContext.contextIndex;
29
+ }
30
+ const setActualIndex = _react.useCallback((index)=>{
31
+ actualIndexRef.current = index;
32
+ _virtualizerContext.setContextIndex(index);
33
+ }, [
34
+ _virtualizerContext
35
+ ]);
36
+ // Store ref to before padding element
37
+ const beforeElementRef = _react.useRef(null);
38
+ // Store ref to before padding element
39
+ const afterElementRef = _react.useRef(null);
40
+ // We need to store an array to track dynamic sizes, we can use this to incrementally update changes
41
+ const childSizes = _react.useRef(new Array(getItemSize ? numItems : 0));
42
+ /* We keep track of the progressive sizing/placement down the list,
43
+ this helps us skip re-calculations unless children/size changes */ const childProgressiveSizes = _react.useRef(new Array(getItemSize ? numItems : 0));
44
+ if (virtualizerContext === null || virtualizerContext === void 0 ? void 0 : virtualizerContext.childProgressiveSizes) {
45
+ virtualizerContext.childProgressiveSizes.current = childProgressiveSizes.current;
46
+ }
47
+ // The internal tracking REF for child array (updates often).
48
+ const childArray = _react.useRef(new Array(virtualizerLength));
49
+ const populateSizeArrays = ()=>{
50
+ if (!getItemSize) {
51
+ // Static sizes, never mind!
52
+ return;
53
+ }
54
+ if (numItems !== childSizes.current.length) {
55
+ childSizes.current = new Array(numItems);
56
+ }
57
+ if (numItems !== childProgressiveSizes.current.length) {
58
+ childProgressiveSizes.current = new Array(numItems);
59
+ if (virtualizerContext === null || virtualizerContext === void 0 ? void 0 : virtualizerContext.childProgressiveSizes) {
60
+ virtualizerContext.childProgressiveSizes.current = childProgressiveSizes.current;
61
+ }
62
+ }
63
+ for(let index = 0; index < numItems; index++){
64
+ const _gap = index < numItems - 1 ? gap : 0;
65
+ childSizes.current[index] = getItemSize(index) + _gap;
66
+ if (index === 0) {
67
+ childProgressiveSizes.current[index] = childSizes.current[index];
68
+ } else {
69
+ childProgressiveSizes.current[index] = childProgressiveSizes.current[index - 1] + childSizes.current[index];
70
+ }
71
+ }
72
+ };
73
+ const [isScrolling, setIsScrolling] = _react.useState(false);
74
+ const [setScrollTimer, clearScrollTimer] = (0, _reactutilities.useTimeout)();
75
+ const scrollCounter = _react.useRef(0);
76
+ const initializeScrollingTimer = _react.useCallback(()=>{
77
+ if (!enableScrollLoad) {
78
+ // Disabled by default for reduction of render callbacks
79
+ setIsScrolling(false);
80
+ clearScrollTimer();
81
+ return;
82
+ }
83
+ /*
84
+ * This can be considered the 'velocity' required to start 'isScrolling'
85
+ * INIT_SCROLL_FLAG_REQ: Number of renders required to activate isScrolling
86
+ * INIT_SCROLL_FLAG_DELAY: Amount of time (ms) before current number of renders is reset
87
+ * - Maybe we should let users customize these in the future.
88
+ */ const INIT_SCROLL_FLAG_REQ = 10;
89
+ const INIT_SCROLL_FLAG_DELAY = 100;
90
+ scrollCounter.current++;
91
+ if (scrollCounter.current >= INIT_SCROLL_FLAG_REQ) {
92
+ setIsScrolling(true);
93
+ }
94
+ clearScrollTimer();
95
+ setScrollTimer(()=>{
96
+ setIsScrolling(false);
97
+ scrollCounter.current = 0;
98
+ }, INIT_SCROLL_FLAG_DELAY);
99
+ }, [
100
+ clearScrollTimer,
101
+ setScrollTimer,
102
+ enableScrollLoad
103
+ ]);
104
+ _react.useEffect(()=>{
105
+ initializeScrollingTimer();
106
+ }, [
107
+ actualIndex,
108
+ initializeScrollingTimer
109
+ ]);
110
+ const updateChildRows = _react.useCallback((newIndex)=>{
111
+ if (numItems === 0) {
112
+ /* Nothing to virtualize */ return;
113
+ }
114
+ /*
115
+ We reset the array every time to ensure children are re-rendered
116
+ This function should only be called when update is nessecary
117
+ */ childArray.current = new Array(virtualizerLength);
118
+ const _actualIndex = Math.max(newIndex, 0);
119
+ const end = Math.min(_actualIndex + virtualizerLength, numItems);
120
+ for(let i = _actualIndex; i < end; i++){
121
+ childArray.current[i - _actualIndex] = renderChild(i, isScrolling);
122
+ }
123
+ }, [
124
+ isScrolling,
125
+ numItems,
126
+ renderChild,
127
+ virtualizerLength
128
+ ]);
129
+ const updateCurrentItemSizes = _react.useCallback((newIndex)=>{
130
+ if (!getItemSize) {
131
+ // Static sizes, not required.
132
+ return;
133
+ }
134
+ // We should always call our size function on index change (only for the items that will be rendered)
135
+ // This ensures we request the latest data for incoming items in case sizing has changed.
136
+ const endIndex = Math.min(newIndex + virtualizerLength, numItems);
137
+ const startIndex = Math.max(newIndex, 0);
138
+ let didUpdate = false;
139
+ for(let i = startIndex; i < endIndex; i++){
140
+ const _gap = i < numItems - 1 ? gap : 0;
141
+ const newSize = getItemSize(i) + _gap;
142
+ if (newSize !== childSizes.current[i]) {
143
+ childSizes.current[i] = newSize;
144
+ didUpdate = true;
145
+ }
146
+ }
147
+ if (didUpdate) {
148
+ // Update our progressive size array
149
+ for(let i = startIndex; i < numItems; i++){
150
+ const prevSize = i > 0 ? childProgressiveSizes.current[i - 1] : 0;
151
+ childProgressiveSizes.current[i] = prevSize + childSizes.current[i];
152
+ }
153
+ }
154
+ }, [
155
+ getItemSize,
156
+ numItems,
157
+ virtualizerLength,
158
+ gap
159
+ ]);
160
+ const batchUpdateNewIndex = _react.useCallback((index)=>{
161
+ // Local updates
162
+ updateChildRows(index);
163
+ updateCurrentItemSizes(index);
164
+ // State setters
165
+ setActualIndex(index);
166
+ }, [
167
+ setActualIndex,
168
+ updateChildRows,
169
+ updateCurrentItemSizes
170
+ ]);
171
+ const findIndexRecursive = _react.useCallback((scrollPos, lowIndex, highIndex)=>{
172
+ if (lowIndex > highIndex) {
173
+ // We shouldn't get here - but no-op the index if we do.
174
+ return actualIndex;
175
+ }
176
+ const midpoint = Math.floor((lowIndex + highIndex) / 2);
177
+ const iBefore = Math.max(midpoint - 1, 0);
178
+ const iAfter = Math.min(midpoint + 1, childProgressiveSizes.current.length - 1);
179
+ const indexValue = childProgressiveSizes.current[midpoint];
180
+ const afterIndexValue = childProgressiveSizes.current[iAfter];
181
+ const beforeIndexValue = childProgressiveSizes.current[iBefore];
182
+ if (scrollPos <= afterIndexValue && scrollPos >= beforeIndexValue) {
183
+ /* We've found our index - if we are exactly matching before/after index that's ok,
184
+ better to reduce checks if it's right on the boundary. */ return midpoint;
185
+ }
186
+ if (indexValue > scrollPos) {
187
+ return findIndexRecursive(scrollPos, lowIndex, midpoint - 1);
188
+ } else {
189
+ return findIndexRecursive(scrollPos, midpoint + 1, highIndex);
190
+ }
191
+ }, [
192
+ actualIndex
193
+ ]);
194
+ const getIndexFromSizeArray = _react.useCallback((scrollPos)=>{
195
+ /* Quick searches our progressive height array */ if (scrollPos === 0 || childProgressiveSizes.current.length === 0 || scrollPos <= childProgressiveSizes.current[0]) {
196
+ // Check start
197
+ return 0;
198
+ }
199
+ if (scrollPos >= childProgressiveSizes.current[childProgressiveSizes.current.length - 1]) {
200
+ // Check end
201
+ return childProgressiveSizes.current.length - 1;
202
+ }
203
+ return findIndexRecursive(scrollPos, 0, childProgressiveSizes.current.length - 1);
204
+ }, [
205
+ findIndexRecursive
206
+ ]);
207
+ const getIndexFromScrollPosition = _react.useCallback((scrollPos)=>{
208
+ if (!getItemSize) {
209
+ return Math.round(scrollPos / (itemSize + gap));
210
+ }
211
+ return getIndexFromSizeArray(scrollPos);
212
+ }, [
213
+ getIndexFromSizeArray,
214
+ getItemSize,
215
+ itemSize,
216
+ gap
217
+ ]);
218
+ const calculateTotalSize = _react.useCallback(()=>{
219
+ if (!getItemSize) {
220
+ return (itemSize + gap) * numItems;
221
+ }
222
+ // Time for custom size calcs
223
+ return childProgressiveSizes.current[numItems - 1];
224
+ }, [
225
+ getItemSize,
226
+ itemSize,
227
+ numItems,
228
+ gap
229
+ ]);
230
+ const calculateBefore = _react.useCallback(()=>{
231
+ const currentIndex = Math.min(actualIndex, numItems - 1);
232
+ if (!getItemSize) {
233
+ // The missing items from before virtualization starts height
234
+ return currentIndex * (itemSize + gap);
235
+ }
236
+ if (currentIndex <= 0) {
237
+ return 0;
238
+ }
239
+ // Time for custom size calcs
240
+ return childProgressiveSizes.current[currentIndex - 1];
241
+ }, [
242
+ actualIndex,
243
+ getItemSize,
244
+ itemSize,
245
+ numItems,
246
+ gap
247
+ ]);
248
+ const calculateAfter = _react.useCallback(()=>{
249
+ if (numItems === 0 || actualIndex + virtualizerLength >= numItems) {
250
+ return 0;
251
+ }
252
+ const lastItemIndex = Math.min(actualIndex + virtualizerLength, numItems);
253
+ if (!getItemSize) {
254
+ // The missing items from after virtualization ends height
255
+ const remainingItems = numItems - lastItemIndex;
256
+ return remainingItems * (itemSize + gap) - gap;
257
+ }
258
+ // Time for custom size calcs
259
+ return childProgressiveSizes.current[numItems - 1] - childProgressiveSizes.current[lastItemIndex - 1];
260
+ }, [
261
+ actualIndex,
262
+ getItemSize,
263
+ itemSize,
264
+ numItems,
265
+ virtualizerLength,
266
+ gap
267
+ ]);
268
+ // We store the number of items since last render, we will allow an update if the number of items changes
269
+ const previousNumItems = _react.useRef(numItems);
270
+ // Observe intersections of virtualized components
271
+ const { setObserverList } = (0, _useIntersectionObserver.useIntersectionObserver)(_react.useCallback((entries, observer)=>{
272
+ /* Sanity check - do we even need virtualization? */ if (virtualizerLength > numItems) {
273
+ if (actualIndex !== 0) {
274
+ batchUpdateNewIndex(0);
275
+ }
276
+ // No-op
277
+ return;
278
+ }
279
+ if (entries.length === 0) {
280
+ // No entries found, return.
281
+ return;
282
+ }
283
+ // Find the latest entry that is intersecting
284
+ const sortedEntries = entries.sort((entry1, entry2)=>entry2.time - entry1.time);
285
+ const latestEntry = sortedEntries.find((entry)=>{
286
+ return entry.isIntersecting;
287
+ });
288
+ if (!latestEntry) {
289
+ return;
290
+ }
291
+ // We have to be sure our item sizes are up to date with current indexed ref before calculation
292
+ // Check if we still need
293
+ updateCurrentItemSizes(actualIndexRef.current);
294
+ const calculateOverBuffer = ()=>{
295
+ /**
296
+ * We avoid using the scroll ref scrollTop, it may be incorrect
297
+ * as virtualization may exist within a scroll view with other elements
298
+ * The benefit of using IO is that we can detect relative scrolls,
299
+ * so any items can be placed around the virtualizer in the scroll view
300
+ */ let measurementPos = 0;
301
+ if (latestEntry.target === afterElementRef.current) {
302
+ // Get after buffers position
303
+ measurementPos = calculateTotalSize() - calculateAfter();
304
+ // Get exact intersection position based on overflow size (how far into IO did we scroll?)
305
+ const overflowAmount = axis === 'vertical' ? latestEntry.intersectionRect.height : latestEntry.intersectionRect.width;
306
+ // Add to original after position
307
+ measurementPos += overflowAmount;
308
+ // Ignore buffer size (IO offset)
309
+ measurementPos -= bufferSize;
310
+ var _containerSizeRef_current;
311
+ // we hit the after buffer and detected the end of view, we need to find the start index.
312
+ measurementPos -= (_containerSizeRef_current = containerSizeRef.current) !== null && _containerSizeRef_current !== void 0 ? _containerSizeRef_current : 0;
313
+ // Calculate how far past the window bounds we are (this will be zero if IO is within window)
314
+ const hOverflow = latestEntry.boundingClientRect.top - latestEntry.intersectionRect.top;
315
+ const hOverflowReversed = latestEntry.boundingClientRect.bottom - latestEntry.intersectionRect.bottom;
316
+ const wOverflow = latestEntry.boundingClientRect.left - latestEntry.intersectionRect.left;
317
+ const wOverflowReversed = latestEntry.boundingClientRect.right - latestEntry.intersectionRect.right;
318
+ const widthOverflow = reversed ? wOverflowReversed : wOverflow;
319
+ const heightOverflow = reversed ? hOverflowReversed : hOverflow;
320
+ const additionalOverflow = axis === 'vertical' ? heightOverflow : widthOverflow;
321
+ if (reversed) {
322
+ measurementPos += additionalOverflow;
323
+ } else {
324
+ measurementPos -= additionalOverflow;
325
+ }
326
+ } else if (latestEntry.target === beforeElementRef.current) {
327
+ // Get before buffers position
328
+ measurementPos = calculateBefore();
329
+ // Get exact intersection position based on overflow size (how far into window did we scroll IO?)
330
+ const overflowAmount = axis === 'vertical' ? latestEntry.intersectionRect.height : latestEntry.intersectionRect.width;
331
+ // Minus from original before position
332
+ measurementPos -= overflowAmount;
333
+ // Ignore buffer size (IO offset)
334
+ measurementPos += bufferSize;
335
+ // Calculate how far past the window bounds we are (this will be zero if IO is within window)
336
+ const hOverflow = latestEntry.boundingClientRect.bottom - latestEntry.intersectionRect.bottom;
337
+ const hOverflowReversed = latestEntry.boundingClientRect.top - latestEntry.intersectionRect.top;
338
+ const wOverflow = latestEntry.boundingClientRect.right - latestEntry.intersectionRect.right;
339
+ const wOverflowReversed = latestEntry.boundingClientRect.left - latestEntry.intersectionRect.left;
340
+ const widthOverflow = reversed ? wOverflowReversed : wOverflow;
341
+ const heightOverflow = reversed ? hOverflowReversed : hOverflow;
342
+ const additionalOverflow = axis === 'vertical' ? heightOverflow : widthOverflow;
343
+ if (reversed) {
344
+ measurementPos += additionalOverflow;
345
+ } else {
346
+ measurementPos -= additionalOverflow;
347
+ }
348
+ }
349
+ return measurementPos;
350
+ };
351
+ // Get exact relative 'scrollTop' via IO values
352
+ const measurementPos = calculateOverBuffer();
353
+ const maxIndex = Math.max(numItems - virtualizerLength, 0);
354
+ const startIndex = getIndexFromScrollPosition(measurementPos) - bufferItems;
355
+ // Safety limits
356
+ const newStartIndex = Math.min(Math.max(startIndex, 0), maxIndex);
357
+ (0, _reactdom.flushSync)(()=>{
358
+ // Callback to allow measure functions to check virtualizer length
359
+ if (previousNumItems.current === numItems && newStartIndex + virtualizerLength >= numItems && actualIndex + virtualizerLength >= numItems) {
360
+ // We've already hit the end, no need to update state.
361
+ return;
362
+ }
363
+ // We should ensure we update virtualizer calculations if the length changes
364
+ previousNumItems.current = virtualizerLength;
365
+ updateScrollPosition === null || updateScrollPosition === void 0 ? void 0 : updateScrollPosition(measurementPos);
366
+ if (actualIndex !== newStartIndex) {
367
+ batchUpdateNewIndex(newStartIndex);
368
+ }
369
+ });
370
+ }, [
371
+ actualIndex,
372
+ virtualizerLength,
373
+ axis,
374
+ reversed,
375
+ numItems,
376
+ bufferSize,
377
+ bufferItems,
378
+ containerSizeRef,
379
+ updateScrollPosition,
380
+ batchUpdateNewIndex,
381
+ calculateAfter,
382
+ calculateBefore,
383
+ calculateTotalSize,
384
+ getIndexFromScrollPosition,
385
+ updateCurrentItemSizes
386
+ ]), {
387
+ root: scrollViewRef ? scrollViewRef === null || scrollViewRef === void 0 ? void 0 : scrollViewRef.current : null,
388
+ rootMargin: '0px',
389
+ threshold: 0
88
390
  });
89
- if (!latestEntry) {
90
- // If we don't find an intersecting area, ignore for now.
91
- return;
92
- }
93
- if (latestEntry.target === afterElementRef.current) {
94
- // We need to inverse the buffer count
95
- bufferCount = virtualizerLength - bufferItems;
96
- measurementPos = reversed ? calculateAfter() : calculateTotalSize() - calculateAfter();
97
- if (!horizontal) {
98
- if (reversed) {
99
- // Scrolling 'up' and hit the after element below
100
- measurementPos -= Math.abs(latestEntry.boundingClientRect.bottom);
101
- } else if (latestEntry.boundingClientRect.top < 0) {
102
- // Scrolling 'down' and hit the after element above top: 0
103
- measurementPos -= latestEntry.boundingClientRect.top;
104
- }
105
- } else {
106
- if (reversed) {
107
- // Scrolling 'left' and hit the after element
108
- measurementPos -= Math.abs(latestEntry.boundingClientRect.right);
109
- } else if (latestEntry.boundingClientRect.left < 0) {
110
- // Scrolling 'right' and hit the after element
111
- measurementPos -= latestEntry.boundingClientRect.left;
112
- }
113
- }
114
- } else if (latestEntry.target === beforeElementRef.current) {
115
- measurementPos = reversed ? calculateTotalSize() - calculateBefore() : calculateBefore();
116
- if (!horizontal) {
117
- if (!reversed) {
118
- measurementPos -= Math.abs(latestEntry.boundingClientRect.bottom);
119
- } else if (latestEntry.boundingClientRect.top < 0) {
120
- // Scrolling 'down' in reverse order and hit the before element above top: 0
121
- measurementPos -= latestEntry.boundingClientRect.top;
122
- }
123
- } else {
124
- if (!reversed) {
125
- measurementPos -= Math.abs(latestEntry.boundingClientRect.right);
126
- } else if (latestEntry.boundingClientRect.left < 0) {
127
- // Scrolling 'left' and hit before element
128
- measurementPos -= latestEntry.boundingClientRect.left;
129
- }
130
- }
131
- }
132
- if (reversed) {
133
- // We're reversed, up is down, left is right, invert the scroll measure.
134
- measurementPos = Math.max(calculateTotalSize() - Math.abs(measurementPos), 0);
135
- }
136
- // For now lets use hardcoded size to assess current element to paginate on
137
- const startIndex = getIndexFromScrollPosition(measurementPos);
138
- let bufferedIndex = Math.max(startIndex - bufferCount, 0);
139
- if (onCalculateIndex) {
140
- // User has chance to intervene/customize prior to render
141
- // They may want to normalize this value.
142
- bufferedIndex = onCalculateIndex(bufferedIndex);
143
- }
144
- // Safety limits
145
- const maxIndex = Math.max(numItems - virtualizerLength, 0);
146
- const newStartIndex = Math.min(Math.max(bufferedIndex, 0), maxIndex);
147
- if (virtualizerStartIndex !== newStartIndex) {
148
- // We flush sync this and perform an immediate state update
149
- // due to virtualizerStartIndex invalidation.
150
- react_dom_1.flushSync(() => {
151
- batchUpdateNewIndex(newStartIndex);
152
- });
153
- }
154
- }, {
155
- root: intersectionObserverRoot ? intersectionObserverRoot === null || intersectionObserverRoot === void 0 ? void 0 : intersectionObserverRoot.current : null,
156
- rootMargin: '0px',
157
- threshold: 0
158
- });
159
- const findIndexRecursive = (scrollPos, lowIndex, highIndex) => {
160
- if (lowIndex > highIndex) {
161
- // We shouldn't get here - but no-op the index if we do.
162
- return virtualizerStartIndex;
163
- }
164
- const midpoint = Math.floor((lowIndex + highIndex) / 2);
165
- const iBefore = Math.max(midpoint - 1, 0);
166
- const iAfter = Math.min(midpoint + 1, childProgressiveSizes.current.length - 1);
167
- const indexValue = childProgressiveSizes.current[midpoint];
168
- const afterIndexValue = childProgressiveSizes.current[iAfter];
169
- const beforeIndexValue = childProgressiveSizes.current[iBefore];
170
- if (scrollPos <= afterIndexValue && scrollPos >= beforeIndexValue) {
171
- /* We've found our index - if we are exactly matching before/after index that's ok,
172
- better to reduce checks if it's right on the boundary. */
173
- return midpoint;
174
- }
175
- if (indexValue > scrollPos) {
176
- return findIndexRecursive(scrollPos, lowIndex, midpoint - 1);
177
- } else {
178
- return findIndexRecursive(scrollPos, midpoint + 1, highIndex);
179
- }
180
- };
181
- const getIndexFromSizeArray = scrollPos => {
182
- /* Quick searches our progressive height array */
183
- if (scrollPos === 0 || childProgressiveSizes.current.length === 0 || scrollPos <= childProgressiveSizes.current[0]) {
184
- // Check start
185
- return 0;
186
- }
187
- if (scrollPos >= childProgressiveSizes.current[childProgressiveSizes.current.length - 1]) {
188
- // Check end
189
- return childProgressiveSizes.current.length - 1;
190
- }
191
- return findIndexRecursive(scrollPos, 0, childProgressiveSizes.current.length - 1);
192
- };
193
- const getIndexFromScrollPosition = scrollPos => {
194
- if (!getItemSize) {
195
- return Math.round(scrollPos / itemSize);
196
- }
197
- return getIndexFromSizeArray(scrollPos);
198
- };
199
- const calculateTotalSize = () => {
200
- if (!getItemSize) {
201
- return itemSize * numItems;
202
- }
203
- // Time for custom size calcs
204
- return childProgressiveSizes.current[numItems - 1];
205
- };
206
- const calculateBefore = () => {
207
- if (!getItemSize) {
208
- // The missing items from before virtualization starts height
209
- return virtualizerStartIndex * itemSize;
210
- }
211
- if (virtualizerStartIndex <= 0) {
212
- return 0;
213
- }
214
- // Time for custom size calcs
215
- return childProgressiveSizes.current[virtualizerStartIndex - 1];
216
- };
217
- const calculateAfter = () => {
218
- if (numItems === 0) {
219
- return 0;
220
- }
221
- const lastItemIndex = Math.min(virtualizerStartIndex + virtualizerLength, numItems - 1);
222
- if (!getItemSize) {
223
- // The missing items from after virtualization ends height
224
- const remainingItems = numItems - lastItemIndex - 1;
225
- return remainingItems * itemSize;
226
- }
227
- // Time for custom size calcs
228
- return childProgressiveSizes.current[numItems - 1] - childProgressiveSizes.current[lastItemIndex];
229
- };
230
- const updateChildRows = newIndex => {
231
- if (numItems === 0) {
232
- /* Nothing to virtualize */
233
- return [];
234
- }
235
- if (childArray.current.length !== numItems) {
236
- childArray.current = new Array(virtualizerLength);
237
- }
238
- const actualIndex = Math.max(newIndex, 0);
239
- const end = Math.min(actualIndex + virtualizerLength, numItems);
240
- for (let i = actualIndex; i < end; i++) {
241
- childArray.current[i - actualIndex] = renderChild(i);
242
- }
243
- };
244
- const setBeforeRef = react_1.useCallback(element => {
245
- if (!element || beforeElementRef.current === element) {
246
- return;
247
- }
248
- beforeElementRef.current = element;
249
- const newList = [];
250
- newList.push(beforeElementRef.current);
251
- if (afterElementRef.current) {
252
- newList.push(afterElementRef.current);
253
- }
254
- // Ensure we update array if before element changed
255
- setObserverList(newList);
256
- }, [setObserverList]);
257
- const setAfterRef = react_1.useCallback(element => {
258
- if (!element || afterElementRef.current === element) {
259
- return;
260
- }
261
- afterElementRef.current = element;
262
- const newList = [];
263
- if (beforeElementRef.current) {
264
- newList.push(beforeElementRef.current);
265
- }
266
- newList.push(afterElementRef.current);
267
- // Ensure we update array if after element changed
268
- setObserverList(newList);
269
- }, [setObserverList]);
270
- const updateCurrentItemSizes = newIndex => {
271
- if (!getItemSize) {
272
- // Static sizes, not required.
273
- return;
274
- }
275
- // We should always call our size function on index change (only for the items that will be rendered)
276
- // This ensures we request the latest data for incoming items in case sizing has changed.
277
- const endIndex = Math.min(newIndex + virtualizerLength, numItems);
278
- const startIndex = Math.max(newIndex, 0);
279
- let didUpdate = false;
280
- for (let i = startIndex; i < endIndex; i++) {
281
- const newSize = getItemSize(i);
282
- if (newSize !== childSizes.current[i]) {
283
- childSizes.current[i] = newSize;
284
- didUpdate = true;
285
- }
286
- }
287
- if (didUpdate) {
288
- // Update our progressive size array
289
- for (let i = startIndex; i < numItems; i++) {
290
- const prevSize = i > 0 ? childProgressiveSizes.current[i - 1] : 0;
291
- childProgressiveSizes.current[i] = prevSize + childSizes.current[i];
292
- }
293
- }
294
- };
295
- // Initialize the size array before first render.
296
- const hasInitialized = react_1.useRef(false);
297
- const initializeSizeArray = () => {
298
- if (hasInitialized.current === false) {
299
- hasInitialized.current = true;
300
- populateSizeArrays();
301
- }
302
- };
303
- // Initialization on mount - update array index to 0 (ready state).
304
- // Only fire on mount (no deps).
305
- react_1.useEffect(() => {
306
- if (virtualizerStartIndex < 0) {
307
- batchUpdateNewIndex(0);
308
- }
391
+ const setBeforeRef = _react.useCallback((element)=>{
392
+ if (!element || beforeElementRef.current === element) {
393
+ return;
394
+ }
395
+ beforeElementRef.current = element;
396
+ const newList = [];
397
+ newList.push(beforeElementRef.current);
398
+ if (afterElementRef.current) {
399
+ newList.push(afterElementRef.current);
400
+ }
401
+ // Ensure we update array if before element changed
402
+ setObserverList(newList);
403
+ }, [
404
+ setObserverList
405
+ ]);
406
+ const setAfterRef = _react.useCallback((element)=>{
407
+ if (!element || afterElementRef.current === element) {
408
+ return;
409
+ }
410
+ afterElementRef.current = element;
411
+ const newList = [];
412
+ if (beforeElementRef.current) {
413
+ newList.push(beforeElementRef.current);
414
+ }
415
+ newList.push(afterElementRef.current);
416
+ // Ensure we update array if after element changed
417
+ setObserverList(newList);
418
+ }, [
419
+ setObserverList
420
+ ]);
421
+ // Initialize the size array before first render.
422
+ const hasInitialized = _react.useRef(false);
423
+ const initializeSizeArray = ()=>{
424
+ if (hasInitialized.current === false) {
425
+ hasInitialized.current = true;
426
+ populateSizeArrays();
427
+ }
428
+ };
429
+ _react.useImperativeHandle(imperativeVirtualizerRef, ()=>{
430
+ return {
431
+ progressiveSizes: childProgressiveSizes,
432
+ nodeSizes: childSizes,
433
+ setFlaggedIndex: (index)=>flaggedIndex.current = index,
434
+ currentIndex: actualIndexRef
435
+ };
436
+ }, [
437
+ childProgressiveSizes,
438
+ childSizes
439
+ ]);
440
+ // Initialization on mount - update array index to 0 (ready state).
441
+ // Only fire on mount (no deps).
442
+ _react.useEffect(()=>{
443
+ if (actualIndex < 0) {
444
+ batchUpdateNewIndex(0);
445
+ }
309
446
  // eslint-disable-next-line react-hooks/exhaustive-deps
310
- }, []);
311
- // If the user passes in an updated renderChild function - update current children
312
- react_1.useEffect(() => {
313
- if (virtualizerStartIndex >= 0) {
314
- updateChildRows(virtualizerStartIndex);
315
- forceUpdate();
316
- }
447
+ }, []);
448
+ /*
449
+ * forceUpdate:
450
+ * We only want to trigger this when child render or scroll loading changes,
451
+ * it will force re-render all children elements
452
+ */ const forceUpdate = _react.useReducer(()=>({}), {})[1];
453
+ // If the user passes in an updated renderChild function - update current children
454
+ _react.useEffect(()=>{
455
+ if (actualIndex >= 0) {
456
+ updateChildRows(actualIndex);
457
+ forceUpdate();
458
+ }
459
+ // eslint-disable-next-line react-hooks/exhaustive-deps
460
+ }, [
461
+ renderChild,
462
+ isScrolling
463
+ ]);
464
+ _react.useEffect(()=>{
465
+ // Ensure we repopulate if getItemSize callback changes
466
+ populateSizeArrays();
467
+ // We only run this effect on getItemSize change (recalc dynamic sizes)
317
468
  // eslint-disable-next-line react-hooks/exhaustive-deps
318
- }, [renderChild]);
319
- // Ensure we have run through and updated the whole size list array at least once.
320
- initializeSizeArray();
321
- if (getItemSize && (numItems !== childSizes.current.length || numItems !== childProgressiveSizes.current.length)) {
322
- // Child length mismatch, repopulate size arrays.
323
- populateSizeArrays();
324
- }
325
- const isFullyInitialized = hasInitialized.current && virtualizerStartIndex >= 0;
326
- return {
327
- components: {
328
- before: 'div',
329
- after: 'div',
330
- beforeContainer: 'div',
331
- afterContainer: 'div'
332
- },
333
- virtualizedChildren: childArray.current,
334
- before: react_utilities_1.resolveShorthand(props.before, {
335
- required: true,
336
- defaultProps: {
337
- ref: setBeforeRef,
338
- role: 'none'
339
- }
340
- }),
341
- after: react_utilities_1.resolveShorthand(props.after, {
342
- required: true,
343
- defaultProps: {
344
- ref: setAfterRef,
345
- role: 'none'
346
- }
347
- }),
348
- beforeContainer: react_utilities_1.resolveShorthand(props.beforeContainer, {
349
- required: true,
350
- defaultProps: {
351
- role: 'none'
352
- }
353
- }),
354
- afterContainer: react_utilities_1.resolveShorthand(props.afterContainer, {
355
- required: true,
356
- defaultProps: {
357
- role: 'none'
358
- }
359
- }),
360
- beforeBufferHeight: isFullyInitialized ? calculateBefore() : 0,
361
- afterBufferHeight: isFullyInitialized ? calculateAfter() : 0,
362
- totalVirtualizerHeight: isFullyInitialized ? calculateTotalSize() : virtualizerLength * itemSize,
363
- virtualizerStartIndex,
364
- axis,
365
- bufferSize,
366
- reversed
367
- };
469
+ }, [
470
+ getItemSize,
471
+ gap
472
+ ]);
473
+ // Effect to check flag index on updates
474
+ _react.useEffect(()=>{
475
+ if (!onRenderedFlaggedIndex || flaggedIndex.current === null) {
476
+ return;
477
+ }
478
+ if (actualIndex <= flaggedIndex.current && actualIndex + virtualizerLength >= flaggedIndex.current) {
479
+ onRenderedFlaggedIndex(flaggedIndex.current);
480
+ flaggedIndex.current = null;
481
+ }
482
+ }, [
483
+ actualIndex,
484
+ onRenderedFlaggedIndex,
485
+ virtualizerLength
486
+ ]);
487
+ // Ensure we have run through and updated the whole size list array at least once.
488
+ initializeSizeArray();
489
+ if (getItemSize && (numItems !== childSizes.current.length || numItems !== childProgressiveSizes.current.length)) {
490
+ // Child length mismatch, repopulate size arrays.
491
+ populateSizeArrays();
492
+ }
493
+ // Ensure we recalc if virtualizer length changes
494
+ const maxCompare = Math.min(virtualizerLength, numItems);
495
+ if (childArray.current.length !== maxCompare && actualIndex + childArray.current.length < numItems) {
496
+ updateChildRows(actualIndex);
497
+ }
498
+ const isFullyInitialized = hasInitialized.current && actualIndex >= 0;
499
+ return {
500
+ components: {
501
+ before: 'div',
502
+ after: 'div',
503
+ beforeContainer: 'div',
504
+ afterContainer: 'div'
505
+ },
506
+ virtualizedChildren: childArray.current,
507
+ before: _reactutilities.slot.always(props.before, {
508
+ defaultProps: {
509
+ ref: setBeforeRef,
510
+ role: 'none'
511
+ },
512
+ elementType: 'div'
513
+ }),
514
+ after: _reactutilities.slot.always(props.after, {
515
+ defaultProps: {
516
+ ref: setAfterRef,
517
+ role: 'none'
518
+ },
519
+ elementType: 'div'
520
+ }),
521
+ beforeContainer: _reactutilities.slot.always(props.beforeContainer, {
522
+ defaultProps: {
523
+ role: 'none'
524
+ },
525
+ elementType: 'div'
526
+ }),
527
+ afterContainer: _reactutilities.slot.always(props.afterContainer, {
528
+ defaultProps: {
529
+ role: 'none'
530
+ },
531
+ elementType: 'div'
532
+ }),
533
+ beforeBufferHeight: isFullyInitialized ? calculateBefore() : 0,
534
+ afterBufferHeight: isFullyInitialized ? calculateAfter() : 0,
535
+ totalVirtualizerHeight: isFullyInitialized ? calculateTotalSize() : virtualizerLength * itemSize,
536
+ virtualizerStartIndex: actualIndex,
537
+ axis,
538
+ bufferSize,
539
+ reversed,
540
+ childSizes,
541
+ childProgressiveSizes
542
+ };
368
543
  }
369
- exports.useVirtualizer_unstable = useVirtualizer_unstable;
370
- //# sourceMappingURL=useVirtualizer.js.map