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