@fluentui/react-virtualizer 9.0.0-alpha.10 → 9.0.0-alpha.101

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 +951 -10
  2. package/README.md +2 -0
  3. package/dist/index.d.ts +416 -33
  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 +9 -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} +9 -4
  25. package/lib/components/Virtualizer/useVirtualizerStyles.styles.js.map +1 -0
  26. package/lib/components/Virtualizer/useVirtualizerStyles.styles.raw.js +102 -0
  27. package/lib/components/Virtualizer/useVirtualizerStyles.styles.raw.js.map +1 -0
  28. package/lib/components/VirtualizerScrollView/VirtualizerScrollView.js +15 -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 +50 -0
  39. package/lib/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.js.map +1 -0
  40. package/lib/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.raw.js +45 -0
  41. package/lib/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.raw.js.map +1 -0
  42. package/lib/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.js +15 -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 +50 -0
  53. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js.map +1 -0
  54. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.raw.js +45 -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 +127 -0
  61. package/lib/hooks/useDynamicPagination.js.map +1 -0
  62. package/lib/hooks/useDynamicVirtualizerMeasure.js +127 -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 +129 -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 +103 -0
  73. package/lib/hooks/useStaticPagination.js.map +1 -0
  74. package/lib/hooks/useVirtualizerMeasure.js +63 -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 +32 -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 +19 -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 +123 -0
  119. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.styles.js.map +1 -0
  120. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.styles.raw.js +115 -0
  121. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.styles.raw.js.map +1 -0
  122. package/lib-commonjs/components/VirtualizerScrollView/VirtualizerScrollView.js +23 -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 +70 -0
  133. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.js.map +1 -0
  134. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.raw.js +58 -0
  135. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.raw.js.map +1 -0
  136. package/lib-commonjs/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.js +23 -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 +70 -0
  147. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js.map +1 -0
  148. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.raw.js +58 -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 +131 -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 +134 -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 +107 -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 +48 -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 +20 -24
  193. package/CHANGELOG.json +0 -155
  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,127 @@
1
+ import { useIsomorphicLayoutEffect, useMergedRefs } from '@fluentui/react-utilities';
2
+ import * as React from 'react';
3
+ import { useResizeObserverRef_unstable } from './useResizeObserverRef';
4
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
5
+ /**
6
+ * React hook that measures virtualized space dynamically to ensure optimized virtualization length.
7
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
8
+ */ export const useDynamicVirtualizerMeasure = (virtualizerProps)=>{
9
+ const { defaultItemSize, direction = 'vertical', numItems, getItemSize, bufferItems, bufferSize, virtualizerContext } = virtualizerProps;
10
+ const [state, setState] = React.useState({
11
+ virtualizerLength: 0,
12
+ virtualizerBufferItems: 0,
13
+ virtualizerBufferSize: 0
14
+ });
15
+ const containerSizeRef = React.useRef(0);
16
+ const scrollPosition = React.useRef(0);
17
+ const { virtualizerLength, virtualizerBufferItems, virtualizerBufferSize } = state;
18
+ const { targetDocument } = useFluent();
19
+ const container = React.useRef(null);
20
+ const handleScrollResize = React.useCallback((scrollRef)=>{
21
+ if (!(scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current)) {
22
+ // Error? ignore?
23
+ return;
24
+ }
25
+ if (scrollRef.current !== (targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.body)) {
26
+ // We have a local scroll container
27
+ 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;
28
+ } else if (targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView) {
29
+ var _targetDocument_defaultView, _targetDocument_defaultView1;
30
+ // If our scroll ref is the document body, we should check window height
31
+ 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;
32
+ }
33
+ let indexSizer = 0;
34
+ let i = 0;
35
+ let length = 0;
36
+ const startIndex = virtualizerContext.contextIndex;
37
+ const sizeToBeat = containerSizeRef.current + virtualizerBufferSize * 2;
38
+ while(indexSizer <= sizeToBeat && i + startIndex < numItems){
39
+ const iItemSize = getItemSize(startIndex + i);
40
+ if (virtualizerContext.childProgressiveSizes.current.length < numItems) {
41
+ /* We are in unknown territory, either an initial render or an update
42
+ in virtualizer item length has occurred.
43
+ We need to let the new items render first then we can accurately assess.*/ return virtualizerLength - virtualizerBufferSize * 2;
44
+ }
45
+ const currentScrollPos = scrollPosition.current;
46
+ const currentItemPos = virtualizerContext.childProgressiveSizes.current[startIndex + i] - iItemSize;
47
+ if (currentScrollPos > currentItemPos + iItemSize) {
48
+ // The item isn't in view, ignore for now.
49
+ i++;
50
+ continue;
51
+ } else if (currentScrollPos > currentItemPos) {
52
+ // The item is partially out of view, ignore the out of bounds portion
53
+ const variance = currentItemPos + iItemSize - currentScrollPos;
54
+ indexSizer += variance;
55
+ } else {
56
+ // Item is in view
57
+ indexSizer += iItemSize;
58
+ }
59
+ // Increment
60
+ i++;
61
+ length++;
62
+ }
63
+ /*
64
+ * Number of items to append at each end, i.e. 'preload' each side before entering view.
65
+ * Minimum: 2 - we give slightly more buffer for dynamic version.
66
+ */ const newBufferItems = bufferItems !== null && bufferItems !== void 0 ? bufferItems : Math.max(Math.ceil(length / 3), 1);
67
+ /*
68
+ * This is how far we deviate into the bufferItems to detect a redraw.
69
+ */ const newBufferSize = bufferSize !== null && bufferSize !== void 0 ? bufferSize : Math.max(defaultItemSize / 2, 1);
70
+ const totalLength = length + newBufferItems * 2;
71
+ setState({
72
+ virtualizerLength: totalLength,
73
+ virtualizerBufferSize: newBufferSize,
74
+ virtualizerBufferItems: newBufferItems
75
+ });
76
+ }, [
77
+ bufferItems,
78
+ bufferSize,
79
+ defaultItemSize,
80
+ direction,
81
+ getItemSize,
82
+ numItems,
83
+ targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.body,
84
+ targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView,
85
+ virtualizerBufferSize,
86
+ virtualizerContext.childProgressiveSizes,
87
+ virtualizerContext.contextIndex,
88
+ virtualizerLength
89
+ ]);
90
+ const resizeCallback = React.useCallback((_entries, // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
91
+ // eslint-disable-next-line no-restricted-globals
92
+ _observer, scrollRef)=>{
93
+ if (scrollRef) {
94
+ handleScrollResize(scrollRef);
95
+ }
96
+ }, [
97
+ handleScrollResize
98
+ ]);
99
+ const scrollRef = useMergedRefs(container, useResizeObserverRef_unstable(resizeCallback));
100
+ useIsomorphicLayoutEffect(()=>{
101
+ if (virtualizerContext.contextIndex + virtualizerLength < numItems) {
102
+ // Avoid re-rendering/re-calculating when the end index has already been reached
103
+ handleScrollResize(container);
104
+ }
105
+ }, [
106
+ handleScrollResize,
107
+ numItems,
108
+ virtualizerContext.contextIndex,
109
+ virtualizerLength
110
+ ]);
111
+ const updateScrollPosition = React.useCallback((_scrollPosition)=>{
112
+ scrollPosition.current = _scrollPosition;
113
+ // Check if our vLength's need recalculating
114
+ handleScrollResize(scrollRef);
115
+ }, [
116
+ handleScrollResize,
117
+ scrollRef
118
+ ]);
119
+ return {
120
+ virtualizerLength,
121
+ bufferItems: virtualizerBufferItems,
122
+ bufferSize: virtualizerBufferSize,
123
+ scrollRef,
124
+ containerSizeRef,
125
+ updateScrollPosition
126
+ };
127
+ };
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/useDynamicVirtualizerMeasure.ts"],"sourcesContent":["import { 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 (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 // eslint-disable-next-line no-restricted-globals\n _observer: ResizeObserver,\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","updateScrollPosition","_scrollPosition"],"mappings":"AAAA,SAASA,yBAAyB,EAAEC,aAAa,QAAQ,4BAA4B;AACrF,YAAYC,WAAW,QAAQ;AAE/B,SAAS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eAAgBU,WAAW;QAC3Bd;QACAN,mBAAmB8B,qBAAqB;QACxC9B,mBAAmB2B,YAAY;QAC/BvB;KACD;IAGH,MAAMoC,iBAAiBpD,MAAMyB,WAAW,CACtC,CACE4B,UACA,8FAA8F;IAC9F,iDAAiD;IACjDC,WACA5B;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,33 @@
1
1
  import * as React from 'react';
2
2
  import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
3
- const {
4
- useState,
5
- useRef
6
- } = React;
3
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
4
+ const { useCallback, useState, useRef } = React;
5
+ import { useMutationObserver } from './useMutationObserver';
6
+ /**
7
+ * 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.
8
+ * @param ltrRootMargin the margin to be processed and flipped if required
9
+ * @param target target element that will have its current reading direction determined
10
+ * @returns the corrected rootMargin (if it was necessary to correct)
11
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
12
+ */ export const getRTLRootMargin = (ltrRootMargin, target, win)=>{
13
+ if (target && win) {
14
+ // get the computed dir for the target element
15
+ const newDir = win.getComputedStyle(target).direction;
16
+ // If we're in rtl reading direction, we might need to flip the margins on the left/right sides
17
+ if (newDir === 'rtl') {
18
+ let newMargin = ltrRootMargin;
19
+ const splitMargins = ltrRootMargin.split(' ');
20
+ // We only need to do this if we get four values, otherwise the sides are equal and don't require flipping.
21
+ if (splitMargins.length === 4) {
22
+ newMargin = `${splitMargins[0]} ${splitMargins[3]} ${splitMargins[2]} ${splitMargins[1]}`;
23
+ }
24
+ return newMargin;
25
+ } else {
26
+ return ltrRootMargin;
27
+ }
28
+ }
29
+ return ltrRootMargin;
30
+ };
7
31
  /**
8
32
  * React hook that allows easy usage of the browser API IntersectionObserver within React
9
33
  * @param callback - A function called when the percentage of the target element is visible crosses a threshold.
@@ -12,41 +36,96 @@ const {
12
36
  * enough to trigger a callback).
13
37
  * @returns An array containing a callback to update the list of Elements the observer should listen to, a callback to
14
38
  * 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
- }
39
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
40
+ */ export const useIntersectionObserver = (callback, options)=>{
41
+ 'use no memo';
42
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
43
+ // eslint-disable-next-line no-restricted-globals
44
+ const observer = useRef();
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":["import * 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 // eslint-disable-next-line no-restricted-globals\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 // eslint-disable-next-line no-restricted-globals\n const observer = useRef<IntersectionObserver>();\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","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,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,gBAAgB,CAACH,QAAmBI,SAAS;QAEhE,+FAA+F;QAC/F,IAAIF,WAAW,OAAO;YACpB,IAAIG,YAAYN;YAChB,MAAMO,eAAeP,cAAcQ,KAAK,CAAC;YAEzC,2GAA2G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iBI,iBAAiBkB;QAAyBxB;IAAS;AAC/E,EAAE"}
@@ -0,0 +1,129 @@
1
+ import * as React from 'react';
2
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
3
+ /**
4
+ * Provides a way of automating size in the virtualizer
5
+ * Returns
6
+ * `width` - element width ref (0 by default),
7
+ * `height` - element height ref (0 by default),
8
+ * `measureElementRef` - a ref function to be passed as `ref` to the element you want to measure
9
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
10
+ */ export function useMeasureList(currentIndex, refLength, totalLength, defaultItemSize) {
11
+ const widthArray = React.useRef(new Array(totalLength).fill(defaultItemSize));
12
+ const heightArray = React.useRef(new Array(totalLength).fill(defaultItemSize));
13
+ const refArray = React.useRef([]);
14
+ const { targetDocument } = useFluent();
15
+ // This lets us trigger updates when a size change occurs.
16
+ const sizeUpdateCount = React.useRef(0);
17
+ // the handler for resize observer
18
+ const handleIndexUpdate = React.useCallback((index)=>{
19
+ var _refArray_current_index;
20
+ let isChanged = false;
21
+ const boundClientRect = (_refArray_current_index = refArray.current[index]) === null || _refArray_current_index === void 0 ? void 0 : _refArray_current_index.getBoundingClientRect();
22
+ const containerWidth = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.width;
23
+ if (containerWidth !== widthArray.current[currentIndex + index]) {
24
+ isChanged = true;
25
+ }
26
+ widthArray.current[currentIndex + index] = containerWidth || defaultItemSize;
27
+ const containerHeight = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.height;
28
+ if (containerHeight !== heightArray.current[currentIndex + index]) {
29
+ isChanged = true;
30
+ }
31
+ heightArray.current[currentIndex + index] = containerHeight || defaultItemSize;
32
+ if (isChanged) {
33
+ sizeUpdateCount.current = sizeUpdateCount.current + 1;
34
+ }
35
+ }, [
36
+ currentIndex,
37
+ defaultItemSize,
38
+ sizeUpdateCount
39
+ ]);
40
+ const handleElementResizeCallback = (entries)=>{
41
+ for (const entry of entries){
42
+ const target = entry.target;
43
+ // Call the elements own resize handler (indexed)
44
+ target.handleResize();
45
+ }
46
+ };
47
+ React.useEffect(()=>{
48
+ const newHeightLength = totalLength - heightArray.current.length;
49
+ const newWidthLength = totalLength - widthArray.current.length;
50
+ /* Ensure we grow or truncate arrays with prior properties,
51
+ keeping the existing values is important for whitespace assumptions.
52
+ Even if items in the 'middle' are deleted, we will recalc the whitespace as it is explored.*/ if (newWidthLength > 0) {
53
+ widthArray.current = widthArray.current.concat(new Array(newWidthLength).fill(defaultItemSize));
54
+ } else if (newWidthLength < 0) {
55
+ widthArray.current = widthArray.current.slice(0, totalLength);
56
+ }
57
+ if (newHeightLength > 0) {
58
+ heightArray.current = heightArray.current.concat(new Array(newHeightLength).fill(defaultItemSize));
59
+ } else if (newHeightLength < 0) {
60
+ heightArray.current = heightArray.current.slice(0, totalLength);
61
+ }
62
+ }, [
63
+ defaultItemSize,
64
+ totalLength
65
+ ]);
66
+ // Keep the reference of ResizeObserver as a ref, as it should live through renders
67
+ const resizeObserver = React.useRef(createResizeObserverFromDocument(targetDocument, handleElementResizeCallback));
68
+ /* createIndexedRef provides a dynamic function to create an undefined number of refs at render time
69
+ * these refs then provide an indexed callback via attaching 'handleResize' to the element itself
70
+ * this function is then called on resize by handleElementResize and relies on indexing
71
+ * to track continuous sizes throughout renders while releasing all virtualized element refs each render cycle.
72
+ */ const createIndexedRef = React.useCallback((index)=>{
73
+ const measureElementRef = (el)=>{
74
+ if (!targetDocument || !resizeObserver.current) {
75
+ return;
76
+ }
77
+ if (el) {
78
+ el.handleResize = ()=>{
79
+ handleIndexUpdate(index);
80
+ };
81
+ }
82
+ // cleanup previous container
83
+ if (refArray.current[index] !== undefined && refArray.current[index] !== null) {
84
+ resizeObserver.current.unobserve(refArray.current[index]);
85
+ }
86
+ refArray.current[index] = undefined;
87
+ if (el) {
88
+ refArray.current[index] = el;
89
+ resizeObserver.current.observe(el);
90
+ handleIndexUpdate(index);
91
+ }
92
+ };
93
+ return measureElementRef;
94
+ }, [
95
+ handleIndexUpdate,
96
+ resizeObserver,
97
+ targetDocument
98
+ ]);
99
+ React.useEffect(()=>{
100
+ const _resizeObserver = resizeObserver;
101
+ return ()=>{
102
+ var _resizeObserver_current;
103
+ return (_resizeObserver_current = _resizeObserver.current) === null || _resizeObserver_current === void 0 ? void 0 : _resizeObserver_current.disconnect();
104
+ };
105
+ }, [
106
+ resizeObserver
107
+ ]);
108
+ return {
109
+ widthArray,
110
+ heightArray,
111
+ createIndexedRef,
112
+ refArray,
113
+ sizeUpdateCount: sizeUpdateCount.current
114
+ };
115
+ }
116
+ /**
117
+ * FIXME - TS 3.8/3.9 don't have ResizeObserver types by default, move this to a shared utility once we bump the minbar
118
+ * A utility method that creates a ResizeObserver from a target document
119
+ * @param targetDocument - document to use to create the ResizeObserver
120
+ * @param callback - https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/ResizeObserver#callback
121
+ * @returns a ResizeObserver instance or null if the global does not exist on the document
122
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
123
+ */ export function createResizeObserverFromDocument(targetDocument, callback) {
124
+ var _targetDocument_defaultView;
125
+ if (!(targetDocument === null || targetDocument === void 0 ? void 0 : (_targetDocument_defaultView = targetDocument.defaultView) === null || _targetDocument_defaultView === void 0 ? void 0 : _targetDocument_defaultView.ResizeObserver)) {
126
+ return null;
127
+ }
128
+ return new targetDocument.defaultView.ResizeObserver(callback);
129
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/hooks/useMeasureList.ts"],"sourcesContent":["import * 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>(currentIndex: number, refLength: number, totalLength: number, defaultItemSize: number) {\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) => {\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) {\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,YAAYA,WAAW,QAAQ;AAC/B,SAASC,sBAAsBC,SAAS,QAAQ,kCAAkC;AAQlF;;;;;;;CAOC,GACD,OAAO,SAASC,eAEdC,YAAoB,EAAEC,SAAiB,EAAEC,WAAmB,EAAEC,eAAuB;IACrF,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;IAC1D,MAAMa,kBAAkBf,MAAMS,MAAM,CAAC;IAErC,kCAAkC;IAClC,MAAMO,oBAAoBhB,MAAMiB,WAAW,CACzC,CAACC;YAEyBL;QADxB,IAAIM,YAAY;QAChB,MAAMC,mBAAkBP,0BAAAA,SAASQ,OAAO,CAACH,MAAM,cAAvBL,8CAAAA,wBAAyBS,qBAAqB;QACtE,MAAMC,iBAAiBH,4BAAAA,sCAAAA,gBAAiBI,KAAK;QAC7C,IAAID,mBAAmBf,WAAWa,OAAO,CAACjB,eAAec,MAAM,EAAE;YAC/DC,YAAY;QACd;QACAX,WAAWa,OAAO,CAACjB,eAAec,MAAM,GAAGK,kBAAkBhB;QAE7D,MAAMkB,kBAAkBL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AW,CACxC,CAACC;QACC,MAAMuB,oBAAoB,CAACC;YACzB,IAAI,CAAC5B,kBAAkB,CAACwB,eAAejB,OAAO,EAAE;gBAC9C;YACF;YAEA,IAAIqB,IAAI;gBACNA,GAAGX,YAAY,GAAG;oBAChBf,kBAAkBE;gBACpB;YACF;YAEA,6BAA6B;YAC7B,IAAIL,SAASQ,OAAO,CAACH,MAAM,KAAKyB,aAAa9B,SAASQ,OAAO,CAACH,MAAM,KAAK,MAAM;gBAC7EoB,eAAejB,OAAO,CAACuB,SAAS,CAAC/B,SAASQ,OAAO,CAACH,MAAM;YAC1D;YAEAL,SAASQ,OAAO,CAACH,MAAM,GAAGyB;YAC1B,IAAID,IAAI;gBACN7B,SAASQ,OAAO,CAACH,MAAM,GAAGwB;gBAC1BJ,eAAejB,OAAO,CAACwB,OAAO,CAACH;gBAC/B1B,kBAAkBE;YACpB;QACF;QAEA,OAAOuB;IACT,GACA;QAACzB;QAAmBsB;QAAgBxB;KAAe;IAGrDd,MAAMgC,SAAS,CAAC;QACd,MAAMc,kBAAkBR;QACxB,OAAO;gBAAMQ;oBAAAA,0BAAAA,gBAAgBzB,OAAO,cAAvByB,8CAAAA,wBAAyBC,UAAU;;IAClD,GAAG;QAACT;KAAe;IAEnB,OAAO;QAAE9B;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
+ import * as React from 'react';
2
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
3
+ const { useRef, useEffect } = React;
4
+ /**
5
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
6
+ */ export const useMutationObserver = (target, callback, options)=>{
7
+ 'use no memo';
8
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
9
+ // eslint-disable-next-line no-restricted-globals
10
+ const observer = useRef();
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":["import * 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 observer: React.MutableRefObject<MutationObserver | undefined>; // eslint-disable-line no-restricted-globals\n} => {\n 'use no memo';\n\n // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286\n // eslint-disable-next-line no-restricted-globals\n const observer = useRef<MutationObserver>();\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","targetDocument","win","defaultView","current","MutationObserver","observe","disconnect"],"mappings":"AAAA,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;IAIA;IAEA,8FAA8F;IAC9F,iDAAiD;IACjD,MAAMC,WAAWN;IACjB,MAAM,EAAEO,cAAc,EAAE,GAAGR;IAC3B,MAAMS,MAAMD,2BAAAA,qCAAAA,eAAgBE,WAAW;IAEvCR,UAAU;QACR,IAAI,CAACO,KAAK;YACR;QACF;QACA,8DAA8D;QAC9DF,SAASI,OAAO,GAAG,IAAIF,IAAIG,gBAAgB,CAACP;IAC9C,GAAG;QAACA;QAAUI;KAAI;IAElBP,UAAU;QACR,IAAIE,QAAQ;gBACV,2DAA2D;YAC3DG;aAAAA,oBAAAA,SAASI,OAAO,cAAhBJ,wCAAAA,kBAAkBM,OAAO,CAACT,QAAQE;QACpC;QAEA,OAAO;gBACLC;aAAAA,oBAAAA,SAASI,OAAO,cAAhBJ,wCAAAA,kBAAkBO,UAAU;QAC9B;IACF,GAAG;QAACV;QAAQE;KAAQ;IAEpB,OAAO;QAAEC;IAAS;AACpB,EAAE"}
@@ -0,0 +1,61 @@
1
+ import * as React from 'react';
2
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
3
+ import { debounce } from '../utilities/debounce';
4
+ import { createResizeObserverFromDocument } from '../utilities/createResizeObserverFromDocument';
5
+ /**
6
+ * useResizeObserverRef_unstable simplifies resize observer connection and ensures debounce/cleanup
7
+ * @deprecated migrated to \@fluentui\-contrib/react\-virtualizer for stable release.
8
+ */ export const useResizeObserverRef_unstable = (resizeCallback)=>{
9
+ 'use no memo';
10
+ const { targetDocument } = useFluent();
11
+ const container = React.useRef(null);
12
+ const containerHeightRef = React.useRef(0);
13
+ const containerWidthRef = React.useRef(0);
14
+ // the handler for resize observer
15
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
16
+ // eslint-disable-next-line no-restricted-globals
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
+ };