@fluentui/react-virtualizer 9.0.0-alpha.9 → 9.0.0-alpha.91

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