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

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