@fluentui/react-virtualizer 9.0.0-alpha.9 → 9.0.0-alpha.90

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. package/CHANGELOG.md +844 -9
  2. package/dist/index.d.ts +326 -33
  3. package/lib/Hooks.js +1 -2
  4. package/lib/Hooks.js.map +1 -1
  5. package/lib/Utilities.js +1 -0
  6. package/lib/Utilities.js.map +1 -0
  7. package/lib/Virtualizer.js +1 -2
  8. package/lib/Virtualizer.js.map +1 -1
  9. package/lib/VirtualizerScrollView.js +1 -0
  10. package/lib/VirtualizerScrollView.js.map +1 -0
  11. package/lib/VirtualizerScrollViewDynamic.js +1 -0
  12. package/lib/VirtualizerScrollViewDynamic.js.map +1 -0
  13. package/lib/components/Virtualizer/Virtualizer.js +6 -7
  14. package/lib/components/Virtualizer/Virtualizer.js.map +1 -1
  15. package/lib/components/Virtualizer/Virtualizer.types.js +1 -2
  16. package/lib/components/Virtualizer/Virtualizer.types.js.map +1 -1
  17. package/lib/components/Virtualizer/index.js +4 -6
  18. package/lib/components/Virtualizer/index.js.map +1 -1
  19. package/lib/components/Virtualizer/renderVirtualizer.js +21 -16
  20. package/lib/components/Virtualizer/renderVirtualizer.js.map +1 -1
  21. package/lib/components/Virtualizer/useVirtualizer.js +522 -356
  22. package/lib/components/Virtualizer/useVirtualizer.js.map +1 -1
  23. package/lib/components/Virtualizer/{useVirtualizerStyles.js → useVirtualizerStyles.styles.js} +5 -4
  24. package/lib/components/Virtualizer/useVirtualizerStyles.styles.js.map +1 -0
  25. package/lib/components/VirtualizerScrollView/VirtualizerScrollView.js +12 -0
  26. package/lib/components/VirtualizerScrollView/VirtualizerScrollView.js.map +1 -0
  27. package/lib/components/VirtualizerScrollView/VirtualizerScrollView.types.js +1 -0
  28. package/lib/components/VirtualizerScrollView/VirtualizerScrollView.types.js.map +1 -0
  29. package/lib/components/VirtualizerScrollView/index.js +4 -0
  30. package/lib/components/VirtualizerScrollView/index.js.map +1 -0
  31. package/lib/components/VirtualizerScrollView/renderVirtualizerScrollView.js +9 -0
  32. package/lib/components/VirtualizerScrollView/renderVirtualizerScrollView.js.map +1 -0
  33. package/lib/components/VirtualizerScrollView/useVirtualizerScrollView.js +80 -0
  34. package/lib/components/VirtualizerScrollView/useVirtualizerScrollView.js.map +1 -0
  35. package/lib/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.js +46 -0
  36. package/lib/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.js.map +1 -0
  37. package/lib/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.js +12 -0
  38. package/lib/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.js.map +1 -0
  39. package/lib/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.types.js +1 -0
  40. package/lib/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.types.js.map +1 -0
  41. package/lib/components/VirtualizerScrollViewDynamic/index.js +4 -0
  42. package/lib/components/VirtualizerScrollViewDynamic/index.js.map +1 -0
  43. package/lib/components/VirtualizerScrollViewDynamic/renderVirtualizerScrollViewDynamic.js +9 -0
  44. package/lib/components/VirtualizerScrollViewDynamic/renderVirtualizerScrollViewDynamic.js.map +1 -0
  45. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js +152 -0
  46. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js.map +1 -0
  47. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js +46 -0
  48. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js.map +1 -0
  49. package/lib/hooks/hooks.types.js +1 -0
  50. package/lib/hooks/hooks.types.js.map +1 -0
  51. package/lib/hooks/index.js +5 -2
  52. package/lib/hooks/index.js.map +1 -1
  53. package/lib/hooks/useDynamicPagination.js +126 -0
  54. package/lib/hooks/useDynamicPagination.js.map +1 -0
  55. package/lib/hooks/useDynamicVirtualizerMeasure.js +126 -0
  56. package/lib/hooks/useDynamicVirtualizerMeasure.js.map +1 -0
  57. package/lib/hooks/useIntersectionObserver.js +117 -40
  58. package/lib/hooks/useIntersectionObserver.js.map +1 -1
  59. package/lib/hooks/useMeasureList.js +127 -0
  60. package/lib/hooks/useMeasureList.js.map +1 -0
  61. package/lib/hooks/useMutationObserver.js +38 -0
  62. package/lib/hooks/useMutationObserver.js.map +1 -0
  63. package/lib/hooks/useResizeObserverRef.js +60 -0
  64. package/lib/hooks/useResizeObserverRef.js.map +1 -0
  65. package/lib/hooks/useStaticPagination.js +102 -0
  66. package/lib/hooks/useStaticPagination.js.map +1 -0
  67. package/lib/hooks/useVirtualizerMeasure.js +62 -0
  68. package/lib/hooks/useVirtualizerMeasure.js.map +1 -0
  69. package/lib/index.js +4 -2
  70. package/lib/index.js.map +1 -1
  71. package/lib/utilities/ImperativeScrolling/imperativeScrolling.js +32 -0
  72. package/lib/utilities/ImperativeScrolling/imperativeScrolling.js.map +1 -0
  73. package/lib/utilities/ImperativeScrolling/imperativeScrolling.types.js +1 -0
  74. package/lib/utilities/ImperativeScrolling/imperativeScrolling.types.js.map +1 -0
  75. package/lib/utilities/ImperativeScrolling/imperativeScrollingDynamic.js +45 -0
  76. package/lib/utilities/ImperativeScrolling/imperativeScrollingDynamic.js.map +1 -0
  77. package/lib/utilities/ImperativeScrolling/index.js +2 -0
  78. package/lib/utilities/ImperativeScrolling/index.js.map +1 -0
  79. package/lib/utilities/VirtualizerContext/VirtualizerContext.js +26 -0
  80. package/lib/utilities/VirtualizerContext/VirtualizerContext.js.map +1 -0
  81. package/lib/utilities/VirtualizerContext/index.js +1 -0
  82. package/lib/utilities/VirtualizerContext/index.js.map +1 -0
  83. package/lib/utilities/VirtualizerContext/types.js +1 -0
  84. package/lib/utilities/VirtualizerContext/types.js.map +1 -0
  85. package/lib/utilities/createResizeObserverFromDocument.js +13 -0
  86. package/lib/utilities/createResizeObserverFromDocument.js.map +1 -0
  87. package/lib/utilities/debounce.js +19 -0
  88. package/lib/utilities/debounce.js.map +1 -0
  89. package/lib/utilities/index.js +2 -0
  90. package/lib/utilities/index.js.map +1 -0
  91. package/lib-commonjs/Hooks.js +31 -5
  92. package/lib-commonjs/Hooks.js.map +1 -1
  93. package/lib-commonjs/Utilities.js +28 -0
  94. package/lib-commonjs/Utilities.js.map +1 -0
  95. package/lib-commonjs/Virtualizer.js +28 -5
  96. package/lib-commonjs/Virtualizer.js.map +1 -1
  97. package/lib-commonjs/VirtualizerScrollView.js +28 -0
  98. package/lib-commonjs/VirtualizerScrollView.js.map +1 -0
  99. package/lib-commonjs/VirtualizerScrollViewDynamic.js +28 -0
  100. package/lib-commonjs/VirtualizerScrollViewDynamic.js.map +1 -0
  101. package/lib-commonjs/components/Virtualizer/Virtualizer.js +17 -18
  102. package/lib-commonjs/components/Virtualizer/Virtualizer.js.map +1 -1
  103. package/lib-commonjs/components/Virtualizer/Virtualizer.types.js +3 -3
  104. package/lib-commonjs/components/Virtualizer/Virtualizer.types.js.map +1 -1
  105. package/lib-commonjs/components/Virtualizer/index.js +31 -9
  106. package/lib-commonjs/components/Virtualizer/index.js.map +1 -1
  107. package/lib-commonjs/components/Virtualizer/renderVirtualizer.js +38 -21
  108. package/lib-commonjs/components/Virtualizer/renderVirtualizer.js.map +1 -1
  109. package/lib-commonjs/components/Virtualizer/useVirtualizer.js +531 -362
  110. package/lib-commonjs/components/Virtualizer/useVirtualizer.js.map +1 -1
  111. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.styles.js +123 -0
  112. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.styles.js.map +1 -0
  113. package/lib-commonjs/components/VirtualizerScrollView/VirtualizerScrollView.js +21 -0
  114. package/lib-commonjs/components/VirtualizerScrollView/VirtualizerScrollView.js.map +1 -0
  115. package/lib-commonjs/components/VirtualizerScrollView/VirtualizerScrollView.types.js +6 -0
  116. package/lib-commonjs/components/VirtualizerScrollView/VirtualizerScrollView.types.js.map +1 -0
  117. package/lib-commonjs/components/VirtualizerScrollView/index.js +31 -0
  118. package/lib-commonjs/components/VirtualizerScrollView/index.js.map +1 -0
  119. package/lib-commonjs/components/VirtualizerScrollView/renderVirtualizerScrollView.js +19 -0
  120. package/lib-commonjs/components/VirtualizerScrollView/renderVirtualizerScrollView.js.map +1 -0
  121. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollView.js +91 -0
  122. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollView.js.map +1 -0
  123. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.js +70 -0
  124. package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.js.map +1 -0
  125. package/lib-commonjs/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.js +21 -0
  126. package/lib-commonjs/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.js.map +1 -0
  127. package/lib-commonjs/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.types.js +6 -0
  128. package/lib-commonjs/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.types.js.map +1 -0
  129. package/lib-commonjs/components/VirtualizerScrollViewDynamic/index.js +31 -0
  130. package/lib-commonjs/components/VirtualizerScrollViewDynamic/index.js.map +1 -0
  131. package/lib-commonjs/components/VirtualizerScrollViewDynamic/renderVirtualizerScrollViewDynamic.js +19 -0
  132. package/lib-commonjs/components/VirtualizerScrollViewDynamic/renderVirtualizerScrollViewDynamic.js.map +1 -0
  133. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js +162 -0
  134. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js.map +1 -0
  135. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js +70 -0
  136. package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js.map +1 -0
  137. package/lib-commonjs/hooks/hooks.types.js +6 -0
  138. package/lib-commonjs/hooks/hooks.types.js.map +1 -0
  139. package/lib-commonjs/hooks/index.js +35 -5
  140. package/lib-commonjs/hooks/index.js.map +1 -1
  141. package/lib-commonjs/hooks/useDynamicPagination.js +131 -0
  142. package/lib-commonjs/hooks/useDynamicPagination.js.map +1 -0
  143. package/lib-commonjs/hooks/useDynamicVirtualizerMeasure.js +134 -0
  144. package/lib-commonjs/hooks/useDynamicVirtualizerMeasure.js.map +1 -0
  145. package/lib-commonjs/hooks/useIntersectionObserver.js +129 -53
  146. package/lib-commonjs/hooks/useIntersectionObserver.js.map +1 -1
  147. package/lib-commonjs/hooks/useMeasureList.js +134 -0
  148. package/lib-commonjs/hooks/useMeasureList.js.map +1 -0
  149. package/lib-commonjs/hooks/useMutationObserver.js +48 -0
  150. package/lib-commonjs/hooks/useMutationObserver.js.map +1 -0
  151. package/lib-commonjs/hooks/useResizeObserverRef.js +69 -0
  152. package/lib-commonjs/hooks/useResizeObserverRef.js.map +1 -0
  153. package/lib-commonjs/hooks/useStaticPagination.js +107 -0
  154. package/lib-commonjs/hooks/useStaticPagination.js.map +1 -0
  155. package/lib-commonjs/hooks/useVirtualizerMeasure.js +70 -0
  156. package/lib-commonjs/hooks/useVirtualizerMeasure.js.map +1 -0
  157. package/lib-commonjs/index.js +85 -41
  158. package/lib-commonjs/index.js.map +1 -1
  159. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrolling.js +42 -0
  160. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrolling.js.map +1 -0
  161. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrolling.types.js +6 -0
  162. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrolling.types.js.map +1 -0
  163. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrollingDynamic.js +55 -0
  164. package/lib-commonjs/utilities/ImperativeScrolling/imperativeScrollingDynamic.js.map +1 -0
  165. package/lib-commonjs/utilities/ImperativeScrolling/index.js +20 -0
  166. package/lib-commonjs/utilities/ImperativeScrolling/index.js.map +1 -0
  167. package/lib-commonjs/utilities/VirtualizerContext/VirtualizerContext.js +48 -0
  168. package/lib-commonjs/utilities/VirtualizerContext/VirtualizerContext.js.map +1 -0
  169. package/lib-commonjs/utilities/VirtualizerContext/index.js +22 -0
  170. package/lib-commonjs/utilities/VirtualizerContext/index.js.map +1 -0
  171. package/lib-commonjs/utilities/VirtualizerContext/types.js +6 -0
  172. package/lib-commonjs/utilities/VirtualizerContext/types.js.map +1 -0
  173. package/lib-commonjs/utilities/createResizeObserverFromDocument.js +23 -0
  174. package/lib-commonjs/utilities/createResizeObserverFromDocument.js.map +1 -0
  175. package/lib-commonjs/utilities/debounce.js +29 -0
  176. package/lib-commonjs/utilities/debounce.js.map +1 -0
  177. package/lib-commonjs/utilities/index.js +29 -0
  178. package/lib-commonjs/utilities/index.js.map +1 -0
  179. package/package.json +20 -24
  180. package/CHANGELOG.json +0 -140
  181. package/lib/components/Virtualizer/useVirtualizerStyles.js.map +0 -1
  182. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.js +0 -115
  183. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.js.map +0 -1
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useDynamicVirtualizerMeasure", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useDynamicVirtualizerMeasure;
9
+ }
10
+ });
11
+ const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
12
+ const _reactutilities = require("@fluentui/react-utilities");
13
+ const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
14
+ const _useResizeObserverRef = require("./useResizeObserverRef");
15
+ const _reactsharedcontexts = require("@fluentui/react-shared-contexts");
16
+ const useDynamicVirtualizerMeasure = (virtualizerProps)=>{
17
+ const { defaultItemSize, direction = 'vertical', numItems, getItemSize, bufferItems, bufferSize, virtualizerContext } = virtualizerProps;
18
+ const [state, setState] = _react.useState({
19
+ virtualizerLength: 0,
20
+ virtualizerBufferItems: 0,
21
+ virtualizerBufferSize: 0
22
+ });
23
+ const containerSizeRef = _react.useRef(0);
24
+ const scrollPosition = _react.useRef(0);
25
+ const { virtualizerLength, virtualizerBufferItems, virtualizerBufferSize } = state;
26
+ const { targetDocument } = (0, _reactsharedcontexts.useFluent_unstable)();
27
+ const container = _react.useRef(null);
28
+ const handleScrollResize = _react.useCallback((scrollRef)=>{
29
+ if (!(scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current)) {
30
+ // Error? ignore?
31
+ return;
32
+ }
33
+ if (scrollRef.current !== (targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.body)) {
34
+ // We have a local scroll container
35
+ 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;
36
+ } else if (targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView) {
37
+ var _targetDocument_defaultView, _targetDocument_defaultView1;
38
+ // If our scroll ref is the document body, we should check window height
39
+ 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;
40
+ }
41
+ let indexSizer = 0;
42
+ let i = 0;
43
+ let length = 0;
44
+ const startIndex = virtualizerContext.contextIndex;
45
+ const sizeToBeat = containerSizeRef.current + virtualizerBufferSize * 2;
46
+ while(indexSizer <= sizeToBeat && i + startIndex < numItems){
47
+ const iItemSize = getItemSize(startIndex + i);
48
+ if (virtualizerContext.childProgressiveSizes.current.length < numItems) {
49
+ /* We are in unknown territory, either an initial render or an update
50
+ in virtualizer item length has occurred.
51
+ We need to let the new items render first then we can accurately assess.*/ return virtualizerLength - virtualizerBufferSize * 2;
52
+ }
53
+ const currentScrollPos = scrollPosition.current;
54
+ const currentItemPos = virtualizerContext.childProgressiveSizes.current[startIndex + i] - iItemSize;
55
+ if (currentScrollPos > currentItemPos + iItemSize) {
56
+ // The item isn't in view, ignore for now.
57
+ i++;
58
+ continue;
59
+ } else if (currentScrollPos > currentItemPos) {
60
+ // The item is partially out of view, ignore the out of bounds portion
61
+ const variance = currentItemPos + iItemSize - currentScrollPos;
62
+ indexSizer += variance;
63
+ } else {
64
+ // Item is in view
65
+ indexSizer += iItemSize;
66
+ }
67
+ // Increment
68
+ i++;
69
+ length++;
70
+ }
71
+ /*
72
+ * Number of items to append at each end, i.e. 'preload' each side before entering view.
73
+ * Minimum: 2 - we give slightly more buffer for dynamic version.
74
+ */ const newBufferItems = bufferItems !== null && bufferItems !== void 0 ? bufferItems : Math.max(Math.ceil(length / 3), 1);
75
+ /*
76
+ * This is how far we deviate into the bufferItems to detect a redraw.
77
+ */ const newBufferSize = bufferSize !== null && bufferSize !== void 0 ? bufferSize : Math.max(defaultItemSize / 2, 1);
78
+ const totalLength = length + newBufferItems * 2;
79
+ setState({
80
+ virtualizerLength: totalLength,
81
+ virtualizerBufferSize: newBufferSize,
82
+ virtualizerBufferItems: newBufferItems
83
+ });
84
+ }, [
85
+ bufferItems,
86
+ bufferSize,
87
+ defaultItemSize,
88
+ direction,
89
+ getItemSize,
90
+ numItems,
91
+ targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.body,
92
+ targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView,
93
+ virtualizerBufferSize,
94
+ virtualizerContext.childProgressiveSizes,
95
+ virtualizerContext.contextIndex,
96
+ virtualizerLength
97
+ ]);
98
+ const resizeCallback = _react.useCallback((_entries, // eslint-disable-next-line no-restricted-globals
99
+ _observer, scrollRef)=>{
100
+ if (scrollRef) {
101
+ handleScrollResize(scrollRef);
102
+ }
103
+ }, [
104
+ handleScrollResize
105
+ ]);
106
+ const scrollRef = (0, _reactutilities.useMergedRefs)(container, (0, _useResizeObserverRef.useResizeObserverRef_unstable)(resizeCallback));
107
+ (0, _reactutilities.useIsomorphicLayoutEffect)(()=>{
108
+ if (virtualizerContext.contextIndex + virtualizerLength < numItems) {
109
+ // Avoid re-rendering/re-calculating when the end index has already been reached
110
+ handleScrollResize(container);
111
+ }
112
+ }, [
113
+ handleScrollResize,
114
+ numItems,
115
+ virtualizerContext.contextIndex,
116
+ virtualizerLength
117
+ ]);
118
+ const updateScrollPosition = _react.useCallback((_scrollPosition)=>{
119
+ scrollPosition.current = _scrollPosition;
120
+ // Check if our vLength's need recalculating
121
+ handleScrollResize(scrollRef);
122
+ }, [
123
+ handleScrollResize,
124
+ scrollRef
125
+ ]);
126
+ return {
127
+ virtualizerLength,
128
+ bufferItems: virtualizerBufferItems,
129
+ bufferSize: virtualizerBufferSize,
130
+ scrollRef,
131
+ containerSizeRef,
132
+ updateScrollPosition
133
+ };
134
+ };
@@ -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 */\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":["useDynamicVirtualizerMeasure","virtualizerProps","defaultItemSize","direction","numItems","getItemSize","bufferItems","bufferSize","virtualizerContext","state","setState","React","useState","virtualizerLength","virtualizerBufferItems","virtualizerBufferSize","containerSizeRef","useRef","scrollPosition","targetDocument","useFluent","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","useMergedRefs","useResizeObserverRef_unstable","useIsomorphicLayoutEffect","updateScrollPosition","_scrollPosition"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";;;;+BASaA;;;eAAAA;;;;gCAT4C;iEAClC;sCAEuB;qCACE;AAKzC,MAAMA,+BAA+B,CAC1CC;IASA,MAAM,EACJC,eAAe,EACfC,YAAY,UAAU,EACtBC,QAAQ,EACRC,WAAW,EACXC,WAAW,EACXC,UAAU,EACVC,kBAAkB,EACnB,GAAGP;IAEJ,MAAM,CAACQ,OAAOC,SAAS,GAAGC,OAAMC,QAAQ,CAAC;QACvCC,mBAAmB;QACnBC,wBAAwB;QACxBC,uBAAuB;IACzB;IAEA,MAAMC,mBAAmBL,OAAMM,MAAM,CAAS;IAC9C,MAAMC,iBAAiBP,OAAMM,MAAM,CAAS;IAC5C,MAAM,EAAEJ,iBAAiB,EAAEC,sBAAsB,EAAEC,qBAAqB,EAAE,GAAGN;IAE7E,MAAM,EAAEU,cAAc,EAAE,GAAGC,IAAAA,uCAAAA;IAC3B,MAAMC,YAAYV,OAAMM,MAAM,CAAqB;IACnD,MAAMK,qBAAqBX,OAAMY,WAAW,CAC1C,CAACC;Q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A,IAAIA,WAAW;YACbF,mBAAmBE;QACrB;IACF,GACA;QAACF;KAAmB;IAGtB,MAAME,YAAY6B,IAAAA,6BAAAA,EAAchC,WAAWiC,IAAAA,mDAAAA,EAA8BJ;IAEzEK,IAAAA,yCAAAA,EAA0B;QACxB,IAAI/C,mBAAmB6B,YAAY,GAAGxB,oBAAoBT,UAAU;YAClE,gFAAgF;YAChFkB,mBAAmBD;QACrB;IACF,GAAG;QAACC;QAAoBlB;QAAUI,mBAAmB6B,YAAY;QAAExB;KAAkB;IAErF,MAAM2C,uBAAuB7C,OAAMY,WAAW,CAC5C,CAACkC;QACCvC,eAAeO,OAAO,GAAGgC;QACzB,4CAA4C;QAC5CnC,mBAAmBE;IACrB,GACA;QAACF;QAAoBE;KAAU;IAGjC,OAAO;QACLX;QACAP,aAAaQ;QACbP,YAAYQ;QACZS;QACAR;QACAwC;IACF;AACF"}
@@ -1,59 +1,135 @@
1
1
  "use strict";
2
-
3
2
  Object.defineProperty(exports, "__esModule", {
4
- value: true
3
+ value: true
5
4
  });
6
- exports.useIntersectionObserver = void 0;
7
- const React = /*#__PURE__*/require("react");
8
- const react_utilities_1 = /*#__PURE__*/require("@fluentui/react-utilities");
9
- const {
10
- useState,
11
- useRef
12
- } = React;
13
- /**
14
- * React hook that allows easy usage of the browser API IntersectionObserver within React
15
- * @param callback - A function called when the percentage of the target element is visible crosses a threshold.
16
- * @param options - An optional object which customizes the observer. If options isn't specified, the observer uses the
17
- * document's viewport as the root, with no margin, and a 0% threshold (meaning that even a one-pixel change is
18
- * enough to trigger a callback).
19
- * @returns An array containing a callback to update the list of Elements the observer should listen to, a callback to
20
- * update the init options of the IntersectionObserver and a ref to the IntersectionObserver instance itself.
21
- */
22
- const useIntersectionObserver = (callback, options) => {
23
- // export const useIntersectionObserver = (
24
- // callback: IntersectionObserverCallback,
25
- // options?: IntersectionObserverInit,
26
- // ): [
27
- // Dispatch<SetStateAction<Element[] | undefined>>,
28
- // Dispatch<SetStateAction<IntersectionObserverInit | undefined>>,
29
- // MutableRefObject<IntersectionObserver | undefined>,
30
- // ] => {
31
- const observer = useRef();
32
- const [observerList, setObserverList] = useState();
33
- const [observerInit, setObserverInit] = useState(options);
34
- // Observer elements in passed in list and clean up previous list
35
- // This effect is only triggered when observerList is updated
36
- react_utilities_1.useIsomorphicLayoutEffect(() => {
37
- observer.current = new IntersectionObserver(callback, observerInit);
38
- // If we have an instance of IO and a list with elements, observer the elements
39
- if (observer.current && observerList && observerList.length > 0) {
40
- observerList.forEach(element => {
41
- var _a;
42
- (_a = observer.current) === null || _a === void 0 ? void 0 : _a.observe(element);
43
- });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ getRTLRootMargin: function() {
13
+ return getRTLRootMargin;
14
+ },
15
+ useIntersectionObserver: function() {
16
+ return useIntersectionObserver;
44
17
  }
45
- // clean up previous elements being listened to
46
- return () => {
47
- if (observer.current) {
48
- observer.current.disconnect();
49
- }
18
+ });
19
+ const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
20
+ const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
21
+ const _reactutilities = require("@fluentui/react-utilities");
22
+ const _reactsharedcontexts = require("@fluentui/react-shared-contexts");
23
+ const _useMutationObserver = require("./useMutationObserver");
24
+ const { useCallback, useState, useRef } = _react;
25
+ const getRTLRootMargin = (ltrRootMargin, target, win)=>{
26
+ if (target && win) {
27
+ // get the computed dir for the target element
28
+ const newDir = win.getComputedStyle(target).direction;
29
+ // If we're in rtl reading direction, we might need to flip the margins on the left/right sides
30
+ if (newDir === 'rtl') {
31
+ let newMargin = ltrRootMargin;
32
+ const splitMargins = ltrRootMargin.split(' ');
33
+ // We only need to do this if we get four values, otherwise the sides are equal and don't require flipping.
34
+ if (splitMargins.length === 4) {
35
+ newMargin = `${splitMargins[0]} ${splitMargins[3]} ${splitMargins[2]} ${splitMargins[1]}`;
36
+ }
37
+ return newMargin;
38
+ } else {
39
+ return ltrRootMargin;
40
+ }
41
+ }
42
+ return ltrRootMargin;
43
+ };
44
+ const useIntersectionObserver = (callback, options)=>{
45
+ 'use no memo';
46
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
47
+ // eslint-disable-next-line no-restricted-globals
48
+ const observer = useRef();
49
+ const [observerList, setObserverList] = useState();
50
+ const { targetDocument } = (0, _reactsharedcontexts.useFluent_unstable)();
51
+ const win = targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView;
52
+ var _options_rootMargin;
53
+ // set the initial init with corrected margins based on the observed root's calculated reading direction.
54
+ const [observerInit, setObserverInit] = useState(options && {
55
+ ...options,
56
+ rootMargin: getRTLRootMargin((_options_rootMargin = options.rootMargin) !== null && _options_rootMargin !== void 0 ? _options_rootMargin : '0px', options.root, win)
57
+ });
58
+ var _options_rootMargin1;
59
+ // 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.
60
+ const ltrRootMargin = useRef((_options_rootMargin1 = options === null || options === void 0 ? void 0 : options.rootMargin) !== null && _options_rootMargin1 !== void 0 ? _options_rootMargin1 : '0px');
61
+ // Callback function to execute when mutations are observed
62
+ const mutationObserverCallback = useCallback((mutationList)=>{
63
+ for (const mutation of mutationList){
64
+ // Ensuring that the right attribute is being observed and that the root is within the tree of the element being mutated.
65
+ 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)) {
66
+ setObserverInit({
67
+ ...observerInit,
68
+ rootMargin: getRTLRootMargin(ltrRootMargin.current, observerInit === null || observerInit === void 0 ? void 0 : observerInit.root, win)
69
+ });
70
+ }
71
+ }
72
+ }, [
73
+ ltrRootMargin,
74
+ observerInit,
75
+ options === null || options === void 0 ? void 0 : options.root,
76
+ win
77
+ ]);
78
+ // Mutation observer for dir attribute changes in the document
79
+ (0, _useMutationObserver.useMutationObserver)(targetDocument, mutationObserverCallback, {
80
+ attributes: true,
81
+ subtree: true,
82
+ attributeFilter: [
83
+ 'dir'
84
+ ]
85
+ });
86
+ // Observer elements in passed in list and clean up previous list
87
+ // This effect is only triggered when observerList is updated
88
+ (0, _reactutilities.useIsomorphicLayoutEffect)(()=>{
89
+ if (!win) {
90
+ return;
91
+ }
92
+ observer.current = new win.IntersectionObserver(callback, {
93
+ ...observerInit,
94
+ rootMargin: getRTLRootMargin(ltrRootMargin.current, observerInit === null || observerInit === void 0 ? void 0 : observerInit.root, win)
95
+ });
96
+ // If we have an instance of IO and a list with elements, observer the elements
97
+ if (observer.current && observerList && observerList.length > 0) {
98
+ observerList.forEach((element)=>{
99
+ var _observer_current;
100
+ (_observer_current = observer.current) === null || _observer_current === void 0 ? void 0 : _observer_current.observe(element);
101
+ });
102
+ }
103
+ // clean up previous elements being listened to
104
+ return ()=>{
105
+ if (observer.current) {
106
+ observer.current.disconnect();
107
+ }
108
+ };
109
+ }, [
110
+ observerList,
111
+ observerInit,
112
+ callback,
113
+ win
114
+ ]);
115
+ // Do not use internally, we need to track external settings only here
116
+ const setObserverInitExternal = useCallback((newInit)=>{
117
+ var _newInit_rootMargin;
118
+ // Since we know this is coming from consumers, we can store this value as LTR somewhat safely.
119
+ ltrRootMargin.current = (_newInit_rootMargin = newInit === null || newInit === void 0 ? void 0 : newInit.rootMargin) !== null && _newInit_rootMargin !== void 0 ? _newInit_rootMargin : '0px';
120
+ // Call the internal setter to update the value and ensure if our calculated direction is rtl, we flip the margin
121
+ setObserverInit({
122
+ ...newInit,
123
+ rootMargin: getRTLRootMargin(ltrRootMargin.current, newInit === null || newInit === void 0 ? void 0 : newInit.root, win)
124
+ });
125
+ }, [
126
+ ltrRootMargin,
127
+ setObserverInit,
128
+ win
129
+ ]);
130
+ return {
131
+ setObserverList,
132
+ setObserverInit: setObserverInitExternal,
133
+ observer
50
134
  };
51
- }, [observerList, observerInit, callback]);
52
- return {
53
- setObserverList,
54
- setObserverInit,
55
- observer
56
- };
57
135
  };
58
- exports.useIntersectionObserver = useIntersectionObserver;
59
- //# sourceMappingURL=useIntersectionObserver.js.map
@@ -1 +1 @@
1
- {"version":3,"names":["React","require","react_utilities_1","useState","useRef","useIntersectionObserver","callback","options","observer","observerList","setObserverList","observerInit","setObserverInit","useIsomorphicLayoutEffect","current","IntersectionObserver","length","forEach","element","_a","observe","disconnect","exports"],"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,MAAAA,KAAA,gBAAAC,OAAA;AACA,MAAAC,iBAAA,gBAAAD,OAAA;AAEA,MAAM;EAAEE,QAAQ;EAAEC;AAAM,CAAE,GAAGJ,KAAK;AAElC;;;;;;;;;AAUO,MAAMK,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,iBAAA,CAAAW,yBAAyB,CAAC,MAAK;IAC7BL,QAAQ,CAACM,OAAO,GAAG,IAAIC,oBAAoB,CAACT,QAAQ,EAAEK,YAAY,CAAC;IAEnE;IACA,IAAIH,QAAQ,CAACM,OAAO,IAAIL,YAAY,IAAIA,YAAY,CAACO,MAAM,GAAG,CAAC,EAAE;MAC/DP,YAAY,CAACQ,OAAO,CAACC,OAAO,IAAG;;QAC7B,CAAAC,EAAA,GAAAX,QAAQ,CAACM,OAAO,cAAAK,EAAA,uBAAAA,EAAA,CAAEC,OAAO,CAACF,OAAO,CAAC;MACpC,CAAC,CAAC;;IAGJ;IACA,OAAO,MAAK;MACV,IAAIV,QAAQ,CAACM,OAAO,EAAE;QACpBN,QAAQ,CAACM,OAAO,CAACO,UAAU,EAAE;;IAEjC,CAAC;EACH,CAAC,EAAE,CAACZ,YAAY,EAAEE,YAAY,EAAEL,QAAQ,CAAC,CAAC;EAE1C,OAAO;IAAEI,eAAe;IAAEE,eAAe;IAAEJ;EAAQ,CAAE;AACvD,CAAC;AAzCYc,OAAA,CAAAjB,uBAAuB,GAAAA,uBAAA"}
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 */\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 */\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":["getRTLRootMargin","useIntersectionObserver","useCallback","useState","useRef","React","ltrRootMargin","target","win","newDir","getComputedStyle","direction","newMargin","splitMargins","split","length","callback","options","observer","observerList","setObserverList","targetDocument","useFluent","defaultView","observerInit","setObserverInit","rootMargin","root","mutationObserverCallback","mutationList","mutation","type","attributeName","contains","current","useMutationObserver","attributes","subtree","attributeFilter","useIsomorphicLayoutEffect","IntersectionObserver","forEach","element","observe","disconnect","setObserverInitExternal","newInit"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";;;;;;;;;;;IAaaA,gBAAAA;eAAAA;;IAsCAC,uBAAAA;eAAAA;;;;iEAnDU;gCACmB;qCACM;qCAGZ;AADpC,MAAM,EAAEC,WAAW,EAAEC,QAAQ,EAAEC,MAAM,EAAE,GAAGC;AASnC,MAAML,mBAAmB,CAC9B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hBhB,sBAAAA,KAAAA,IAAAA,KAAAA,IAAAA,kBAAkByB,OAAO,CAACD;YAC5B;QACF;QAEA,+CAA+C;QAC/C,OAAO;YACL,IAAIxB,SAASgB,OAAO,EAAE;gBACpBhB,SAASgB,OAAO,CAACU,UAAU;YAC7B;QACF;IACF,GAAG;QAACzB;QAAcK;QAAcR;QAAUR;KAAI;IAE9C,sEAAsE;IACtE,MAAMqC,0BAA0B3C,YAC9B,CAAC4C;YAEyBA;QADxB,+FAA+F;QAC/FxC,cAAc4B,OAAO,GAAGY,CAAAA,sBAAAA,YAAAA,QAAAA,YAAAA,KAAAA,IAAAA,KAAAA,IAAAA,QAASpB,UAAU,AAAVA,MAAU,QAAnBoB,wBAAAA,KAAAA,IAAAA,sBAAuB;QAE/C,iHAAiH;QACjHrB,gBAAgB;YACd,GAAGqB,OAAO;YACVpB,YAAY1B,iBAAiBM,cAAc4B,OAAO,EAAEY,YAAAA,QAAAA,YAAAA,KAAAA,IAAAA,KAAAA,IAAAA,QAASnB,IAAI,EAAanB;QAChF;IACF,GACA;QAACF;QAAemB;QAAiBjB;KAAI;IAGvC,OAAO;QAAEY;QAAiBK,iBAAiBoB;QAAyB3B;IAAS;AAC/E"}
@@ -0,0 +1,134 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ function _export(target, all) {
6
+ for(var name in all)Object.defineProperty(target, name, {
7
+ enumerable: true,
8
+ get: all[name]
9
+ });
10
+ }
11
+ _export(exports, {
12
+ createResizeObserverFromDocument: function() {
13
+ return createResizeObserverFromDocument;
14
+ },
15
+ useMeasureList: function() {
16
+ return useMeasureList;
17
+ }
18
+ });
19
+ const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
20
+ const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
21
+ const _reactsharedcontexts = require("@fluentui/react-shared-contexts");
22
+ function useMeasureList(currentIndex, refLength, totalLength, defaultItemSize) {
23
+ const widthArray = _react.useRef(new Array(totalLength).fill(defaultItemSize));
24
+ const heightArray = _react.useRef(new Array(totalLength).fill(defaultItemSize));
25
+ const refArray = _react.useRef([]);
26
+ const { targetDocument } = (0, _reactsharedcontexts.useFluent_unstable)();
27
+ // This lets us trigger updates when a size change occurs.
28
+ const sizeUpdateCount = _react.useRef(0);
29
+ // the handler for resize observer
30
+ const handleIndexUpdate = _react.useCallback((index)=>{
31
+ var _refArray_current_index;
32
+ let isChanged = false;
33
+ const boundClientRect = (_refArray_current_index = refArray.current[index]) === null || _refArray_current_index === void 0 ? void 0 : _refArray_current_index.getBoundingClientRect();
34
+ const containerWidth = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.width;
35
+ if (containerWidth !== widthArray.current[currentIndex + index]) {
36
+ isChanged = true;
37
+ }
38
+ widthArray.current[currentIndex + index] = containerWidth || defaultItemSize;
39
+ const containerHeight = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.height;
40
+ if (containerHeight !== heightArray.current[currentIndex + index]) {
41
+ isChanged = true;
42
+ }
43
+ heightArray.current[currentIndex + index] = containerHeight || defaultItemSize;
44
+ if (isChanged) {
45
+ sizeUpdateCount.current = sizeUpdateCount.current + 1;
46
+ }
47
+ }, [
48
+ currentIndex,
49
+ defaultItemSize,
50
+ sizeUpdateCount
51
+ ]);
52
+ const handleElementResizeCallback = (entries)=>{
53
+ for (const entry of entries){
54
+ const target = entry.target;
55
+ // Call the elements own resize handler (indexed)
56
+ target.handleResize();
57
+ }
58
+ };
59
+ _react.useEffect(()=>{
60
+ const newHeightLength = totalLength - heightArray.current.length;
61
+ const newWidthLength = totalLength - widthArray.current.length;
62
+ /* Ensure we grow or truncate arrays with prior properties,
63
+ keeping the existing values is important for whitespace assumptions.
64
+ Even if items in the 'middle' are deleted, we will recalc the whitespace as it is explored.*/ if (newWidthLength > 0) {
65
+ widthArray.current = widthArray.current.concat(new Array(newWidthLength).fill(defaultItemSize));
66
+ } else if (newWidthLength < 0) {
67
+ widthArray.current = widthArray.current.slice(0, totalLength);
68
+ }
69
+ if (newHeightLength > 0) {
70
+ heightArray.current = heightArray.current.concat(new Array(newHeightLength).fill(defaultItemSize));
71
+ } else if (newHeightLength < 0) {
72
+ heightArray.current = heightArray.current.slice(0, totalLength);
73
+ }
74
+ }, [
75
+ defaultItemSize,
76
+ totalLength
77
+ ]);
78
+ // Keep the reference of ResizeObserver as a ref, as it should live through renders
79
+ const resizeObserver = _react.useRef(createResizeObserverFromDocument(targetDocument, handleElementResizeCallback));
80
+ /* createIndexedRef provides a dynamic function to create an undefined number of refs at render time
81
+ * these refs then provide an indexed callback via attaching 'handleResize' to the element itself
82
+ * this function is then called on resize by handleElementResize and relies on indexing
83
+ * to track continuous sizes throughout renders while releasing all virtualized element refs each render cycle.
84
+ */ const createIndexedRef = _react.useCallback((index)=>{
85
+ const measureElementRef = (el)=>{
86
+ if (!targetDocument || !resizeObserver.current) {
87
+ return;
88
+ }
89
+ if (el) {
90
+ el.handleResize = ()=>{
91
+ handleIndexUpdate(index);
92
+ };
93
+ }
94
+ // cleanup previous container
95
+ if (refArray.current[index] !== undefined && refArray.current[index] !== null) {
96
+ resizeObserver.current.unobserve(refArray.current[index]);
97
+ }
98
+ refArray.current[index] = undefined;
99
+ if (el) {
100
+ refArray.current[index] = el;
101
+ resizeObserver.current.observe(el);
102
+ handleIndexUpdate(index);
103
+ }
104
+ };
105
+ return measureElementRef;
106
+ }, [
107
+ handleIndexUpdate,
108
+ resizeObserver,
109
+ targetDocument
110
+ ]);
111
+ _react.useEffect(()=>{
112
+ const _resizeObserver = resizeObserver;
113
+ return ()=>{
114
+ var _resizeObserver_current;
115
+ return (_resizeObserver_current = _resizeObserver.current) === null || _resizeObserver_current === void 0 ? void 0 : _resizeObserver_current.disconnect();
116
+ };
117
+ }, [
118
+ resizeObserver
119
+ ]);
120
+ return {
121
+ widthArray,
122
+ heightArray,
123
+ createIndexedRef,
124
+ refArray,
125
+ sizeUpdateCount: sizeUpdateCount.current
126
+ };
127
+ }
128
+ function createResizeObserverFromDocument(targetDocument, callback) {
129
+ var _targetDocument_defaultView;
130
+ if (!(targetDocument === null || targetDocument === void 0 ? void 0 : (_targetDocument_defaultView = targetDocument.defaultView) === null || _targetDocument_defaultView === void 0 ? void 0 : _targetDocument_defaultView.ResizeObserver)) {
131
+ return null;
132
+ }
133
+ return new targetDocument.defaultView.ResizeObserver(callback);
134
+ }
@@ -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\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 */\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 */\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":["createResizeObserverFromDocument","useMeasureList","currentIndex","refLength","totalLength","defaultItemSize","widthArray","React","useRef","Array","fill","heightArray","refArray","targetDocument","useFluent","sizeUpdateCount","handleIndexUpdate","useCallback","index","isChanged","boundClientRect","current","getBoundingClientRect","containerWidth","width","containerHeight","height","handleElementResizeCallback","entries","entry","target","handleResize","useEffect","newHeightLength","length","newWidthLength","concat","slice","resizeObserver","createIndexedRef","measureElementRef","el","undefined","unobserve","observe","_resizeObserver","disconnect","callback","defaultView","ResizeObserver"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";;;;;;;;;;;IAkIgBA,gCAAAA;eAAAA;;IArHAC,cAAAA;eAAAA;;;;iEAbO;qCACyB;AAYzC,SAASA,eAEdC,YAAoB,EAAEC,SAAiB,EAAEC,WAAmB,EAAEC,eAAuB;IACrF,MAAMC,aAAaC,OAAMC,MAAM,CAAC,IAAIC,MAAML,aAAaM,IAAI,CAACL;IAC5D,MAAMM,cAAcJ,OAAMC,MAAM,CA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H;gBAC/BzB,kBAAkBE;YACpB;QACF;QAEA,OAAOsB;IACT,GACA;QAACxB;QAAmBsB;QAAgBzB;KAAe;IAGrDN,OAAMyB,SAAS,CAAC;QACd,MAAMa,kBAAkBP;QACxB,OAAO;gBAAMO;mBAAAA,CAAAA,0BAAAA,gBAAgBxB,OAAO,AAAPA,MAAO,QAAvBwB,4BAAAA,KAAAA,IAAAA,KAAAA,IAAAA,wBAAyBC,UAAU;;IAClD,GAAG;QAACR;KAAe;IAEnB,OAAO;QAAEhC;QAAYK;QAAa4B;QAAkB3B;QAAUG,iBAAiBA,gBAAgBM,OAAO;IAAC;AACzG;AASO,SAASrB,iCACda,cAA2C,EAC3CkC,QAAgC;QAE3BlC;IAAL,IAAI,CAACA,CAAAA,mBAAAA,QAAAA,mBAAAA,KAAAA,IAAAA,KAAAA,IAAAA,CAAAA,8BAAAA,eAAgBmC,WAAW,AAAXA,MAAW,QAA3BnC,gCAAAA,KAAAA,IAAAA,KAAAA,IAAAA,4BAA6BoC,cAAc,AAAdA,GAAgB;QAChD,OAAO;IACT;IAEA,OAAO,IAAIpC,eAAemC,WAAW,CAACC,cAAc,CAACF;AACvD"}
@@ -0,0 +1,48 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useMutationObserver", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useMutationObserver;
9
+ }
10
+ });
11
+ const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
12
+ const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
13
+ const _reactsharedcontexts = require("@fluentui/react-shared-contexts");
14
+ const { useRef, useEffect } = _react;
15
+ const useMutationObserver = (target, callback, options)=>{
16
+ 'use no memo';
17
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
18
+ // eslint-disable-next-line no-restricted-globals
19
+ const observer = useRef();
20
+ const { targetDocument } = (0, _reactsharedcontexts.useFluent_unstable)();
21
+ const win = targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView;
22
+ useEffect(()=>{
23
+ if (!win) {
24
+ return;
25
+ }
26
+ // Create an observer instance linked to the callback function
27
+ observer.current = new win.MutationObserver(callback);
28
+ }, [
29
+ callback,
30
+ win
31
+ ]);
32
+ useEffect(()=>{
33
+ if (target) {
34
+ var _observer_current;
35
+ (_observer_current = observer.current) === null || _observer_current === void 0 ? void 0 : _observer_current.observe(target, options);
36
+ }
37
+ return ()=>{
38
+ var _observer_current;
39
+ (_observer_current = observer.current) === null || _observer_current === void 0 ? void 0 : _observer_current.disconnect();
40
+ };
41
+ }, [
42
+ target,
43
+ options
44
+ ]);
45
+ return {
46
+ observer
47
+ };
48
+ };
@@ -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\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":["useMutationObserver","useRef","useEffect","React","target","callback","options","observer","targetDocument","useFluent","win","defaultView","current","MutationObserver","observe","disconnect"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";;;;+BAKaA;;;eAAAA;;;;iEALU;qCACyB;AAEhD,MAAM,EAAEC,MAAM,EAAEC,SAAS,EAAE,GAAGC;AAEvB,MAAMH,sBAAsB,CACjCI,QACAC,UACAC;IAIA;IAEA,8FAA8F;IAC9F,iDAAiD;IACjD,MAAMC,WAAWN;IACjB,MAAM,EAAEO,cAAc,EAAE,GAAGC,IAAAA,uCAAAA;IAC3B,MAAMC,MAAMF,mBAAAA,QAAAA,mBAAAA,KAAAA,IAAAA,KAAAA,IAAAA,eAAgBG,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;gBAEVG;YAAAA,CAAAA,oBAAAA,SAASK,OAAO,AAAPA,MAAO,QAAhBL,sBAAAA,KAAAA,IAAAA,KAAAA,IAAAA,kBAAkBO,OAAO,CAACV,QAAQE;QACpC;QAEA,OAAO;gBACLC;YAAAA,CAAAA,oBAAAA,SAASK,OAAO,AAAPA,MAAO,QAAhBL,sBAAAA,KAAAA,IAAAA,KAAAA,IAAAA,kBAAkBQ,UAAU;QAC9B;IACF,GAAG;QAACX;QAAQE;KAAQ;IAEpB,OAAO;QAAEC;IAAS;AACpB"}