@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
@@ -0,0 +1,129 @@
1
+ 'use client';
2
+ import { useIsomorphicLayoutEffect, useMergedRefs } from '@fluentui/react-utilities';
3
+ import * as React from 'react';
4
+ import { useResizeObserverRef_unstable } from './useResizeObserverRef';
5
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
6
+ /**
7
+ * React hook that measures virtualized space dynamically to ensure optimized virtualization length.
8
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
9
+ */ export const useDynamicVirtualizerMeasure = (virtualizerProps)=>{
10
+ const { defaultItemSize, direction = 'vertical', numItems, getItemSize, bufferItems, bufferSize, virtualizerContext } = virtualizerProps;
11
+ const [state, setState] = React.useState({
12
+ virtualizerLength: 0,
13
+ virtualizerBufferItems: 0,
14
+ virtualizerBufferSize: 0
15
+ });
16
+ const containerSizeRef = React.useRef(0);
17
+ const scrollPosition = React.useRef(0);
18
+ const { virtualizerLength, virtualizerBufferItems, virtualizerBufferSize } = state;
19
+ const { targetDocument } = useFluent();
20
+ const container = React.useRef(null);
21
+ const handleScrollResize = React.useCallback(// eslint-disable-next-line @typescript-eslint/no-deprecated
22
+ (scrollRef)=>{
23
+ if (!(scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current)) {
24
+ // Error? ignore?
25
+ return;
26
+ }
27
+ if (scrollRef.current !== (targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.body)) {
28
+ // We have a local scroll container
29
+ containerSizeRef.current = direction === 'vertical' ? scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current.getBoundingClientRect().height : scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current.getBoundingClientRect().width;
30
+ } else if (targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView) {
31
+ var _targetDocument_defaultView, _targetDocument_defaultView1;
32
+ // If our scroll ref is the document body, we should check window height
33
+ containerSizeRef.current = direction === 'vertical' ? targetDocument === null || targetDocument === void 0 ? void 0 : (_targetDocument_defaultView = targetDocument.defaultView) === null || _targetDocument_defaultView === void 0 ? void 0 : _targetDocument_defaultView.innerHeight : targetDocument === null || targetDocument === void 0 ? void 0 : (_targetDocument_defaultView1 = targetDocument.defaultView) === null || _targetDocument_defaultView1 === void 0 ? void 0 : _targetDocument_defaultView1.innerWidth;
34
+ }
35
+ let indexSizer = 0;
36
+ let i = 0;
37
+ let length = 0;
38
+ const startIndex = virtualizerContext.contextIndex;
39
+ const sizeToBeat = containerSizeRef.current + virtualizerBufferSize * 2;
40
+ while(indexSizer <= sizeToBeat && i + startIndex < numItems){
41
+ const iItemSize = getItemSize(startIndex + i);
42
+ if (virtualizerContext.childProgressiveSizes.current.length < numItems) {
43
+ /* We are in unknown territory, either an initial render or an update
44
+ in virtualizer item length has occurred.
45
+ We need to let the new items render first then we can accurately assess.*/ return virtualizerLength - virtualizerBufferSize * 2;
46
+ }
47
+ const currentScrollPos = scrollPosition.current;
48
+ const currentItemPos = virtualizerContext.childProgressiveSizes.current[startIndex + i] - iItemSize;
49
+ if (currentScrollPos > currentItemPos + iItemSize) {
50
+ // The item isn't in view, ignore for now.
51
+ i++;
52
+ continue;
53
+ } else if (currentScrollPos > currentItemPos) {
54
+ // The item is partially out of view, ignore the out of bounds portion
55
+ const variance = currentItemPos + iItemSize - currentScrollPos;
56
+ indexSizer += variance;
57
+ } else {
58
+ // Item is in view
59
+ indexSizer += iItemSize;
60
+ }
61
+ // Increment
62
+ i++;
63
+ length++;
64
+ }
65
+ /*
66
+ * Number of items to append at each end, i.e. 'preload' each side before entering view.
67
+ * Minimum: 2 - we give slightly more buffer for dynamic version.
68
+ */ const newBufferItems = bufferItems !== null && bufferItems !== void 0 ? bufferItems : Math.max(Math.ceil(length / 3), 1);
69
+ /*
70
+ * This is how far we deviate into the bufferItems to detect a redraw.
71
+ */ const newBufferSize = bufferSize !== null && bufferSize !== void 0 ? bufferSize : Math.max(defaultItemSize / 2, 1);
72
+ const totalLength = length + newBufferItems * 2;
73
+ setState({
74
+ virtualizerLength: totalLength,
75
+ virtualizerBufferSize: newBufferSize,
76
+ virtualizerBufferItems: newBufferItems
77
+ });
78
+ }, [
79
+ bufferItems,
80
+ bufferSize,
81
+ defaultItemSize,
82
+ direction,
83
+ getItemSize,
84
+ numItems,
85
+ targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.body,
86
+ targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView,
87
+ virtualizerBufferSize,
88
+ virtualizerContext.childProgressiveSizes,
89
+ virtualizerContext.contextIndex,
90
+ virtualizerLength
91
+ ]);
92
+ const resizeCallback = React.useCallback((_entries, // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
93
+ _observer, // eslint-disable-next-line @typescript-eslint/no-deprecated
94
+ scrollRef)=>{
95
+ if (scrollRef) {
96
+ handleScrollResize(scrollRef);
97
+ }
98
+ }, [
99
+ handleScrollResize
100
+ ]);
101
+ const scrollRef = useMergedRefs(container, useResizeObserverRef_unstable(resizeCallback));
102
+ useIsomorphicLayoutEffect(()=>{
103
+ if (virtualizerContext.contextIndex + virtualizerLength < numItems) {
104
+ // Avoid re-rendering/re-calculating when the end index has already been reached
105
+ handleScrollResize(container);
106
+ }
107
+ }, [
108
+ handleScrollResize,
109
+ numItems,
110
+ virtualizerContext.contextIndex,
111
+ virtualizerLength
112
+ ]);
113
+ const updateScrollPosition = React.useCallback((_scrollPosition)=>{
114
+ scrollPosition.current = _scrollPosition;
115
+ // Check if our vLength's need recalculating
116
+ handleScrollResize(scrollRef);
117
+ }, [
118
+ handleScrollResize,
119
+ scrollRef
120
+ ]);
121
+ return {
122
+ virtualizerLength,
123
+ bufferItems: virtualizerBufferItems,
124
+ bufferSize: virtualizerBufferSize,
125
+ scrollRef,
126
+ containerSizeRef,
127
+ updateScrollPosition
128
+ };
129
+ };
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/useDynamicVirtualizerMeasure.ts"],"sourcesContent":["'use client';\n\nimport { useIsomorphicLayoutEffect, useMergedRefs } from '@fluentui/react-utilities';\nimport * as React from 'react';\nimport { VirtualizerMeasureDynamicProps } from './hooks.types';\nimport { useResizeObserverRef_unstable } from './useResizeObserverRef';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\n\n/**\n * React hook that measures virtualized space dynamically to ensure optimized virtualization length.\n * @deprecated migrated to \\@fluentui\\-contrib/react\\-virtualizer for stable release.\n */\nexport const useDynamicVirtualizerMeasure = <TElement extends HTMLElement>(\n virtualizerProps: VirtualizerMeasureDynamicProps,\n): {\n virtualizerLength: number;\n bufferItems: number;\n bufferSize: number;\n scrollRef: (instance: TElement | null) => void;\n containerSizeRef: React.RefObject<number>;\n updateScrollPosition: (scrollPosition: number) => void;\n} => {\n const {\n defaultItemSize,\n direction = 'vertical',\n numItems,\n getItemSize,\n bufferItems,\n bufferSize,\n virtualizerContext,\n } = virtualizerProps;\n\n const [state, setState] = React.useState({\n virtualizerLength: 0,\n virtualizerBufferItems: 0,\n virtualizerBufferSize: 0,\n });\n\n const containerSizeRef = React.useRef<number>(0);\n const scrollPosition = React.useRef<number>(0);\n const { virtualizerLength, virtualizerBufferItems, virtualizerBufferSize } = state;\n\n const { targetDocument } = useFluent();\n const container = React.useRef<HTMLElement | null>(null);\n const handleScrollResize = React.useCallback(\n // eslint-disable-next-line @typescript-eslint/no-deprecated\n (scrollRef: React.MutableRefObject<HTMLElement | null>) => {\n if (!scrollRef?.current) {\n // Error? ignore?\n return;\n }\n\n if (scrollRef.current !== targetDocument?.body) {\n // We have a local scroll container\n containerSizeRef.current =\n direction === 'vertical'\n ? scrollRef?.current.getBoundingClientRect().height\n : scrollRef?.current.getBoundingClientRect().width;\n } else if (targetDocument?.defaultView) {\n // If our scroll ref is the document body, we should check window height\n containerSizeRef.current =\n direction === 'vertical' ? targetDocument?.defaultView?.innerHeight : targetDocument?.defaultView?.innerWidth;\n }\n\n let indexSizer = 0;\n let i = 0;\n let length = 0;\n\n const startIndex = virtualizerContext.contextIndex;\n const sizeToBeat = containerSizeRef.current + virtualizerBufferSize * 2;\n\n while (indexSizer <= sizeToBeat && i + startIndex < numItems) {\n const iItemSize = getItemSize(startIndex + i);\n if (virtualizerContext.childProgressiveSizes.current.length < numItems) {\n /* We are in unknown territory, either an initial render or an update\n in virtualizer item length has occurred.\n We need to let the new items render first then we can accurately assess.*/\n return virtualizerLength - virtualizerBufferSize * 2;\n }\n\n const currentScrollPos = scrollPosition.current;\n const currentItemPos = virtualizerContext.childProgressiveSizes.current[startIndex + i] - iItemSize;\n\n if (currentScrollPos > currentItemPos + iItemSize) {\n // The item isn't in view, ignore for now.\n i++;\n continue;\n } else if (currentScrollPos > currentItemPos) {\n // The item is partially out of view, ignore the out of bounds portion\n const variance = currentItemPos + iItemSize - currentScrollPos;\n indexSizer += variance;\n } else {\n // Item is in view\n indexSizer += iItemSize;\n }\n // Increment\n i++;\n length++;\n }\n\n /*\n * Number of items to append at each end, i.e. 'preload' each side before entering view.\n * Minimum: 2 - we give slightly more buffer for dynamic version.\n */\n const newBufferItems = bufferItems ?? Math.max(Math.ceil(length / 3), 1);\n\n /*\n * This is how far we deviate into the bufferItems to detect a redraw.\n */\n const newBufferSize = bufferSize ?? Math.max(defaultItemSize / 2, 1);\n const totalLength = length + newBufferItems * 2;\n\n setState({\n virtualizerLength: totalLength,\n virtualizerBufferSize: newBufferSize,\n virtualizerBufferItems: newBufferItems,\n });\n },\n [\n bufferItems,\n bufferSize,\n defaultItemSize,\n direction,\n getItemSize,\n numItems,\n targetDocument?.body,\n targetDocument?.defaultView,\n virtualizerBufferSize,\n virtualizerContext.childProgressiveSizes,\n virtualizerContext.contextIndex,\n virtualizerLength,\n ],\n );\n\n const resizeCallback = React.useCallback(\n (\n _entries: ResizeObserverEntry[],\n // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286\n\n _observer: ResizeObserver,\n // eslint-disable-next-line @typescript-eslint/no-deprecated\n scrollRef?: React.MutableRefObject<HTMLElement | null>,\n ) => {\n if (scrollRef) {\n handleScrollResize(scrollRef);\n }\n },\n [handleScrollResize],\n );\n\n const scrollRef = useMergedRefs(container, useResizeObserverRef_unstable(resizeCallback));\n\n useIsomorphicLayoutEffect(() => {\n if (virtualizerContext.contextIndex + virtualizerLength < numItems) {\n // Avoid re-rendering/re-calculating when the end index has already been reached\n handleScrollResize(container);\n }\n }, [handleScrollResize, numItems, virtualizerContext.contextIndex, virtualizerLength]);\n\n const updateScrollPosition = React.useCallback(\n (_scrollPosition: number) => {\n scrollPosition.current = _scrollPosition;\n // Check if our vLength's need recalculating\n handleScrollResize(scrollRef);\n },\n [handleScrollResize, scrollRef],\n );\n\n return {\n virtualizerLength,\n bufferItems: virtualizerBufferItems,\n bufferSize: virtualizerBufferSize,\n scrollRef,\n containerSizeRef,\n updateScrollPosition,\n };\n};\n"],"names":["useIsomorphicLayoutEffect","useMergedRefs","React","useResizeObserverRef_unstable","useFluent_unstable","useFluent","useDynamicVirtualizerMeasure","virtualizerProps","defaultItemSize","direction","numItems","getItemSize","bufferItems","bufferSize","virtualizerContext","state","setState","useState","virtualizerLength","virtualizerBufferItems","virtualizerBufferSize","containerSizeRef","useRef","scrollPosition","targetDocument","container","handleScrollResize","useCallback","scrollRef","current","body","getBoundingClientRect","height","width","defaultView","innerHeight","innerWidth","indexSizer","i","length","startIndex","contextIndex","sizeToBeat","iItemSize","childProgressiveSizes","currentScrollPos","currentItemPos","variance","newBufferItems","Math","max","ceil","newBufferSize","totalLength","resizeCallback","_entries","_observer","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C;YACvBjC,wBAAwB6B;QAC1B;IACF,GACA;QACEpC;QACAC;QACAL;QACAC;QACAE;QACAD;QACAc,2BAAAA,qCAAAA,eAAgBM,IAAI;QACpBN,2BAAAA,qCAAAA,eAAgBU,WAAW;QAC3Bd;QACAN,mBAAmB8B,qBAAqB;QACxC9B,mBAAmB2B,YAAY;QAC/BvB;KACD;IAGH,MAAMoC,iBAAiBpD,MAAMyB,WAAW,CACtC,CACE4B,UACA,8FAA8F;IAE9FC,WACA,4DAA4D;IAC5D5B;QAEA,IAAIA,WAAW;YACbF,mBAAmBE;QACrB;IACF,GACA;QAACF;KAAmB;IAGtB,MAAME,YAAY3B,cAAcwB,WAAWtB,8BAA8BmD;IAEzEtD,0BAA0B;QACxB,IAAIc,mBAAmB2B,YAAY,GAAGvB,oBAAoBR,UAAU;YAClE,gFAAgF;YAChFgB,mBAAmBD;QACrB;IACF,GAAG;QAACC;QAAoBhB;QAAUI,mBAAmB2B,YAAY;QAAEvB;KAAkB;IAErF,MAAMuC,uBAAuBvD,MAAMyB,WAAW,CAC5C,CAAC+B;QACCnC,eAAeM,OAAO,GAAG6B;QACzB,4CAA4C;QAC5ChC,mBAAmBE;IACrB,GACA;QAACF;QAAoBE;KAAU;IAGjC,OAAO;QACLV;QACAN,aAAaO;QACbN,YAAYO;QACZQ;QACAP;QACAoC;IACF;AACF,EAAE"}
@@ -1,9 +1,34 @@
1
+ 'use client';
1
2
  import * as React from 'react';
2
3
  import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
3
- const {
4
- useState,
5
- useRef
6
- } = React;
4
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
5
+ const { useCallback, useState, useRef } = React;
6
+ import { useMutationObserver } from './useMutationObserver';
7
+ /**
8
+ * This function will take the rootMargin and flip the sides if we are in RTL based on the computed reading direction of the target element.
9
+ * @param ltrRootMargin the margin to be processed and flipped if required
10
+ * @param target target element that will have its current reading direction determined
11
+ * @returns the corrected rootMargin (if it was necessary to correct)
12
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
13
+ */ export const getRTLRootMargin = (ltrRootMargin, target, win)=>{
14
+ if (target && win) {
15
+ // get the computed dir for the target element
16
+ const newDir = win.getComputedStyle(target).direction;
17
+ // If we're in rtl reading direction, we might need to flip the margins on the left/right sides
18
+ if (newDir === 'rtl') {
19
+ let newMargin = ltrRootMargin;
20
+ const splitMargins = ltrRootMargin.split(' ');
21
+ // We only need to do this if we get four values, otherwise the sides are equal and don't require flipping.
22
+ if (splitMargins.length === 4) {
23
+ newMargin = `${splitMargins[0]} ${splitMargins[3]} ${splitMargins[2]} ${splitMargins[1]}`;
24
+ }
25
+ return newMargin;
26
+ } else {
27
+ return ltrRootMargin;
28
+ }
29
+ }
30
+ return ltrRootMargin;
31
+ };
7
32
  /**
8
33
  * React hook that allows easy usage of the browser API IntersectionObserver within React
9
34
  * @param callback - A function called when the percentage of the target element is visible crosses a threshold.
@@ -12,41 +37,95 @@ const {
12
37
  * enough to trigger a callback).
13
38
  * @returns An array containing a callback to update the list of Elements the observer should listen to, a callback to
14
39
  * update the init options of the IntersectionObserver and a ref to the IntersectionObserver instance itself.
15
- */
16
- export const useIntersectionObserver = (callback, options) => {
17
- // export const useIntersectionObserver = (
18
- // callback: IntersectionObserverCallback,
19
- // options?: IntersectionObserverInit,
20
- // ): [
21
- // Dispatch<SetStateAction<Element[] | undefined>>,
22
- // Dispatch<SetStateAction<IntersectionObserverInit | undefined>>,
23
- // MutableRefObject<IntersectionObserver | undefined>,
24
- // ] => {
25
- const observer = useRef();
26
- const [observerList, setObserverList] = useState();
27
- const [observerInit, setObserverInit] = useState(options);
28
- // Observer elements in passed in list and clean up previous list
29
- // This effect is only triggered when observerList is updated
30
- useIsomorphicLayoutEffect(() => {
31
- observer.current = new IntersectionObserver(callback, observerInit);
32
- // If we have an instance of IO and a list with elements, observer the elements
33
- if (observer.current && observerList && observerList.length > 0) {
34
- observerList.forEach(element => {
35
- var _a;
36
- (_a = observer.current) === null || _a === void 0 ? void 0 : _a.observe(element);
37
- });
38
- }
39
- // clean up previous elements being listened to
40
- return () => {
41
- if (observer.current) {
42
- observer.current.disconnect();
43
- }
40
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
41
+ */ export const useIntersectionObserver = (callback, options)=>{
42
+ 'use no memo';
43
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
44
+ const observer = useRef(undefined);
45
+ const [observerList, setObserverList] = useState();
46
+ const { targetDocument } = useFluent();
47
+ const win = targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView;
48
+ var _options_rootMargin;
49
+ // set the initial init with corrected margins based on the observed root's calculated reading direction.
50
+ const [observerInit, setObserverInit] = useState(options && {
51
+ ...options,
52
+ rootMargin: getRTLRootMargin((_options_rootMargin = options.rootMargin) !== null && _options_rootMargin !== void 0 ? _options_rootMargin : '0px', options.root, win)
53
+ });
54
+ var _options_rootMargin1;
55
+ // We have to assume that any values passed in for rootMargin by the consuming app are ltr values. As such we will store the ltr value.
56
+ const ltrRootMargin = useRef((_options_rootMargin1 = options === null || options === void 0 ? void 0 : options.rootMargin) !== null && _options_rootMargin1 !== void 0 ? _options_rootMargin1 : '0px');
57
+ // Callback function to execute when mutations are observed
58
+ const mutationObserverCallback = useCallback((mutationList)=>{
59
+ for (const mutation of mutationList){
60
+ // Ensuring that the right attribute is being observed and that the root is within the tree of the element being mutated.
61
+ if (mutation.type === 'attributes' && mutation.attributeName === 'dir' && (options === null || options === void 0 ? void 0 : options.root) && mutation.target.contains(options === null || options === void 0 ? void 0 : options.root)) {
62
+ setObserverInit({
63
+ ...observerInit,
64
+ rootMargin: getRTLRootMargin(ltrRootMargin.current, observerInit === null || observerInit === void 0 ? void 0 : observerInit.root, win)
65
+ });
66
+ }
67
+ }
68
+ }, [
69
+ ltrRootMargin,
70
+ observerInit,
71
+ options === null || options === void 0 ? void 0 : options.root,
72
+ win
73
+ ]);
74
+ // Mutation observer for dir attribute changes in the document
75
+ useMutationObserver(targetDocument, mutationObserverCallback, {
76
+ attributes: true,
77
+ subtree: true,
78
+ attributeFilter: [
79
+ 'dir'
80
+ ]
81
+ });
82
+ // Observer elements in passed in list and clean up previous list
83
+ // This effect is only triggered when observerList is updated
84
+ useIsomorphicLayoutEffect(()=>{
85
+ if (!win) {
86
+ return;
87
+ }
88
+ observer.current = new win.IntersectionObserver(callback, {
89
+ ...observerInit,
90
+ rootMargin: getRTLRootMargin(ltrRootMargin.current, observerInit === null || observerInit === void 0 ? void 0 : observerInit.root, win)
91
+ });
92
+ // If we have an instance of IO and a list with elements, observer the elements
93
+ if (observer.current && observerList && observerList.length > 0) {
94
+ observerList.forEach((element)=>{
95
+ var _observer_current;
96
+ (_observer_current = observer.current) === null || _observer_current === void 0 ? void 0 : _observer_current.observe(element);
97
+ });
98
+ }
99
+ // clean up previous elements being listened to
100
+ return ()=>{
101
+ if (observer.current) {
102
+ observer.current.disconnect();
103
+ }
104
+ };
105
+ }, [
106
+ observerList,
107
+ observerInit,
108
+ callback,
109
+ win
110
+ ]);
111
+ // Do not use internally, we need to track external settings only here
112
+ const setObserverInitExternal = useCallback((newInit)=>{
113
+ var _newInit_rootMargin;
114
+ // Since we know this is coming from consumers, we can store this value as LTR somewhat safely.
115
+ ltrRootMargin.current = (_newInit_rootMargin = newInit === null || newInit === void 0 ? void 0 : newInit.rootMargin) !== null && _newInit_rootMargin !== void 0 ? _newInit_rootMargin : '0px';
116
+ // Call the internal setter to update the value and ensure if our calculated direction is rtl, we flip the margin
117
+ setObserverInit({
118
+ ...newInit,
119
+ rootMargin: getRTLRootMargin(ltrRootMargin.current, newInit === null || newInit === void 0 ? void 0 : newInit.root, win)
120
+ });
121
+ }, [
122
+ ltrRootMargin,
123
+ setObserverInit,
124
+ win
125
+ ]);
126
+ return {
127
+ setObserverList,
128
+ setObserverInit: setObserverInitExternal,
129
+ observer
44
130
  };
45
- }, [observerList, observerInit, callback]);
46
- return {
47
- setObserverList,
48
- setObserverInit,
49
- observer
50
- };
51
131
  };
52
- //# sourceMappingURL=useIntersectionObserver.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["React","useIsomorphicLayoutEffect","useState","useRef","useIntersectionObserver","callback","options","observer","observerList","setObserverList","observerInit","setObserverInit","current","IntersectionObserver","length","forEach","element","_a","observe","disconnect"],"sources":["../src/packages/react-components/react-virtualizer/src/hooks/useIntersectionObserver.ts"],"sourcesContent":["import type { Dispatch, MutableRefObject, SetStateAction } from 'react';\nimport * as React from 'react';\nimport { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\n\nconst { useState, useRef } = React;\n\n/**\n * React hook that allows easy usage of the browser API IntersectionObserver within React\n * @param callback - A function called when the percentage of the target element is visible crosses a threshold.\n * @param options - An optional object which customizes the observer. If options isn't specified, the observer uses the\n * document's viewport as the root, with no margin, and a 0% threshold (meaning that even a one-pixel change is\n * enough to trigger a callback).\n * @returns An array containing a callback to update the list of Elements the observer should listen to, a callback to\n * update the init options of the IntersectionObserver and a ref to the IntersectionObserver instance itself.\n */\n\nexport const useIntersectionObserver = (\n callback: IntersectionObserverCallback,\n options?: IntersectionObserverInit,\n): {\n setObserverList: Dispatch<SetStateAction<Element[] | undefined>>;\n setObserverInit: Dispatch<SetStateAction<IntersectionObserverInit | undefined>>;\n observer: MutableRefObject<IntersectionObserver | undefined>;\n} => {\n // export const useIntersectionObserver = (\n // callback: IntersectionObserverCallback,\n // options?: IntersectionObserverInit,\n // ): [\n // Dispatch<SetStateAction<Element[] | undefined>>,\n // Dispatch<SetStateAction<IntersectionObserverInit | undefined>>,\n // MutableRefObject<IntersectionObserver | undefined>,\n // ] => {\n const observer = useRef<IntersectionObserver>();\n const [observerList, setObserverList] = useState<Element[]>();\n const [observerInit, setObserverInit] = useState<IntersectionObserverInit | undefined>(options);\n\n // Observer elements in passed in list and clean up previous list\n // This effect is only triggered when observerList is updated\n useIsomorphicLayoutEffect(() => {\n observer.current = new IntersectionObserver(callback, observerInit);\n\n // If we have an instance of IO and a list with elements, observer the elements\n if (observer.current && observerList && observerList.length > 0) {\n observerList.forEach(element => {\n observer.current?.observe(element);\n });\n }\n\n // clean up previous elements being listened to\n return () => {\n if (observer.current) {\n observer.current.disconnect();\n }\n };\n }, [observerList, observerInit, callback]);\n\n return { setObserverList, setObserverInit, observer };\n};\n"],"mappings":"AACA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAC9B,SAASC,yBAAyB,QAAQ,2BAA2B;AAErE,MAAM;EAAEC,QAAQ;EAAEC;AAAM,CAAE,GAAGH,KAAK;AAElC;;;;;;;;;AAUA,OAAO,MAAMI,uBAAuB,GAAGA,CACrCC,QAAsC,EACtCC,OAAkC,KAKhC;EACF;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,MAAMC,QAAQ,GAAGJ,MAAM,EAAwB;EAC/C,MAAM,CAACK,YAAY,EAAEC,eAAe,CAAC,GAAGP,QAAQ,EAAa;EAC7D,MAAM,CAACQ,YAAY,EAAEC,eAAe,CAAC,GAAGT,QAAQ,CAAuCI,OAAO,CAAC;EAE/F;EACA;EACAL,yBAAyB,CAAC,MAAK;IAC7BM,QAAQ,CAACK,OAAO,GAAG,IAAIC,oBAAoB,CAACR,QAAQ,EAAEK,YAAY,CAAC;IAEnE;IACA,IAAIH,QAAQ,CAACK,OAAO,IAAIJ,YAAY,IAAIA,YAAY,CAACM,MAAM,GAAG,CAAC,EAAE;MAC/DN,YAAY,CAACO,OAAO,CAACC,OAAO,IAAG;;QAC7B,CAAAC,EAAA,GAAAV,QAAQ,CAACK,OAAO,cAAAK,EAAA,uBAAAA,EAAA,CAAEC,OAAO,CAACF,OAAO,CAAC;MACpC,CAAC,CAAC;;IAGJ;IACA,OAAO,MAAK;MACV,IAAIT,QAAQ,CAACK,OAAO,EAAE;QACpBL,QAAQ,CAACK,OAAO,CAACO,UAAU,EAAE;;IAEjC,CAAC;EACH,CAAC,EAAE,CAACX,YAAY,EAAEE,YAAY,EAAEL,QAAQ,CAAC,CAAC;EAE1C,OAAO;IAAEI,eAAe;IAAEE,eAAe;IAAEJ;EAAQ,CAAE;AACvD,CAAC"}
1
+ {"version":3,"sources":["../src/hooks/useIntersectionObserver.ts"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\n\nconst { useCallback, useState, useRef } = React;\nimport { useMutationObserver } from './useMutationObserver';\n\n/**\n * This function will take the rootMargin and flip the sides if we are in RTL based on the computed reading direction of the target element.\n * @param ltrRootMargin the margin to be processed and flipped if required\n * @param target target element that will have its current reading direction determined\n * @returns the corrected rootMargin (if it was necessary to correct)\n * @deprecated migrated to \\@fluentui\\-contrib/react\\-virtualizer for stable release.\n */\nexport const getRTLRootMargin = (\n ltrRootMargin: string,\n target?: Element | Document | null | undefined,\n win?: Window | null,\n): string => {\n if (target && win) {\n // get the computed dir for the target element\n const newDir = win.getComputedStyle(target as Element).direction;\n\n // If we're in rtl reading direction, we might need to flip the margins on the left/right sides\n if (newDir === 'rtl') {\n let newMargin = ltrRootMargin;\n const splitMargins = ltrRootMargin.split(' ');\n\n // We only need to do this if we get four values, otherwise the sides are equal and don't require flipping.\n if (splitMargins.length === 4) {\n newMargin = `${splitMargins[0]} ${splitMargins[3]} ${splitMargins[2]} ${splitMargins[1]}`;\n }\n\n return newMargin;\n } else {\n return ltrRootMargin;\n }\n }\n\n return ltrRootMargin;\n};\n\n/**\n * React hook that allows easy usage of the browser API IntersectionObserver within React\n * @param callback - A function called when the percentage of the target element is visible crosses a threshold.\n * @param options - An optional object which customizes the observer. If options isn't specified, the observer uses the\n * document's viewport as the root, with no margin, and a 0% threshold (meaning that even a one-pixel change is\n * enough to trigger a callback).\n * @returns An array containing a callback to update the list of Elements the observer should listen to, a callback to\n * update the init options of the IntersectionObserver and a ref to the IntersectionObserver instance itself.\n * @deprecated migrated to \\@fluentui\\-contrib/react\\-virtualizer for stable release.\n */\nexport const useIntersectionObserver = (\n callback: IntersectionObserverCallback,\n options?: IntersectionObserverInit,\n): {\n setObserverList: React.Dispatch<React.SetStateAction<Element[] | undefined>>;\n setObserverInit: (newInit: IntersectionObserverInit | undefined) => void;\n // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286\n\n // eslint-disable-next-line @typescript-eslint/no-deprecated\n observer: React.MutableRefObject<IntersectionObserver | undefined>;\n} => {\n 'use no memo';\n\n // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286\n\n const observer = useRef<IntersectionObserver>(undefined);\n const [observerList, setObserverList] = useState<Element[]>();\n const { targetDocument } = useFluent();\n const win = targetDocument?.defaultView;\n\n // set the initial init with corrected margins based on the observed root's calculated reading direction.\n const [observerInit, setObserverInit] = useState<IntersectionObserverInit | undefined>(\n options && {\n ...options,\n rootMargin: getRTLRootMargin(options.rootMargin ?? '0px', options.root as Element, win),\n },\n );\n\n // We have to assume that any values passed in for rootMargin by the consuming app are ltr values. As such we will store the ltr value.\n const ltrRootMargin = useRef<string>(options?.rootMargin ?? '0px');\n\n // Callback function to execute when mutations are observed\n const mutationObserverCallback: MutationCallback = useCallback(\n mutationList => {\n for (const mutation of mutationList) {\n // Ensuring that the right attribute is being observed and that the root is within the tree of the element being mutated.\n if (\n mutation.type === 'attributes' &&\n mutation.attributeName === 'dir' &&\n options?.root &&\n mutation.target.contains(options?.root)\n ) {\n setObserverInit({\n ...observerInit,\n rootMargin: getRTLRootMargin(ltrRootMargin.current, observerInit?.root, win),\n });\n }\n }\n },\n [ltrRootMargin, observerInit, options?.root, win],\n );\n\n // Mutation observer for dir attribute changes in the document\n useMutationObserver(targetDocument, mutationObserverCallback, {\n attributes: true,\n subtree: true,\n attributeFilter: ['dir'],\n });\n\n // Observer elements in passed in list and clean up previous list\n // This effect is only triggered when observerList is updated\n useIsomorphicLayoutEffect(() => {\n if (!win) {\n return;\n }\n\n observer.current = new win.IntersectionObserver(callback, {\n ...observerInit,\n rootMargin: getRTLRootMargin(ltrRootMargin.current, observerInit?.root, win),\n });\n\n // If we have an instance of IO and a list with elements, observer the elements\n if (observer.current && observerList && observerList.length > 0) {\n observerList.forEach(element => {\n observer.current?.observe(element);\n });\n }\n\n // clean up previous elements being listened to\n return () => {\n if (observer.current) {\n observer.current.disconnect();\n }\n };\n }, [observerList, observerInit, callback, win]);\n\n // Do not use internally, we need to track external settings only here\n const setObserverInitExternal = useCallback(\n (newInit: IntersectionObserverInit | undefined) => {\n // Since we know this is coming from consumers, we can store this value as LTR somewhat safely.\n ltrRootMargin.current = newInit?.rootMargin ?? '0px';\n\n // Call the internal setter to update the value and ensure if our calculated direction is rtl, we flip the margin\n setObserverInit({\n ...newInit,\n rootMargin: getRTLRootMargin(ltrRootMargin.current, newInit?.root as Element, win),\n });\n },\n [ltrRootMargin, setObserverInit, win],\n );\n\n return { setObserverList, setObserverInit: setObserverInitExternal, observer };\n};\n"],"names":["React","useIsomorphicLayoutEffect","useFluent_unstable","useFluent","useCallback","useState","useRef","useMutationObserver","getRTLRootMargin","ltrRootMargin","target","win","newDir","getComputedStyle","direction","newMargin","splitMargins","split","length","useIntersectionObserver","callback","options","observer","undefined","observerList","setObserverList","targetDocument","defaultView","observerInit","setObserverInit","rootMargin","root","mutationObserverCallback","mutationList","mutation","type","attributeName","contains","current","attributes","subtree","attributeFilter","IntersectionObserver","forEach","element","observe","disconnect","setObserverInitExternal","newInit"],"mappings":"AAAA;AAEA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,yBAAyB,QAAQ,4BAA4B;AACtE,SAASC,sBAAsBC,SAAS,QAAQ,kCAAkC;AAElF,MAAM,EAAEC,WAAW,EAAEC,QAAQ,EAAEC,MAAM,EAAE,GAAGN;AAC1C,SAASO,mBAAmB,QAAQ,wBAAwB;AAE5D;;;;;;CAMC,GACD,OAAO,MAAMC,mBAAmB,CAC9BC,eACAC,QACAC;IAEA,IAAID,UAAUC,KAAK;QACjB,8CAA8C;QAC9C,MAAMC,SAASD,IAAIE,gBAAg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tB,iBAAiBC,cAAc6B,OAAO,EAAEU,oBAAAA,8BAAAA,QAASjB,IAAI,EAAapB;QAChF;IACF,GACA;QAACF;QAAeoB;QAAiBlB;KAAI;IAGvC,OAAO;QAAEc;QAAiBI,iBAAiBkB;QAAyBzB;IAAS;AAC/E,EAAE"}
@@ -0,0 +1,130 @@
1
+ 'use client';
2
+ import * as React from 'react';
3
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
4
+ /**
5
+ * Provides a way of automating size in the virtualizer
6
+ * Returns
7
+ * `width` - element width ref (0 by default),
8
+ * `height` - element height ref (0 by default),
9
+ * `measureElementRef` - a ref function to be passed as `ref` to the element you want to measure
10
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
11
+ */ export function useMeasureList(currentIndex, refLength, totalLength, defaultItemSize) {
12
+ const widthArray = React.useRef(new Array(totalLength).fill(defaultItemSize));
13
+ const heightArray = React.useRef(new Array(totalLength).fill(defaultItemSize));
14
+ const refArray = React.useRef([]);
15
+ const { targetDocument } = useFluent();
16
+ // This lets us trigger updates when a size change occurs.
17
+ const sizeUpdateCount = React.useRef(0);
18
+ // the handler for resize observer
19
+ const handleIndexUpdate = React.useCallback((index)=>{
20
+ var _refArray_current_index;
21
+ let isChanged = false;
22
+ const boundClientRect = (_refArray_current_index = refArray.current[index]) === null || _refArray_current_index === void 0 ? void 0 : _refArray_current_index.getBoundingClientRect();
23
+ const containerWidth = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.width;
24
+ if (containerWidth !== widthArray.current[currentIndex + index]) {
25
+ isChanged = true;
26
+ }
27
+ widthArray.current[currentIndex + index] = containerWidth || defaultItemSize;
28
+ const containerHeight = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.height;
29
+ if (containerHeight !== heightArray.current[currentIndex + index]) {
30
+ isChanged = true;
31
+ }
32
+ heightArray.current[currentIndex + index] = containerHeight || defaultItemSize;
33
+ if (isChanged) {
34
+ sizeUpdateCount.current = sizeUpdateCount.current + 1;
35
+ }
36
+ }, [
37
+ currentIndex,
38
+ defaultItemSize,
39
+ sizeUpdateCount
40
+ ]);
41
+ const handleElementResizeCallback = (entries)=>{
42
+ for (const entry of entries){
43
+ const target = entry.target;
44
+ // Call the elements own resize handler (indexed)
45
+ target.handleResize();
46
+ }
47
+ };
48
+ React.useEffect(()=>{
49
+ const newHeightLength = totalLength - heightArray.current.length;
50
+ const newWidthLength = totalLength - widthArray.current.length;
51
+ /* Ensure we grow or truncate arrays with prior properties,
52
+ keeping the existing values is important for whitespace assumptions.
53
+ Even if items in the 'middle' are deleted, we will recalc the whitespace as it is explored.*/ if (newWidthLength > 0) {
54
+ widthArray.current = widthArray.current.concat(new Array(newWidthLength).fill(defaultItemSize));
55
+ } else if (newWidthLength < 0) {
56
+ widthArray.current = widthArray.current.slice(0, totalLength);
57
+ }
58
+ if (newHeightLength > 0) {
59
+ heightArray.current = heightArray.current.concat(new Array(newHeightLength).fill(defaultItemSize));
60
+ } else if (newHeightLength < 0) {
61
+ heightArray.current = heightArray.current.slice(0, totalLength);
62
+ }
63
+ }, [
64
+ defaultItemSize,
65
+ totalLength
66
+ ]);
67
+ // Keep the reference of ResizeObserver as a ref, as it should live through renders
68
+ const resizeObserver = React.useRef(createResizeObserverFromDocument(targetDocument, handleElementResizeCallback));
69
+ /* createIndexedRef provides a dynamic function to create an undefined number of refs at render time
70
+ * these refs then provide an indexed callback via attaching 'handleResize' to the element itself
71
+ * this function is then called on resize by handleElementResize and relies on indexing
72
+ * to track continuous sizes throughout renders while releasing all virtualized element refs each render cycle.
73
+ */ const createIndexedRef = React.useCallback((index)=>{
74
+ const measureElementRef = (el)=>{
75
+ if (!targetDocument || !resizeObserver.current) {
76
+ return;
77
+ }
78
+ if (el) {
79
+ el.handleResize = ()=>{
80
+ handleIndexUpdate(index);
81
+ };
82
+ }
83
+ // cleanup previous container
84
+ if (refArray.current[index] !== undefined && refArray.current[index] !== null) {
85
+ resizeObserver.current.unobserve(refArray.current[index]);
86
+ }
87
+ refArray.current[index] = undefined;
88
+ if (el) {
89
+ refArray.current[index] = el;
90
+ resizeObserver.current.observe(el);
91
+ handleIndexUpdate(index);
92
+ }
93
+ };
94
+ return measureElementRef;
95
+ }, [
96
+ handleIndexUpdate,
97
+ resizeObserver,
98
+ targetDocument
99
+ ]);
100
+ React.useEffect(()=>{
101
+ const _resizeObserver = resizeObserver;
102
+ return ()=>{
103
+ var _resizeObserver_current;
104
+ return (_resizeObserver_current = _resizeObserver.current) === null || _resizeObserver_current === void 0 ? void 0 : _resizeObserver_current.disconnect();
105
+ };
106
+ }, [
107
+ resizeObserver
108
+ ]);
109
+ return {
110
+ widthArray,
111
+ heightArray,
112
+ createIndexedRef,
113
+ refArray,
114
+ sizeUpdateCount: sizeUpdateCount.current
115
+ };
116
+ }
117
+ /**
118
+ * FIXME - TS 3.8/3.9 don't have ResizeObserver types by default, move this to a shared utility once we bump the minbar
119
+ * A utility method that creates a ResizeObserver from a target document
120
+ * @param targetDocument - document to use to create the ResizeObserver
121
+ * @param callback - https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/ResizeObserver#callback
122
+ * @returns a ResizeObserver instance or null if the global does not exist on the document
123
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
124
+ */ export function createResizeObserverFromDocument(targetDocument, callback) {
125
+ var _targetDocument_defaultView;
126
+ if (!(targetDocument === null || targetDocument === void 0 ? void 0 : (_targetDocument_defaultView = targetDocument.defaultView) === null || _targetDocument_defaultView === void 0 ? void 0 : _targetDocument_defaultView.ResizeObserver)) {
127
+ return null;
128
+ }
129
+ return new targetDocument.defaultView.ResizeObserver(callback);
130
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/useMeasureList.ts"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\n\n/**\n * @deprecated migrated to \\@fluentui\\-contrib/react\\-virtualizer for stable release.\n */\nexport interface IndexedResizeCallbackElement {\n handleResize: () => void;\n}\n/**\n * Provides a way of automating size in the virtualizer\n * Returns\n * `width` - element width ref (0 by default),\n * `height` - element height ref (0 by default),\n * `measureElementRef` - a ref function to be passed as `ref` to the element you want to measure\n * @deprecated migrated to \\@fluentui\\-contrib/react\\-virtualizer for stable release.\n */\nexport function useMeasureList<\n TElement extends HTMLElement & IndexedResizeCallbackElement = HTMLElement & IndexedResizeCallbackElement,\n>(\n currentIndex: number,\n refLength: number,\n totalLength: number,\n defaultItemSize: number,\n): {\n // eslint-disable-next-line @typescript-eslint/no-deprecated\n widthArray: React.MutableRefObject<number[]>;\n // eslint-disable-next-line @typescript-eslint/no-deprecated\n heightArray: React.MutableRefObject<number[]>;\n createIndexedRef: (index: number) => (el: TElement | null) => void;\n // eslint-disable-next-line @typescript-eslint/no-deprecated\n refArray: React.MutableRefObject<Array<TElement | undefined | null>>;\n sizeUpdateCount: number;\n} {\n const widthArray = React.useRef(new Array(totalLength).fill(defaultItemSize));\n const heightArray = React.useRef(new Array(totalLength).fill(defaultItemSize));\n\n const refArray = React.useRef<Array<TElement | undefined | null>>([]);\n const { targetDocument } = useFluent();\n\n // This lets us trigger updates when a size change occurs.\n const sizeUpdateCount = React.useRef(0);\n\n // the handler for resize observer\n const handleIndexUpdate = React.useCallback(\n (index: number) => {\n let isChanged = false;\n const boundClientRect = refArray.current[index]?.getBoundingClientRect();\n const containerWidth = boundClientRect?.width;\n if (containerWidth !== widthArray.current[currentIndex + index]) {\n isChanged = true;\n }\n widthArray.current[currentIndex + index] = containerWidth || defaultItemSize;\n\n const containerHeight = boundClientRect?.height;\n\n if (containerHeight !== heightArray.current[currentIndex + index]) {\n isChanged = true;\n }\n heightArray.current[currentIndex + index] = containerHeight || defaultItemSize;\n\n if (isChanged) {\n sizeUpdateCount.current = sizeUpdateCount.current + 1;\n }\n },\n [currentIndex, defaultItemSize, sizeUpdateCount],\n );\n\n const handleElementResizeCallback = (entries: ResizeObserverEntry[]) => {\n for (const entry of entries) {\n const target = entry.target as TElement;\n // Call the elements own resize handler (indexed)\n target.handleResize();\n }\n };\n\n React.useEffect(() => {\n const newHeightLength = totalLength - heightArray.current.length;\n const newWidthLength = totalLength - widthArray.current.length;\n /* Ensure we grow or truncate arrays with prior properties,\n keeping the existing values is important for whitespace assumptions.\n Even if items in the 'middle' are deleted, we will recalc the whitespace as it is explored.*/\n if (newWidthLength > 0) {\n widthArray.current = widthArray.current.concat(new Array(newWidthLength).fill(defaultItemSize));\n } else if (newWidthLength < 0) {\n widthArray.current = widthArray.current.slice(0, totalLength);\n }\n if (newHeightLength > 0) {\n heightArray.current = heightArray.current.concat(new Array(newHeightLength).fill(defaultItemSize));\n } else if (newHeightLength < 0) {\n heightArray.current = heightArray.current.slice(0, totalLength);\n }\n }, [defaultItemSize, totalLength]);\n\n // Keep the reference of ResizeObserver as a ref, as it should live through renders\n const resizeObserver = React.useRef(createResizeObserverFromDocument(targetDocument, handleElementResizeCallback));\n\n /* createIndexedRef provides a dynamic function to create an undefined number of refs at render time\n * these refs then provide an indexed callback via attaching 'handleResize' to the element itself\n * this function is then called on resize by handleElementResize and relies on indexing\n * to track continuous sizes throughout renders while releasing all virtualized element refs each render cycle.\n */\n const createIndexedRef = React.useCallback(\n (index: number) => {\n const measureElementRef = (el: TElement | null) => {\n if (!targetDocument || !resizeObserver.current) {\n return;\n }\n\n if (el) {\n el.handleResize = () => {\n handleIndexUpdate(index);\n };\n }\n\n // cleanup previous container\n if (refArray.current[index] !== undefined && refArray.current[index] !== null) {\n resizeObserver.current.unobserve(refArray.current[index]!);\n }\n\n refArray.current[index] = undefined;\n if (el) {\n refArray.current[index] = el;\n resizeObserver.current.observe(el);\n handleIndexUpdate(index);\n }\n };\n\n return measureElementRef;\n },\n [handleIndexUpdate, resizeObserver, targetDocument],\n );\n\n React.useEffect(() => {\n const _resizeObserver = resizeObserver;\n return () => _resizeObserver.current?.disconnect();\n }, [resizeObserver]);\n\n return { widthArray, heightArray, createIndexedRef, refArray, sizeUpdateCount: sizeUpdateCount.current };\n}\n\n/**\n * FIXME - TS 3.8/3.9 don't have ResizeObserver types by default, move this to a shared utility once we bump the minbar\n * A utility method that creates a ResizeObserver from a target document\n * @param targetDocument - document to use to create the ResizeObserver\n * @param callback - https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/ResizeObserver#callback\n * @returns a ResizeObserver instance or null if the global does not exist on the document\n * @deprecated migrated to \\@fluentui\\-contrib/react\\-virtualizer for stable release.\n */\nexport function createResizeObserverFromDocument(\n targetDocument: Document | null | undefined,\n callback: ResizeObserverCallback,\n): ResizeObserver | null {\n if (!targetDocument?.defaultView?.ResizeObserver) {\n return null;\n }\n\n return new targetDocument.defaultView.ResizeObserver(callback);\n}\n"],"names":["React","useFluent_unstable","useFluent","useMeasureList","currentIndex","refLength","totalLength","defaultItemSize","widthArray","useRef","Array","fill","heightArray","refArray","targetDocument","sizeUpdateCount","handleIndexUpdate","useCallback","index","isChanged","boundClientRect","current","getBoundingClientRect","containerWidth","width","containerHeight","height","handleElementResizeCallback","entries","entry","target","handleResize","useEffect","newHeightLength","length","newWidthLength","concat","slice","resizeObserver","createResizeObserverFromDocument","createIndexedRef","measureElementRef","el","undefined","unobserve","observe","_resizeObserver","disconnect","callback","defaultView","ResizeObserver"],"mappings":"AAAA;AAEA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,sBAAsBC,SAAS,QAAQ,kCAAkC;AAQlF;;;;;;;CAOC,GACD,OAAO,SAASC,eAGdC,YAAoB,EACpBC,SAAiB,EACjBC,WAAmB,EACnBC,eAAuB;IAWvB,MAAMC,aAAaR,MAAMS,MAAM,CAAC,IAAIC,MAAMJ,aAAaK,IAAI,CAACJ;IAC5D,MAAMK,cAAcZ,MAAMS,MAAM,CAAC,IAAIC,MAAMJ,aAAaK,IAAI,CAACJ;IAE7D,MAAMM,WAAWb,MAAMS,MAAM,CAAqC,EAAE;IACpE,MAAM,EAAEK,cAAc,EAAE,GAAGZ;IAE3B,0DAA0D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AAE9B;QAAYI;QAAa4B;QAAkB3B;QAAUE,iBAAiBA,gBAAgBM,OAAO;IAAC;AACzG;AAEA;;;;;;;CAOC,GACD,OAAO,SAASkB,iCACdzB,cAA2C,EAC3CkC,QAAgC;QAE3BlC;IAAL,IAAI,EAACA,2BAAAA,sCAAAA,8BAAAA,eAAgBmC,WAAW,cAA3BnC,kDAAAA,4BAA6BoC,cAAc,GAAE;QAChD,OAAO;IACT;IAEA,OAAO,IAAIpC,eAAemC,WAAW,CAACC,cAAc,CAACF;AACvD"}
@@ -0,0 +1,40 @@
1
+ 'use client';
2
+ import * as React from 'react';
3
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
4
+ const { useRef, useEffect } = React;
5
+ /**
6
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
7
+ */ export const useMutationObserver = (target, callback, options)=>{
8
+ 'use no memo';
9
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
10
+ const observer = useRef(undefined);
11
+ const { targetDocument } = useFluent();
12
+ const win = targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView;
13
+ useEffect(()=>{
14
+ if (!win) {
15
+ return;
16
+ }
17
+ // Create an observer instance linked to the callback function
18
+ observer.current = new win.MutationObserver(callback);
19
+ }, [
20
+ callback,
21
+ win
22
+ ]);
23
+ useEffect(()=>{
24
+ if (target) {
25
+ var // Start observing the target node for configured mutations
26
+ _observer_current;
27
+ (_observer_current = observer.current) === null || _observer_current === void 0 ? void 0 : _observer_current.observe(target, options);
28
+ }
29
+ return ()=>{
30
+ var _observer_current;
31
+ (_observer_current = observer.current) === null || _observer_current === void 0 ? void 0 : _observer_current.disconnect();
32
+ };
33
+ }, [
34
+ target,
35
+ options
36
+ ]);
37
+ return {
38
+ observer
39
+ };
40
+ };
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/useMutationObserver.ts"],"sourcesContent":["'use client';\n\nimport * as React from 'react';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\n\nconst { useRef, useEffect } = React;\n\n/**\n * @deprecated migrated to \\@fluentui\\-contrib/react\\-virtualizer for stable release.\n */\nexport const useMutationObserver = (\n target: Element | Document | undefined,\n callback: MutationCallback,\n options?: MutationObserverInit,\n): {\n // eslint-disable-next-line @typescript-eslint/no-deprecated\n observer: React.MutableRefObject<MutationObserver | undefined>;\n} => {\n 'use no memo';\n\n // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286\n\n const observer = useRef<MutationObserver | undefined>(undefined);\n const { targetDocument } = useFluent();\n const win = targetDocument?.defaultView;\n\n useEffect(() => {\n if (!win) {\n return;\n }\n // Create an observer instance linked to the callback function\n observer.current = new win.MutationObserver(callback);\n }, [callback, win]);\n\n useEffect(() => {\n if (target) {\n // Start observing the target node for configured mutations\n observer.current?.observe(target, options);\n }\n\n return () => {\n observer.current?.disconnect();\n };\n }, [target, options]);\n\n return { observer };\n};\n"],"names":["React","useFluent_unstable","useFluent","useRef","useEffect","useMutationObserver","target","callback","options","observer","undefined","targetDocument","win","defaultView","current","MutationObserver","observe","disconnect"],"mappings":"AAAA;AAEA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,sBAAsBC,SAAS,QAAQ,kCAAkC;AAElF,MAAM,EAAEC,MAAM,EAAEC,SAAS,EAAE,GAAGJ;AAE9B;;CAEC,GACD,OAAO,MAAMK,sBAAsB,CACjCC,QACAC,UACAC;IAKA;IAEA,8FAA8F;IAE9F,MAAMC,WAAWN,OAAqCO;IACtD,MAAM,EAAEC,cAAc,EAAE,GAAGT;IAC3B,MAAMU,MAAMD,2BAAAA,qCAAAA,eAAgBE,WAAW;IAEvCT,UAAU;QACR,IAAI,CAACQ,KAAK;YACR;QACF;QACA,8DAA8D;QAC9DH,SAASK,OAAO,GAAG,IAAIF,IAAIG,gBAAgB,CAACR;IAC9C,GAAG;QAACA;QAAUK;KAAI;IAElBR,UAAU;QACR,IAAIE,QAAQ;gBACV,2DAA2D;YAC3DG;aAAAA,oBAAAA,SAASK,OAAO,cAAhBL,wCAAAA,kBAAkBO,OAAO,CAACV,QAAQE;QACpC;QAEA,OAAO;gBACLC;aAAAA,oBAAAA,SAASK,OAAO,cAAhBL,wCAAAA,kBAAkBQ,UAAU;QAC9B;IACF,GAAG;QAACX;QAAQE;KAAQ;IAEpB,OAAO;QAAEC;IAAS;AACpB,EAAE"}
@@ -0,0 +1,61 @@
1
+ 'use client';
2
+ import * as React from 'react';
3
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
4
+ import { debounce } from '../utilities/debounce';
5
+ import { createResizeObserverFromDocument } from '../utilities/createResizeObserverFromDocument';
6
+ /**
7
+ * useResizeObserverRef_unstable simplifies resize observer connection and ensures debounce/cleanup
8
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
9
+ */ export const useResizeObserverRef_unstable = (resizeCallback)=>{
10
+ 'use no memo';
11
+ const { targetDocument } = useFluent();
12
+ const container = React.useRef(null);
13
+ const containerHeightRef = React.useRef(0);
14
+ const containerWidthRef = React.useRef(0);
15
+ // the handler for resize observer
16
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
17
+ const handleResize = debounce((entries, observer)=>{
18
+ var _container_current, _container_current1;
19
+ const containerHeight = (_container_current = container.current) === null || _container_current === void 0 ? void 0 : _container_current.clientHeight;
20
+ const containerWidth = (_container_current1 = container.current) === null || _container_current1 === void 0 ? void 0 : _container_current1.clientWidth;
21
+ // Our resize observer will fire on scroll resize, let index change handle that instead.
22
+ if (containerHeightRef.current !== containerHeight || containerWidth !== containerWidthRef.current) {
23
+ containerWidthRef.current = containerWidth !== null && containerWidth !== void 0 ? containerWidth : 0;
24
+ containerHeightRef.current = containerHeight !== null && containerHeight !== void 0 ? containerHeight : 0;
25
+ resizeCallback(entries, observer, container);
26
+ }
27
+ });
28
+ // Keep the reference of ResizeObserver in the state, as it should live through renders
29
+ const [resizeObserver, setResizeObserver] = React.useState(()=>createResizeObserverFromDocument(targetDocument, handleResize));
30
+ React.useEffect(()=>{
31
+ // Update our state when resizeCallback changes
32
+ container.current = null;
33
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
34
+ setResizeObserver(()=>createResizeObserverFromDocument(targetDocument, handleResize));
35
+ // eslint-disable-next-line react-hooks/exhaustive-deps
36
+ }, [
37
+ resizeCallback,
38
+ targetDocument
39
+ ]);
40
+ React.useEffect(()=>{
41
+ return ()=>{
42
+ container.current = null;
43
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
44
+ };
45
+ // eslint-disable-next-line react-hooks/exhaustive-deps
46
+ }, []);
47
+ const scrollRef = React.useCallback((instance)=>{
48
+ if (container.current !== instance) {
49
+ if (container.current) {
50
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.unobserve(container.current);
51
+ }
52
+ container.current = instance;
53
+ if (container.current) {
54
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.observe(container.current);
55
+ }
56
+ }
57
+ }, [
58
+ resizeObserver
59
+ ]);
60
+ return scrollRef;
61
+ };