@fluentui/react-virtualizer 9.0.0-alpha.1 → 9.0.0-alpha.100

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