@fluentui/react-virtualizer 9.0.0-alpha.8 → 9.0.0-alpha.81

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 +754 -8
  2. package/dist/index.d.ts +248 -30
  3. package/lib/Hooks.js +0 -1
  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 +0 -1
  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 +5 -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 +1 -2
  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 +439 -355
  22. package/lib/components/Virtualizer/useVirtualizer.js.map +1 -1
  23. package/lib/components/Virtualizer/{useVirtualizerStyles.js → useVirtualizerStyles.styles.js} +5 -3
  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 +5 -0
  30. package/lib/components/VirtualizerScrollView/index.js.map +1 -0
  31. package/lib/components/VirtualizerScrollView/renderVirtualizerScrollView.js +9 -0
  32. package/lib/components/VirtualizerScrollView/renderVirtualizerScrollView.js.map +1 -0
  33. package/lib/components/VirtualizerScrollView/useVirtualizerScrollView.js +81 -0
  34. package/lib/components/VirtualizerScrollView/useVirtualizerScrollView.js.map +1 -0
  35. package/lib/components/VirtualizerScrollView/useVirtualizerScrollViewStyles.styles.js +47 -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 +5 -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 +149 -0
  46. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js.map +1 -0
  47. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js +47 -0
  48. package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamicStyles.styles.js.map +1 -0
  49. package/lib/hooks/hooks.types.js +4 -0
  50. package/lib/hooks/hooks.types.js.map +1 -0
  51. package/lib/hooks/index.js +4 -1
  52. package/lib/hooks/index.js.map +1 -1
  53. package/lib/hooks/useDynamicPagination.js +127 -0
  54. package/lib/hooks/useDynamicPagination.js.map +1 -0
  55. package/lib/hooks/useDynamicVirtualizerMeasure.js +101 -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 +116 -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 +50 -0
  64. package/lib/hooks/useResizeObserverRef.js.map +1 -0
  65. package/lib/hooks/useStaticPagination.js +103 -0
  66. package/lib/hooks/useStaticPagination.js.map +1 -0
  67. package/lib/hooks/useVirtualizerMeasure.js +46 -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 +3 -0
  78. package/lib/utilities/ImperativeScrolling/index.js.map +1 -0
  79. package/lib/utilities/VirtualizerContext/VirtualizerContext.js +33 -0
  80. package/lib/utilities/VirtualizerContext/VirtualizerContext.js.map +1 -0
  81. package/lib/utilities/VirtualizerContext/index.js +2 -0
  82. package/lib/utilities/VirtualizerContext/index.js.map +1 -0
  83. package/lib/utilities/VirtualizerContext/types.js +3 -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 +3 -5
  92. package/lib-commonjs/Hooks.js.map +1 -1
  93. package/lib-commonjs/Utilities.js +6 -0
  94. package/lib-commonjs/Utilities.js.map +1 -0
  95. package/lib-commonjs/Virtualizer.js +3 -5
  96. package/lib-commonjs/Virtualizer.js.map +1 -1
  97. package/lib-commonjs/VirtualizerScrollView.js +6 -0
  98. package/lib-commonjs/VirtualizerScrollView.js.map +1 -0
  99. package/lib-commonjs/VirtualizerScrollViewDynamic.js +6 -0
  100. package/lib-commonjs/VirtualizerScrollViewDynamic.js.map +1 -0
  101. package/lib-commonjs/components/Virtualizer/Virtualizer.js +15 -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 +7 -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 +447 -361
  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 +4 -0
  116. package/lib-commonjs/components/VirtualizerScrollView/VirtualizerScrollView.types.js.map +1 -0
  117. package/lib-commonjs/components/VirtualizerScrollView/index.js +10 -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 +4 -0
  128. package/lib-commonjs/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.types.js.map +1 -0
  129. package/lib-commonjs/components/VirtualizerScrollViewDynamic/index.js +10 -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 +158 -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 +7 -0
  138. package/lib-commonjs/hooks/hooks.types.js.map +1 -0
  139. package/lib-commonjs/hooks/index.js +7 -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 +108 -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 +122 -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 +59 -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 +54 -0
  156. package/lib-commonjs/hooks/useVirtualizerMeasure.js.map +1 -0
  157. package/lib-commonjs/index.js +82 -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 +4 -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 +8 -0
  166. package/lib-commonjs/utilities/ImperativeScrolling/index.js.map +1 -0
  167. package/lib-commonjs/utilities/VirtualizerContext/VirtualizerContext.js +54 -0
  168. package/lib-commonjs/utilities/VirtualizerContext/VirtualizerContext.js.map +1 -0
  169. package/lib-commonjs/utilities/VirtualizerContext/index.js +7 -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 +7 -0
  178. package/lib-commonjs/utilities/index.js.map +1 -0
  179. package/package.json +22 -13
  180. package/CHANGELOG.json +0 -125
  181. package/lib/components/Virtualizer/useVirtualizerStyles.js.map +0 -1
  182. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.js +0 -115
  183. package/lib-commonjs/components/Virtualizer/useVirtualizerStyles.js.map +0 -1
@@ -1,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,"mappings":";;;;;;AACA;AACA;AAEA,MAAM;EAAEA,QAAQ;EAAEC;AAAM,CAAE,GAAGC,KAAK;AAElC;;;;;;;;;AAUO,MAAMC,uBAAuB,GAAG,CACrCC,QAAsC,EACtCC,OAAkC,KAKhC;EACF;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,MAAMC,QAAQ,GAAGL,MAAM,EAAwB;EAC/C,MAAM,CAACM,YAAY,EAAEC,eAAe,CAAC,GAAGR,QAAQ,EAAa;EAC7D,MAAM,CAACS,YAAY,EAAEC,eAAe,CAAC,GAAGV,QAAQ,CAAuCK,OAAO,CAAC;EAE/F;EACA;EACAM,2CAAyB,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,cAAQ,CAACJ,OAAO,0CAAEK,OAAO,CAACD,OAAO,CAAC;MACpC,CAAC,CAAC;;IAGJ;IACA,OAAO,MAAK;MACV,IAAIV,QAAQ,CAACM,OAAO,EAAE;QACpBN,QAAQ,CAACM,OAAO,CAACM,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;AAzCYa,+BAAuB","names":["useState","useRef","React","useIntersectionObserver","callback","options","observer","observerList","setObserverList","observerInit","setObserverInit","react_utilities_1","current","IntersectionObserver","length","forEach","element","observe","disconnect","exports"],"sourceRoot":"../src/","sources":["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"]}
1
+ {"version":3,"sources":["useIntersectionObserver.ts"],"sourcesContent":["import type { Dispatch, MutableRefObject, SetStateAction } from 'react';\nimport * as React from 'react';\nimport { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\n\nconst { useCallback, useState, useRef } = React;\nimport { useMutationObserver } from './useMutationObserver';\n\n/**\n * This function will take the rootMargin and flip the sides if we are in RTL based on the computed reading direction of the target element.\n * @param ltrRootMargin the margin to be processed and flipped if required\n * @param target target element that will have its current reading direction determined\n * @returns the corrected rootMargin (if it was necessary to correct)\n */\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: Dispatch<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: 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":";;;;;;;;;;;IAcaA,gBAAAA;eAAAA;;IAsCAC,uBAAAA;eAAAA;;;;iEAnDU;gCACmB;qCACM;qCAGZ;AADpC,MAAM,EAA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CAACC,CAAAA;oBACnBxB;gBAAAA,CAAAA,oBAAAA,SAASgB,OAAO,AAAPA,MAAO,QAAhBhB,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,122 @@
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 = (0, _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
+ widthArray.current = new Array(totalLength).fill(defaultItemSize);
61
+ heightArray.current = new Array(totalLength).fill(defaultItemSize);
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
+ function createResizeObserverFromDocument(targetDocument, callback) {
117
+ var _targetDocument_defaultView;
118
+ if (!(targetDocument === null || targetDocument === void 0 ? void 0 : (_targetDocument_defaultView = targetDocument.defaultView) === null || _targetDocument_defaultView === void 0 ? void 0 : _targetDocument_defaultView.ResizeObserver)) {
119
+ return null;
120
+ }
121
+ return new targetDocument.defaultView.ResizeObserver(callback);
122
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["useMeasureList.ts"],"sourcesContent":["import * as React from 'react';\nimport { useRef } 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 = 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 widthArray.current = new Array(totalLength).fill(defaultItemSize);\n heightArray.current = new Array(totalLength).fill(defaultItemSize);\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","resizeObserver","createIndexedRef","measureElementRef","el","undefined","unobserve","observe","_resizeObserver","disconnect","callback","defaultView","ResizeObserver"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";;;;;;;;;;;IAsHgBA,gCAAAA;eAAAA;;IAxGAC,cAAAA;eAAAA;;;;iEAdO;qCAEyB;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,CAAC,IAAIC,MAAML,aAAaM,IAAI,CAACL;IAE7D,MAAMO,WAAWL,OAAMC,MAAM,CAAqC,EAAE;IACpE,MAAM,EAAEK,cAAc,EAAE,GAAGC,IAAAA,uCAAAA;IAE3B,0DAA0D;IAC1D,MAAMC,kBAAkBP,IAAAA,aAAAA,EAAO;IAE/B,kCAAkC;IAClC,MAAMQ,oBAAoBT,OAAMU,WAAW,CACzC,CAACC;YAEyBN;QADxB,IAAIO,YAAY;QAChB,MAAMC,kBAAAA,AAAkBR,CAAAA,0BAAAA,SAASS,OAAO,CAACH,MAAM,AAANA,MAAM,QAAvBN,4BAAAA,KAAAA,IAAAA,KAAAA,IAAAA,wBAAyBU,qBAAqB;QACtE,MAAMC,iBAAiBH,oBAAAA,QAAAA,oBAAAA,KAAAA,IAAAA,KAAAA,IAAAA,gBAAiBI,KAAK;QAC7C,IAAID,mBAAmBjB,WAAWe,OAAO,CAACnB,eAAegB,MAAM,EAAE;YAC/DC,YAAY;QACd;QACAb,WAAWe,OAAO,CAACnB,eAAegB,MAAM,GAA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AAID,IAAI;gBACNxB,SAASS,OAAO,CAACH,MAAM,GAAGkB;gBAC1BH,eAAeZ,OAAO,CAACkB,OAAO,CAACH;gBAC/BpB,kBAAkBE;YACpB;QACF;QAEA,OAAOiB;IACT,GACA;QAACnB;QAAmBiB;QAAgBpB;KAAe;IAGrDN,OAAMyB,SAAS,CAAC;QACd,MAAMQ,kBAAkBP;QACxB,OAAO;gBAAMO;mBAAAA,CAAAA,0BAAAA,gBAAgBnB,OAAO,AAAPA,MAAO,QAAvBmB,4BAAAA,KAAAA,IAAAA,KAAAA,IAAAA,wBAAyBC,UAAU;;IAClD,GAAG;QAACR;KAAe;IAEnB,OAAO;QAAE3B;QAAYK;QAAauB;QAAkBtB;QAAUG,iBAAiBA,gBAAgBM,OAAO;IAAC;AACzG;AASO,SAASrB,iCACda,cAA2C,EAC3C6B,QAAgC;QAE3B7B;IAAL,IAAI,CAACA,CAAAA,mBAAAA,QAAAA,mBAAAA,KAAAA,IAAAA,KAAAA,IAAAA,CAAAA,8BAAAA,eAAgB8B,WAAW,AAAXA,MAAW,QAA3B9B,gCAAAA,KAAAA,IAAAA,KAAAA,IAAAA,4BAA6B+B,cAAc,AAAdA,GAAgB;QAChD,OAAO;IACT;IAEA,OAAO,IAAI/B,eAAe8B,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":["useMutationObserver.ts"],"sourcesContent":["import type { MutableRefObject } from 'react';\nimport * 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: 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":";;;;+BAMaA;;;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"}
@@ -0,0 +1,59 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useResizeObserverRef_unstable", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useResizeObserverRef_unstable;
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 _debounce = require("../utilities/debounce");
15
+ const _createResizeObserverFromDocument = require("../utilities/createResizeObserverFromDocument");
16
+ const useResizeObserverRef_unstable = (resizeCallback)=>{
17
+ 'use no memo';
18
+ const { targetDocument } = (0, _reactsharedcontexts.useFluent_unstable)();
19
+ const container = _react.useRef(null);
20
+ // the handler for resize observer
21
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
22
+ // eslint-disable-next-line no-restricted-globals
23
+ const handleResize = (0, _debounce.debounce)((entries, observer)=>{
24
+ resizeCallback(entries, observer, container);
25
+ });
26
+ // Keep the reference of ResizeObserver in the state, as it should live through renders
27
+ const [resizeObserver, setResizeObserver] = _react.useState(()=>(0, _createResizeObserverFromDocument.createResizeObserverFromDocument)(targetDocument, handleResize));
28
+ _react.useEffect(()=>{
29
+ // Update our state when resizeCallback changes
30
+ container.current = null;
31
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
32
+ setResizeObserver(()=>(0, _createResizeObserverFromDocument.createResizeObserverFromDocument)(targetDocument, handleResize));
33
+ // eslint-disable-next-line react-hooks/exhaustive-deps
34
+ }, [
35
+ resizeCallback,
36
+ targetDocument
37
+ ]);
38
+ _react.useEffect(()=>{
39
+ return ()=>{
40
+ container.current = null;
41
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
42
+ };
43
+ // eslint-disable-next-line react-hooks/exhaustive-deps
44
+ }, []);
45
+ const scrollRef = _react.useCallback((instance)=>{
46
+ if (container.current !== instance) {
47
+ if (container.current) {
48
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.unobserve(container.current);
49
+ }
50
+ container.current = instance;
51
+ if (container.current) {
52
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.observe(container.current);
53
+ }
54
+ }
55
+ }, [
56
+ resizeObserver
57
+ ]);
58
+ return scrollRef;
59
+ };
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["useResizeObserverRef.ts"],"sourcesContent":["import * as React from 'react';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\nimport { debounce } from '../utilities/debounce';\nimport { createResizeObserverFromDocument } from '../utilities/createResizeObserverFromDocument';\nimport { ResizeCallbackWithRef } from './hooks.types';\n\n/**\n * useResizeObserverRef_unstable simplifies resize observer connection and ensures debounce/cleanup\n */\nexport const useResizeObserverRef_unstable = (resizeCallback: ResizeCallbackWithRef) => {\n 'use no memo';\n\n const { targetDocument } = useFluent();\n const container = React.useRef<HTMLElement | null>(null);\n // the handler for resize observer\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 handleResize = debounce((entries: ResizeObserverEntry[], observer: ResizeObserver) => {\n resizeCallback(entries, observer, container);\n });\n\n // Keep the reference of ResizeObserver in the state, as it should live through renders\n const [resizeObserver, setResizeObserver] = React.useState(() =>\n createResizeObserverFromDocument(targetDocument, handleResize),\n );\n\n React.useEffect(() => {\n // Update our state when resizeCallback changes\n container.current = null;\n resizeObserver?.disconnect();\n setResizeObserver(() => createResizeObserverFromDocument(targetDocument, handleResize));\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, [resizeCallback, targetDocument]);\n\n React.useEffect(() => {\n return () => {\n container.current = null;\n resizeObserver?.disconnect();\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n const scrollRef = React.useCallback(\n (instance: HTMLElement | HTMLDivElement | null) => {\n if (container.current !== instance) {\n if (container.current) {\n resizeObserver?.unobserve(container.current);\n }\n\n container.current = instance;\n if (container.current) {\n resizeObserver?.observe(container.current);\n }\n }\n },\n [resizeObserver],\n );\n\n return scrollRef;\n};\n"],"names":["useResizeObserverRef_unstable","resizeCallback","targetDocument","useFluent","container","React","useRef","handleResize","debounce","entries","observer","resizeObserver","setResizeObserver","useState","createResizeObserverFromDocument","useEff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AAO,GAAGI;YACpB,IAAIhB,UAAUY,OAAO,EAAE;gBACrBL,mBAAAA,QAAAA,mBAAAA,KAAAA,IAAAA,KAAAA,IAAAA,eAAgBW,OAAO,CAAClB,UAAUY,OAAO;YAC3C;QACF;IACF,GACA;QAACL;KAAe;IAGlB,OAAOO;AACT"}
@@ -0,0 +1,107 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", {
3
+ value: true
4
+ });
5
+ Object.defineProperty(exports, "useStaticVirtualizerPagination", {
6
+ enumerable: true,
7
+ get: function() {
8
+ return useStaticVirtualizerPagination;
9
+ }
10
+ });
11
+ const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
12
+ const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
13
+ const _reactutilities = require("@fluentui/react-utilities");
14
+ const useStaticVirtualizerPagination = (virtualizerProps, paginationEnabled = true)=>{
15
+ 'use no memo';
16
+ const { itemSize, axis = 'vertical' } = virtualizerProps;
17
+ const [setScrollTimer, clearScrollTimer] = (0, _reactutilities.useTimeout)();
18
+ const lastScrollPos = (0, _react.useRef)(0);
19
+ const lastIndexScrolled = (0, _react.useRef)(0);
20
+ const scrollContainer = _react.useRef(null);
21
+ const clearListeners = ()=>{
22
+ if (scrollContainer.current) {
23
+ scrollContainer.current.removeEventListener('scroll', onScroll);
24
+ scrollContainer.current = null;
25
+ clearScrollTimer();
26
+ }
27
+ };
28
+ _react.useEffect(()=>{
29
+ return ()=>{
30
+ clearListeners();
31
+ };
32
+ // eslint-disable-next-line react-hooks/exhaustive-deps
33
+ }, []);
34
+ /**
35
+ * Handle scroll stop event and paginate to the closest item
36
+ * If the closest item is the same as the previous scroll end
37
+ * we paginate to the next/previous one based on direction
38
+ */ const onScrollEnd = _react.useCallback(()=>{
39
+ if (!scrollContainer.current || !paginationEnabled) {
40
+ // No container found
41
+ return;
42
+ }
43
+ const currentScrollPos = Math.round(axis === 'vertical' ? scrollContainer.current.scrollTop : scrollContainer.current.scrollLeft);
44
+ const closestItem = Math.round(currentScrollPos / itemSize);
45
+ let nextItem = 0;
46
+ if (Math.round(closestItem - lastIndexScrolled.current) === 0) {
47
+ // Special case for go to next/previous with minimum amount of scroll needed
48
+ const nextTarget = lastScrollPos.current < currentScrollPos ? 1 : -1;
49
+ const isSecondaryScroll = lastScrollPos.current === currentScrollPos;
50
+ const posMod = isSecondaryScroll ? 0 : nextTarget;
51
+ nextItem = closestItem + posMod;
52
+ } else {
53
+ // Pagination for anything else can just jump to the closest!
54
+ nextItem = closestItem;
55
+ }
56
+ const nextItemPos = nextItem * itemSize;
57
+ if (axis === 'vertical') {
58
+ scrollContainer.current.scrollTo({
59
+ top: nextItemPos,
60
+ behavior: 'smooth'
61
+ });
62
+ } else {
63
+ scrollContainer.current.scrollTo({
64
+ left: nextItemPos,
65
+ behavior: 'smooth'
66
+ });
67
+ }
68
+ lastScrollPos.current = nextItemPos;
69
+ lastIndexScrolled.current = nextItem;
70
+ }, [
71
+ paginationEnabled,
72
+ axis,
73
+ itemSize
74
+ ]);
75
+ /**
76
+ * On scroll timer that will continuously delay callback until scrolling stops
77
+ */ const onScroll = _react.useCallback((event)=>{
78
+ clearScrollTimer();
79
+ setScrollTimer(onScrollEnd, 100);
80
+ }, [
81
+ onScrollEnd,
82
+ clearScrollTimer,
83
+ setScrollTimer
84
+ ]);
85
+ /**
86
+ * Pagination ref will ensure we attach listeners to containers on change
87
+ * It is returned from hook and merged into the scroll container externally
88
+ */ const paginationRef = _react.useCallback((instance)=>{
89
+ if (!paginationEnabled) {
90
+ clearListeners();
91
+ scrollContainer.current = null;
92
+ return;
93
+ }
94
+ if (scrollContainer.current !== instance) {
95
+ clearListeners();
96
+ scrollContainer.current = instance;
97
+ if (scrollContainer.current) {
98
+ scrollContainer.current.addEventListener('scroll', onScroll);
99
+ }
100
+ }
101
+ }, [
102
+ onScroll,
103
+ onScrollEnd,
104
+ paginationEnabled
105
+ ]);
106
+ return paginationRef;
107
+ };
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["useStaticPagination.ts"],"sourcesContent":["import * as React from 'react';\nimport { VirtualizerStaticPaginationProps } from './hooks.types';\nimport { useRef } from 'react';\nimport { useTimeout } from '@fluentui/react-utilities';\n\n/**\n * Optional hook that will enable pagination on the virtualizer so that it 'autoscrolls' to an items exact position\n * Sizes are uniform/static, we round to the nearest item on long scrolls\n * On short scrolls, we will go at minimum to the next/previous item so that arrow pagination works\n * All VirtualizerStaticPaginationProps can be grabbed from Virtualizer hooks externally and passed in\n */\nexport const useStaticVirtualizerPagination = (\n virtualizerProps: VirtualizerStaticPaginationProps,\n paginationEnabled: Boolean = true,\n) => {\n 'use no memo';\n\n const { itemSize, axis = 'vertical' } = virtualizerProps;\n\n const [setScrollTimer, clearScrollTimer] = useTimeout();\n const lastScrollPos = useRef<number>(0);\n const lastIndexScrolled = useRef<number>(0);\n\n const scrollContainer = React.useRef<HTMLElement | null>(null);\n\n const clearListeners = () => {\n if (scrollContainer.current) {\n scrollContainer.current.removeEventListener('scroll', onScroll);\n\n scrollContainer.current = null;\n clearScrollTimer();\n }\n };\n\n React.useEffect(() => {\n return () => {\n clearListeners();\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n /**\n * Handle scroll stop event and paginate to the closest item\n * If the closest item is the same as the previous scroll end\n * we paginate to the next/previous one based on direction\n */\n const onScrollEnd = React.useCallback(() => {\n if (!scrollContainer.current || !paginationEnabled) {\n // No container found\n return;\n }\n\n const currentScrollPos = Math.round(\n axis === 'vertical' ? scrollContainer.current.scrollTop : scrollContainer.current.scrollLeft,\n );\n const closestItem = Math.round(currentScrollPos / itemSize);\n\n let nextItem = 0;\n if (Math.round(closestItem - lastIndexScrolled.current) === 0) {\n // Special case for go to next/previous with minimum amount of scroll needed\n const nextTarget = lastScrollPos.current < currentScrollPos ? 1 : -1;\n const isSecondaryScroll = lastScrollPos.current === currentScrollPos;\n const posMod = isSecondaryScroll ? 0 : nextTarget;\n\n nextItem = closestItem + posMod;\n } else {\n // Pagination for anything else can just jump to the closest!\n nextItem = closestItem;\n }\n\n const nextItemPos = nextItem * itemSize;\n\n if (axis === 'vertical') {\n scrollContainer.current.scrollTo({ top: nextItemPos, behavior: 'smooth' });\n } else {\n scrollContainer.current.scrollTo({ left: nextItemPos, behavior: 'smooth' });\n }\n lastScrollPos.current = nextItemPos;\n lastIndexScrolled.current = nextItem;\n }, [paginationEnabled, axis, itemSize]);\n\n /**\n * On scroll timer that will continuously delay callback until scrolling stops\n */\n const onScroll = React.useCallback(\n event => {\n clearScrollTimer();\n setScrollTimer(onScrollEnd, 100);\n },\n [onScrollEnd, clearScrollTimer, setScrollTimer],\n );\n\n /**\n * Pagination ref will ensure we attach listeners to containers on change\n * It is returned from hook and merged into the scroll container externally\n */\n const paginationRef = React.useCallback(\n (instance: HTMLElement | HTMLDivElement | null) => {\n if (!paginationEnabled) {\n clearListeners();\n scrollContainer.current = null;\n return;\n }\n if (scrollContainer.current !== instance) {\n clearListeners();\n\n scrollContainer.current = instance;\n if (scrollContainer.current) {\n scrollContainer.current.addEventListener('scroll', onScroll);\n }\n }\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [onScroll, onScrollEnd, paginationEnabled],\n );\n\n return paginationRef;\n};\n"],"names":["useStaticVirtualizerPagination","virtualizerProps","paginationEnabled","itemSize","axis","setScrollTimer","clearScrollTimer","useTimeout","lastScrollPos","useRef","lastIndexScrolled","scrollContainer","React","clearListeners","current","removeEventListener","onScroll","useEffect","onScrollEnd","useCallback","currentScrollPos","Math","round","scrollTop","scrollLeft","closestItem","nextItem","nextTarget","isSecondaryScroll","posMod","nextItemPos","scrollTo","top","behavior","left","event","paginationRef","instance","addEventListener"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":";;;;+BAWaA;;;eAAAA;;;;iEAXU;gCAGI;AAQp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