@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,9 +1,32 @@
1
1
  import * as React from 'react';
2
2
  import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
3
- const {
4
- useState,
5
- useRef
6
- } = React;
3
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
4
+ const { useCallback, useState, useRef } = React;
5
+ import { useMutationObserver } from './useMutationObserver';
6
+ /**
7
+ * This function will take the rootMargin and flip the sides if we are in RTL based on the computed reading direction of the target element.
8
+ * @param ltrRootMargin the margin to be processed and flipped if required
9
+ * @param target target element that will have its current reading direction determined
10
+ * @returns the corrected rootMargin (if it was necessary to correct)
11
+ */ export const getRTLRootMargin = (ltrRootMargin, target, win)=>{
12
+ if (target && win) {
13
+ // get the computed dir for the target element
14
+ const newDir = win.getComputedStyle(target).direction;
15
+ // If we're in rtl reading direction, we might need to flip the margins on the left/right sides
16
+ if (newDir === 'rtl') {
17
+ let newMargin = ltrRootMargin;
18
+ const splitMargins = ltrRootMargin.split(' ');
19
+ // We only need to do this if we get four values, otherwise the sides are equal and don't require flipping.
20
+ if (splitMargins.length === 4) {
21
+ newMargin = `${splitMargins[0]} ${splitMargins[3]} ${splitMargins[2]} ${splitMargins[1]}`;
22
+ }
23
+ return newMargin;
24
+ } else {
25
+ return ltrRootMargin;
26
+ }
27
+ }
28
+ return ltrRootMargin;
29
+ };
7
30
  /**
8
31
  * React hook that allows easy usage of the browser API IntersectionObserver within React
9
32
  * @param callback - A function called when the percentage of the target element is visible crosses a threshold.
@@ -12,41 +35,95 @@ const {
12
35
  * enough to trigger a callback).
13
36
  * @returns An array containing a callback to update the list of Elements the observer should listen to, a callback to
14
37
  * update the init options of the IntersectionObserver and a ref to the IntersectionObserver instance itself.
15
- */
16
- export const useIntersectionObserver = (callback, options) => {
17
- // export const useIntersectionObserver = (
18
- // callback: IntersectionObserverCallback,
19
- // options?: IntersectionObserverInit,
20
- // ): [
21
- // Dispatch<SetStateAction<Element[] | undefined>>,
22
- // Dispatch<SetStateAction<IntersectionObserverInit | undefined>>,
23
- // MutableRefObject<IntersectionObserver | undefined>,
24
- // ] => {
25
- const observer = useRef();
26
- const [observerList, setObserverList] = useState();
27
- const [observerInit, setObserverInit] = useState(options);
28
- // Observer elements in passed in list and clean up previous list
29
- // This effect is only triggered when observerList is updated
30
- useIsomorphicLayoutEffect(() => {
31
- observer.current = new IntersectionObserver(callback, observerInit);
32
- // If we have an instance of IO and a list with elements, observer the elements
33
- if (observer.current && observerList && observerList.length > 0) {
34
- observerList.forEach(element => {
35
- var _a;
36
- (_a = observer.current) === null || _a === void 0 ? void 0 : _a.observe(element);
37
- });
38
- }
39
- // clean up previous elements being listened to
40
- return () => {
41
- if (observer.current) {
42
- observer.current.disconnect();
43
- }
38
+ */ export const useIntersectionObserver = (callback, options)=>{
39
+ 'use no memo';
40
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
41
+ // eslint-disable-next-line no-restricted-globals
42
+ const observer = useRef();
43
+ const [observerList, setObserverList] = useState();
44
+ const { targetDocument } = useFluent();
45
+ const win = targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView;
46
+ var _options_rootMargin;
47
+ // set the initial init with corrected margins based on the observed root's calculated reading direction.
48
+ const [observerInit, setObserverInit] = useState(options && {
49
+ ...options,
50
+ rootMargin: getRTLRootMargin((_options_rootMargin = options.rootMargin) !== null && _options_rootMargin !== void 0 ? _options_rootMargin : '0px', options.root, win)
51
+ });
52
+ var _options_rootMargin1;
53
+ // 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.
54
+ const ltrRootMargin = useRef((_options_rootMargin1 = options === null || options === void 0 ? void 0 : options.rootMargin) !== null && _options_rootMargin1 !== void 0 ? _options_rootMargin1 : '0px');
55
+ // Callback function to execute when mutations are observed
56
+ const mutationObserverCallback = useCallback((mutationList)=>{
57
+ for (const mutation of mutationList){
58
+ // Ensuring that the right attribute is being observed and that the root is within the tree of the element being mutated.
59
+ 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)) {
60
+ setObserverInit({
61
+ ...observerInit,
62
+ rootMargin: getRTLRootMargin(ltrRootMargin.current, observerInit === null || observerInit === void 0 ? void 0 : observerInit.root, win)
63
+ });
64
+ }
65
+ }
66
+ }, [
67
+ ltrRootMargin,
68
+ observerInit,
69
+ options === null || options === void 0 ? void 0 : options.root,
70
+ win
71
+ ]);
72
+ // Mutation observer for dir attribute changes in the document
73
+ useMutationObserver(targetDocument, mutationObserverCallback, {
74
+ attributes: true,
75
+ subtree: true,
76
+ attributeFilter: [
77
+ 'dir'
78
+ ]
79
+ });
80
+ // Observer elements in passed in list and clean up previous list
81
+ // This effect is only triggered when observerList is updated
82
+ useIsomorphicLayoutEffect(()=>{
83
+ if (!win) {
84
+ return;
85
+ }
86
+ observer.current = new win.IntersectionObserver(callback, {
87
+ ...observerInit,
88
+ rootMargin: getRTLRootMargin(ltrRootMargin.current, observerInit === null || observerInit === void 0 ? void 0 : observerInit.root, win)
89
+ });
90
+ // If we have an instance of IO and a list with elements, observer the elements
91
+ if (observer.current && observerList && observerList.length > 0) {
92
+ observerList.forEach((element)=>{
93
+ var _observer_current;
94
+ (_observer_current = observer.current) === null || _observer_current === void 0 ? void 0 : _observer_current.observe(element);
95
+ });
96
+ }
97
+ // clean up previous elements being listened to
98
+ return ()=>{
99
+ if (observer.current) {
100
+ observer.current.disconnect();
101
+ }
102
+ };
103
+ }, [
104
+ observerList,
105
+ observerInit,
106
+ callback,
107
+ win
108
+ ]);
109
+ // Do not use internally, we need to track external settings only here
110
+ const setObserverInitExternal = useCallback((newInit)=>{
111
+ var _newInit_rootMargin;
112
+ // Since we know this is coming from consumers, we can store this value as LTR somewhat safely.
113
+ ltrRootMargin.current = (_newInit_rootMargin = newInit === null || newInit === void 0 ? void 0 : newInit.rootMargin) !== null && _newInit_rootMargin !== void 0 ? _newInit_rootMargin : '0px';
114
+ // Call the internal setter to update the value and ensure if our calculated direction is rtl, we flip the margin
115
+ setObserverInit({
116
+ ...newInit,
117
+ rootMargin: getRTLRootMargin(ltrRootMargin.current, newInit === null || newInit === void 0 ? void 0 : newInit.root, win)
118
+ });
119
+ }, [
120
+ ltrRootMargin,
121
+ setObserverInit,
122
+ win
123
+ ]);
124
+ return {
125
+ setObserverList,
126
+ setObserverInit: setObserverInitExternal,
127
+ observer
44
128
  };
45
- }, [observerList, observerInit, callback]);
46
- return {
47
- setObserverList,
48
- setObserverInit,
49
- observer
50
- };
51
129
  };
52
- //# sourceMappingURL=useIntersectionObserver.js.map
@@ -1 +1 @@
1
- {"version":3,"mappings":"AACA,OAAO,KAAKA,KAAK,MAAM,OAAO;AAC9B,SAASC,yBAAyB,QAAQ,2BAA2B;AAErE,MAAM;EAAEC,QAAQ;EAAEC;AAAM,CAAE,GAAGH,KAAK;AAElC;;;;;;;;;AAUA,OAAO,MAAMI,uBAAuB,GAAG,CACrCC,QAAsC,EACtCC,OAAkC,KAKhC;EACF;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA,MAAMC,QAAQ,GAAGJ,MAAM,EAAwB;EAC/C,MAAM,CAACK,YAAY,EAAEC,eAAe,CAAC,GAAGP,QAAQ,EAAa;EAC7D,MAAM,CAACQ,YAAY,EAAEC,eAAe,CAAC,GAAGT,QAAQ,CAAuCI,OAAO,CAAC;EAE/F;EACA;EACAL,yBAAyB,CAAC,MAAK;IAC7BM,QAAQ,CAACK,OAAO,GAAG,IAAIC,oBAAoB,CAACR,QAAQ,EAAEK,YAAY,CAAC;IAEnE;IACA,IAAIH,QAAQ,CAACK,OAAO,IAAIJ,YAAY,IAAIA,YAAY,CAACM,MAAM,GAAG,CAAC,EAAE;MAC/DN,YAAY,CAACO,OAAO,CAACC,OAAO,IAAG;;QAC7B,cAAQ,CAACJ,OAAO,0CAAEK,OAAO,CAACD,OAAO,CAAC;MACpC,CAAC,CAAC;;IAGJ;IACA,OAAO,MAAK;MACV,IAAIT,QAAQ,CAACK,OAAO,EAAE;QACpBL,QAAQ,CAACK,OAAO,CAACM,UAAU,EAAE;;IAEjC,CAAC;EACH,CAAC,EAAE,CAACV,YAAY,EAAEE,YAAY,EAAEL,QAAQ,CAAC,CAAC;EAE1C,OAAO;IAAEI,eAAe;IAAEE,eAAe;IAAEJ;EAAQ,CAAE;AACvD,CAAC","names":["React","useIsomorphicLayoutEffect","useState","useRef","useIntersectionObserver","callback","options","observer","observerList","setObserverList","observerInit","setObserverInit","current","IntersectionObserver","length","forEach","element","observe","disconnect"],"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":["React","useIsomorphicLayoutEffect","useFluent_unstable","useFluent","useCallback","useState","useRef","useMutationObserver","getRTLRootMargin","ltrRootMargin","target","win","newDir","getComputedStyle","direction","newMargin","splitMargins","split","length","useIntersectionObserver","callback","options","observer","observerList","setObserverList","targetDocument","defaultView","observerInit","setObserverInit","rootMargin","root","mutationObserverCallback","mutationList","mutation","type","attributeName","contains","current","attributes","subtree","attributeFilter","IntersectionObserver","forEach","element","observe","disconnect","setObserverInitExternal","newInit"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AACA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,yBAAyB,QAAQ,4BAA4B;AACtE,SAASC,sBAAsBC,SAAS,QAAQ,kCAAkC;AAElF,MAAM,EAAEC,WAAW,EAAEC,QAAQ,EAAEC,MAAM,EAAE,GAAGN;AAC1C,SAASO,mBAAmB,QAAQ,wBAAwB;AAE5D;;;;;CAKC,GACD,OAAO,MAAMC,mBAAmB,CAC9BC,eACAC,QACAC;IAEA,IAAID,UAAUC,KAAK;QACjB,8CAA8C;QAC9C,MAAMC,SAASD,IAAIE,gBAAgB,CAACH,QAAmBI,SAAS;QAEhE,+FAA+F;QAC/F,IAAIF,WAAW,OAAO;YACpB,IAAIG,YAAYN;YAChB,MAAMO,eAAeP,cAAcQ,KAAK,C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;KAAI;IAGvC,OAAO;QAAEa;QAAiBI,iBAAiBkB;QAAyBxB;IAAS;AAC/E,EAAE"}
@@ -0,0 +1,116 @@
1
+ import * as React from 'react';
2
+ import { useRef } from 'react';
3
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
4
+ /**
5
+ * Provides a way of automating size in the virtualizer
6
+ * Returns
7
+ * `width` - element width ref (0 by default),
8
+ * `height` - element height ref (0 by default),
9
+ * `measureElementRef` - a ref function to be passed as `ref` to the element you want to measure
10
+ */ export function useMeasureList(currentIndex, refLength, totalLength, defaultItemSize) {
11
+ const widthArray = React.useRef(new Array(totalLength).fill(defaultItemSize));
12
+ const heightArray = React.useRef(new Array(totalLength).fill(defaultItemSize));
13
+ const refArray = React.useRef([]);
14
+ const { targetDocument } = useFluent();
15
+ // This lets us trigger updates when a size change occurs.
16
+ const sizeUpdateCount = useRef(0);
17
+ // the handler for resize observer
18
+ const handleIndexUpdate = React.useCallback((index)=>{
19
+ var _refArray_current_index;
20
+ let isChanged = false;
21
+ const boundClientRect = (_refArray_current_index = refArray.current[index]) === null || _refArray_current_index === void 0 ? void 0 : _refArray_current_index.getBoundingClientRect();
22
+ const containerWidth = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.width;
23
+ if (containerWidth !== widthArray.current[currentIndex + index]) {
24
+ isChanged = true;
25
+ }
26
+ widthArray.current[currentIndex + index] = containerWidth || defaultItemSize;
27
+ const containerHeight = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.height;
28
+ if (containerHeight !== heightArray.current[currentIndex + index]) {
29
+ isChanged = true;
30
+ }
31
+ heightArray.current[currentIndex + index] = containerHeight || defaultItemSize;
32
+ if (isChanged) {
33
+ sizeUpdateCount.current = sizeUpdateCount.current + 1;
34
+ }
35
+ }, [
36
+ currentIndex,
37
+ defaultItemSize,
38
+ sizeUpdateCount
39
+ ]);
40
+ const handleElementResizeCallback = (entries)=>{
41
+ for (const entry of entries){
42
+ const target = entry.target;
43
+ // Call the elements own resize handler (indexed)
44
+ target.handleResize();
45
+ }
46
+ };
47
+ React.useEffect(()=>{
48
+ widthArray.current = new Array(totalLength).fill(defaultItemSize);
49
+ heightArray.current = new Array(totalLength).fill(defaultItemSize);
50
+ }, [
51
+ defaultItemSize,
52
+ totalLength
53
+ ]);
54
+ // Keep the reference of ResizeObserver as a ref, as it should live through renders
55
+ const resizeObserver = React.useRef(createResizeObserverFromDocument(targetDocument, handleElementResizeCallback));
56
+ /* createIndexedRef provides a dynamic function to create an undefined number of refs at render time
57
+ * these refs then provide an indexed callback via attaching 'handleResize' to the element itself
58
+ * this function is then called on resize by handleElementResize and relies on indexing
59
+ * to track continuous sizes throughout renders while releasing all virtualized element refs each render cycle.
60
+ */ const createIndexedRef = React.useCallback((index)=>{
61
+ const measureElementRef = (el)=>{
62
+ if (!targetDocument || !resizeObserver.current) {
63
+ return;
64
+ }
65
+ if (el) {
66
+ el.handleResize = ()=>{
67
+ handleIndexUpdate(index);
68
+ };
69
+ }
70
+ // cleanup previous container
71
+ if (refArray.current[index] !== undefined && refArray.current[index] !== null) {
72
+ resizeObserver.current.unobserve(refArray.current[index]);
73
+ }
74
+ refArray.current[index] = undefined;
75
+ if (el) {
76
+ refArray.current[index] = el;
77
+ resizeObserver.current.observe(el);
78
+ handleIndexUpdate(index);
79
+ }
80
+ };
81
+ return measureElementRef;
82
+ }, [
83
+ handleIndexUpdate,
84
+ resizeObserver,
85
+ targetDocument
86
+ ]);
87
+ React.useEffect(()=>{
88
+ const _resizeObserver = resizeObserver;
89
+ return ()=>{
90
+ var _resizeObserver_current;
91
+ return (_resizeObserver_current = _resizeObserver.current) === null || _resizeObserver_current === void 0 ? void 0 : _resizeObserver_current.disconnect();
92
+ };
93
+ }, [
94
+ resizeObserver
95
+ ]);
96
+ return {
97
+ widthArray,
98
+ heightArray,
99
+ createIndexedRef,
100
+ refArray,
101
+ sizeUpdateCount: sizeUpdateCount.current
102
+ };
103
+ }
104
+ /**
105
+ * FIXME - TS 3.8/3.9 don't have ResizeObserver types by default, move this to a shared utility once we bump the minbar
106
+ * A utility method that creates a ResizeObserver from a target document
107
+ * @param targetDocument - document to use to create the ResizeObserver
108
+ * @param callback - https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver/ResizeObserver#callback
109
+ * @returns a ResizeObserver instance or null if the global does not exist on the document
110
+ */ export function createResizeObserverFromDocument(targetDocument, callback) {
111
+ var _targetDocument_defaultView;
112
+ if (!(targetDocument === null || targetDocument === void 0 ? void 0 : (_targetDocument_defaultView = targetDocument.defaultView) === null || _targetDocument_defaultView === void 0 ? void 0 : _targetDocument_defaultView.ResizeObserver)) {
113
+ return null;
114
+ }
115
+ return new targetDocument.defaultView.ResizeObserver(callback);
116
+ }
@@ -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":["React","useRef","useFluent_unstable","useFluent","useMeasureList","currentIndex","refLength","totalLength","defaultItemSize","widthArray","Array","fill","heightArray","refArray","targetDocument","sizeUpdateCount","handleIndexUpdate","useCallback","index","isChanged","boundClientRect","current","getBoundingClientRect","containerWidth","width","containerHeight","height","handleElementResizeCallback","entries","entry","target","handleResize","useEffect","resizeObserver","createResizeObserverFromDocument","createIndexedRef","measureElementRef","el","undefined","unobserve","observe","_resizeObserver","disconnect","callback","defaultView","ResizeObserver"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,MAAM,QAAQ,QAAQ;AAC/B,SAASC,sBAAsBC,SAAS,QAAQ,kCAAkC;AAKlF;;;;;;CAMC,GACD,OAAO,SAASC,eAEdC,YAAoB,EAAEC,SAAiB,EAAEC,WAAmB,EAAEC,eAAuB;IACrF,MAAMC,aAAaT,MAAMC,MAAM,CAAC,IAAIS,MAAMH,aAAaI,IAAI,CAACH;IAC5D,MAAMI,cAAcZ,MAAMC,MAAM,CAAC,IAAIS,MAAMH,aAAaI,IAAI,CAACH;IAE7D,MAAMK,WAAWb,MAAMC,MAAM,CAAqC,EAAE;IACpE,MAAM,EAAE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AMkB,oBAAoB,CAACC;YACzB,IAAI,CAACvB,kBAAkB,CAACmB,eAAeZ,OAAO,EAAE;gBAC9C;YACF;YAEA,IAAIgB,IAAI;gBACNA,GAAGN,YAAY,GAAG;oBAChBf,kBAAkBE;gBACpB;YACF;YAEA,6BAA6B;YAC7B,IAAIL,SAASQ,OAAO,CAACH,MAAM,KAAKoB,aAAazB,SAASQ,OAAO,CAACH,MAAM,KAAK,MAAM;gBAC7Ee,eAAeZ,OAAO,CAACkB,SAAS,CAAC1B,SAASQ,OAAO,CAACH,MAAM;YAC1D;YAEAL,SAASQ,OAAO,CAACH,MAAM,GAAGoB;YAC1B,IAAID,IAAI;gBACNxB,SAASQ,OAAO,CAACH,MAAM,GAAGmB;gBAC1BJ,eAAeZ,OAAO,CAACmB,OAAO,CAACH;gBAC/BrB,kBAAkBE;YACpB;QACF;QAEA,OAAOkB;IACT,GACA;QAACpB;QAAmBiB;QAAgBnB;KAAe;IAGrDd,MAAMgC,SAAS,CAAC;QACd,MAAMS,kBAAkBR;QACxB,OAAO;gBAAMQ;oBAAAA,0BAAAA,gBAAgBpB,OAAO,cAAvBoB,8CAAAA,wBAAyBC,UAAU;;IAClD,GAAG;QAACT;KAAe;IAEnB,OAAO;QAAExB;QAAYG;QAAauB;QAAkBtB;QAAUE,iBAAiBA,gBAAgBM,OAAO;IAAC;AACzG;AAEA;;;;;;CAMC,GACD,OAAO,SAASa,iCACdpB,cAA2C,EAC3C6B,QAAgC;QAE3B7B;IAAL,IAAI,EAACA,2BAAAA,sCAAAA,8BAAAA,eAAgB8B,WAAW,cAA3B9B,kDAAAA,4BAA6B+B,cAAc,GAAE;QAChD,OAAO;IACT;IAEA,OAAO,IAAI/B,eAAe8B,WAAW,CAACC,cAAc,CAACF;AACvD"}
@@ -0,0 +1,38 @@
1
+ import * as React from 'react';
2
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
3
+ const { useRef, useEffect } = React;
4
+ export const useMutationObserver = (target, callback, options)=>{
5
+ 'use no memo';
6
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
7
+ // eslint-disable-next-line no-restricted-globals
8
+ const observer = useRef();
9
+ const { targetDocument } = useFluent();
10
+ const win = targetDocument === null || targetDocument === void 0 ? void 0 : targetDocument.defaultView;
11
+ useEffect(()=>{
12
+ if (!win) {
13
+ return;
14
+ }
15
+ // Create an observer instance linked to the callback function
16
+ observer.current = new win.MutationObserver(callback);
17
+ }, [
18
+ callback,
19
+ win
20
+ ]);
21
+ useEffect(()=>{
22
+ if (target) {
23
+ var // Start observing the target node for configured mutations
24
+ _observer_current;
25
+ (_observer_current = observer.current) === null || _observer_current === void 0 ? void 0 : _observer_current.observe(target, options);
26
+ }
27
+ return ()=>{
28
+ var _observer_current;
29
+ (_observer_current = observer.current) === null || _observer_current === void 0 ? void 0 : _observer_current.disconnect();
30
+ };
31
+ }, [
32
+ target,
33
+ options
34
+ ]);
35
+ return {
36
+ observer
37
+ };
38
+ };
@@ -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":["React","useFluent_unstable","useFluent","useRef","useEffect","useMutationObserver","target","callback","options","observer","targetDocument","win","defaultView","current","MutationObserver","observe","disconnect"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AACA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,sBAAsBC,SAAS,QAAQ,kCAAkC;AAElF,MAAM,EAAEC,MAAM,EAAEC,SAAS,EAAE,GAAGJ;AAE9B,OAAO,MAAMK,sBAAsB,CACjCC,QACAC,UACAC;IAIA;IAEA,8FAA8F;IAC9F,iDAAiD;IACjD,MAAMC,WAAWN;IACjB,MAAM,EAAEO,cAAc,EAAE,GAAGR;IAC3B,MAAMS,MAAMD,2BAAAA,qCAAAA,eAAgBE,WAAW;IAEvCR,UAAU;QACR,IAAI,CAACO,KAAK;YACR;QACF;QACA,8DAA8D;QAC9DF,SAASI,OAAO,GAAG,IAAIF,IAAIG,gBAAgB,CAACP;IAC9C,GAAG;QAACA;QAAUI;KAAI;IAElBP,UAAU;QACR,IAAIE,QAAQ;gBACV,2DAA2D;YAC3DG;aAAAA,oBAAAA,SAASI,OAAO,cAAhBJ,wCAAAA,kBAAkBM,OAAO,CAACT,QAAQE;QACpC;QAEA,OAAO;gBACLC;aAAAA,oBAAAA,SAASI,OAAO,cAAhBJ,wCAAAA,kBAAkBO,UAAU;QAC9B;IACF,GAAG;QAACV;QAAQE;KAAQ;IAEpB,OAAO;QAAEC;IAAS;AACpB,EAAE"}
@@ -0,0 +1,50 @@
1
+ import * as React from 'react';
2
+ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
3
+ import { debounce } from '../utilities/debounce';
4
+ import { createResizeObserverFromDocument } from '../utilities/createResizeObserverFromDocument';
5
+ /**
6
+ * useResizeObserverRef_unstable simplifies resize observer connection and ensures debounce/cleanup
7
+ */ export const useResizeObserverRef_unstable = (resizeCallback)=>{
8
+ 'use no memo';
9
+ const { targetDocument } = useFluent();
10
+ const container = React.useRef(null);
11
+ // the handler for resize observer
12
+ // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
13
+ // eslint-disable-next-line no-restricted-globals
14
+ const handleResize = debounce((entries, observer)=>{
15
+ resizeCallback(entries, observer, container);
16
+ });
17
+ // Keep the reference of ResizeObserver in the state, as it should live through renders
18
+ const [resizeObserver, setResizeObserver] = React.useState(()=>createResizeObserverFromDocument(targetDocument, handleResize));
19
+ React.useEffect(()=>{
20
+ // Update our state when resizeCallback changes
21
+ container.current = null;
22
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
23
+ setResizeObserver(()=>createResizeObserverFromDocument(targetDocument, handleResize));
24
+ // eslint-disable-next-line react-hooks/exhaustive-deps
25
+ }, [
26
+ resizeCallback,
27
+ targetDocument
28
+ ]);
29
+ React.useEffect(()=>{
30
+ return ()=>{
31
+ container.current = null;
32
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.disconnect();
33
+ };
34
+ // eslint-disable-next-line react-hooks/exhaustive-deps
35
+ }, []);
36
+ const scrollRef = React.useCallback((instance)=>{
37
+ if (container.current !== instance) {
38
+ if (container.current) {
39
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.unobserve(container.current);
40
+ }
41
+ container.current = instance;
42
+ if (container.current) {
43
+ resizeObserver === null || resizeObserver === void 0 ? void 0 : resizeObserver.observe(container.current);
44
+ }
45
+ }
46
+ }, [
47
+ resizeObserver
48
+ ]);
49
+ return scrollRef;
50
+ };
@@ -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":["React","useFluent_unstable","useFluent","debounce","createResizeObserverFromDocument","useResizeObserverRef_unstable","resizeCallback","targetDocument","container","useRef","handleResize","entries","observer","resizeObserver","setResizeObserver","useState","useEffect","current","disconnect","scrollRef","useCallback","instance","unobserve","observe"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,sBAAsBC,SAAS,QAAQ,kCAAkC;AAClF,SAASC,QAAQ,QAAQ,wBAAwB;AACjD,SAASC,gCAAgC,QAAQ,gDAAgD;AAGjG;;CAEC,GACD,OAAO,MAAMC,gCAAgC,CAACC;IAC5C;IAEA,MAAM,EAAEC,cAAc,EAAE,GAAGL;IAC3B,MAAMM,YAAYR,MAAMS,MAAM,CAAqB;IACnD,kCAAkC;IAClC,8FAA8F;IAC9F,iDAAiD;IACjD,MAAMC,eAAeP,SAAS,CAACQ,SAAgCC;QAC7DN,eAAeK,SAASC,UAAUJ;IACpC;IAEA,uFAAuF;IACvF,MAAM,CAACK,gBAAgBC,kBAAkB,GAAGd,MAAMe,QAAQ,CAAC,IACzDX,iCAAiCG,gBAAgBG;IAGnDV,MAAMgB,SAAS,CAAC;QACd,+CAA+C;QAC/CR,UAAUS,OAAO,GAAG;QACpBJ,2BAAAA,qCAAAA,eAAgBK,UAAU;QAC1BJ,kBAAkB,IAAMV,iCAAiCG,gBAAgBG;IACzE,uDAAuD;IACzD,GAAG;QAACJ;QAAgBC;KAAe;IAEnCP,MAAMgB,SAAS,CAAC;QACd,OAAO;YACLR,UAAUS,OAAO,GAAG;YACpBJ,2BAAAA,qCAAAA,eAAgBK,UAAU;QAC5B;IACA,uDAAuD;IACzD,GAAG,EAAE;IAEL,MAAMC,YAAYnB,MAAMoB,WAAW,CACjC,CAACC;QACC,IAAIb,UAAUS,OAAO,KAAKI,UAAU;YAClC,IAAIb,UAAUS,OAAO,EAAE;gBACrBJ,2BAAAA,qCAAAA,eAAgBS,SAAS,CAACd,UAAUS,OAAO;YAC7C;YAEAT,UAAUS,OAAO,GAAGI;YACpB,IAAIb,UAAUS,OAAO,EAAE;gBACrBJ,2BAAAA,qCAAAA,eAAgBU,OAAO,CAACf,UAAUS,OAAO;YAC3C;QACF;IACF,GACA;QAACJ;KAAe;IAGlB,OAAOM;AACT,EAAE"}
@@ -0,0 +1,103 @@
1
+ import * as React from 'react';
2
+ import { useRef } from 'react';
3
+ import { useTimeout } from '@fluentui/react-utilities';
4
+ /**
5
+ * Optional hook that will enable pagination on the virtualizer so that it 'autoscrolls' to an items exact position
6
+ * Sizes are uniform/static, we round to the nearest item on long scrolls
7
+ * On short scrolls, we will go at minimum to the next/previous item so that arrow pagination works
8
+ * All VirtualizerStaticPaginationProps can be grabbed from Virtualizer hooks externally and passed in
9
+ */ export const useStaticVirtualizerPagination = (virtualizerProps, paginationEnabled = true)=>{
10
+ 'use no memo';
11
+ const { itemSize, axis = 'vertical' } = virtualizerProps;
12
+ const [setScrollTimer, clearScrollTimer] = useTimeout();
13
+ const lastScrollPos = useRef(0);
14
+ const lastIndexScrolled = useRef(0);
15
+ const scrollContainer = React.useRef(null);
16
+ const clearListeners = ()=>{
17
+ if (scrollContainer.current) {
18
+ scrollContainer.current.removeEventListener('scroll', onScroll);
19
+ scrollContainer.current = null;
20
+ clearScrollTimer();
21
+ }
22
+ };
23
+ React.useEffect(()=>{
24
+ return ()=>{
25
+ clearListeners();
26
+ };
27
+ // eslint-disable-next-line react-hooks/exhaustive-deps
28
+ }, []);
29
+ /**
30
+ * Handle scroll stop event and paginate to the closest item
31
+ * If the closest item is the same as the previous scroll end
32
+ * we paginate to the next/previous one based on direction
33
+ */ const onScrollEnd = React.useCallback(()=>{
34
+ if (!scrollContainer.current || !paginationEnabled) {
35
+ // No container found
36
+ return;
37
+ }
38
+ const currentScrollPos = Math.round(axis === 'vertical' ? scrollContainer.current.scrollTop : scrollContainer.current.scrollLeft);
39
+ const closestItem = Math.round(currentScrollPos / itemSize);
40
+ let nextItem = 0;
41
+ if (Math.round(closestItem - lastIndexScrolled.current) === 0) {
42
+ // Special case for go to next/previous with minimum amount of scroll needed
43
+ const nextTarget = lastScrollPos.current < currentScrollPos ? 1 : -1;
44
+ const isSecondaryScroll = lastScrollPos.current === currentScrollPos;
45
+ const posMod = isSecondaryScroll ? 0 : nextTarget;
46
+ nextItem = closestItem + posMod;
47
+ } else {
48
+ // Pagination for anything else can just jump to the closest!
49
+ nextItem = closestItem;
50
+ }
51
+ const nextItemPos = nextItem * itemSize;
52
+ if (axis === 'vertical') {
53
+ scrollContainer.current.scrollTo({
54
+ top: nextItemPos,
55
+ behavior: 'smooth'
56
+ });
57
+ } else {
58
+ scrollContainer.current.scrollTo({
59
+ left: nextItemPos,
60
+ behavior: 'smooth'
61
+ });
62
+ }
63
+ lastScrollPos.current = nextItemPos;
64
+ lastIndexScrolled.current = nextItem;
65
+ }, [
66
+ paginationEnabled,
67
+ axis,
68
+ itemSize
69
+ ]);
70
+ /**
71
+ * On scroll timer that will continuously delay callback until scrolling stops
72
+ */ const onScroll = React.useCallback((event)=>{
73
+ clearScrollTimer();
74
+ setScrollTimer(onScrollEnd, 100);
75
+ }, [
76
+ onScrollEnd,
77
+ clearScrollTimer,
78
+ setScrollTimer
79
+ ]);
80
+ /**
81
+ * Pagination ref will ensure we attach listeners to containers on change
82
+ * It is returned from hook and merged into the scroll container externally
83
+ */ const paginationRef = React.useCallback((instance)=>{
84
+ if (!paginationEnabled) {
85
+ clearListeners();
86
+ scrollContainer.current = null;
87
+ return;
88
+ }
89
+ if (scrollContainer.current !== instance) {
90
+ clearListeners();
91
+ scrollContainer.current = instance;
92
+ if (scrollContainer.current) {
93
+ scrollContainer.current.addEventListener('scroll', onScroll);
94
+ }
95
+ }
96
+ }, // eslint-disable-next-line react-hooks/exhaustive-deps
97
+ [
98
+ onScroll,
99
+ onScrollEnd,
100
+ paginationEnabled
101
+ ]);
102
+ return paginationRef;
103
+ };
@@ -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":["React","useRef","useTimeout","useStaticVirtualizerPagination","virtualizerProps","paginationEnabled","itemSize","axis","setScrollTimer","clearScrollTimer","lastScrollPos","lastIndexScrolled","scrollContainer","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":"AAAA,YAAYA,WAAW,QAAQ;AAE/B,SAASC,MAAM,QAAQ,QAAQ;AAC/B,SAASC,UAAU,QAAQ,4BAA4B;AAEvD;;;;;CAKC,GACD,OAAO,MAAMC,iCAAiC,CAC5CC,kBACAC,oBAA6B,IAAI;IAEjC;IAEA,MAAM,EAAEC,QAAQ,EAAEC,OAAO,UAAU,EAAE,GAAGH;IAExC,MAAM,CAACI,gBAAgBC,iBAAiB,GAAGP;IAC3C,MAAMQ,gBAAgBT,OAAe;IACrC,MAAMU,oBAAoBV,OAAe;IAEzC,MAAMW,kBAAkBZ,MAAMC,MAAM,CAAqB;IAEzD,MAAMY,iBAAiB;QACrB,IAAID,gBAAgBE,OAAO,EAAE;YAC3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9B,GACA;QAACA;QAAaT;QAAkBD;KAAe;IAGjD;;;GAGC,GACD,MAAM4B,gBAAgBpC,MAAMmB,WAAW,CACrC,CAACkB;QACC,IAAI,CAAChC,mBAAmB;YACtBQ;YACAD,gBAAgBE,OAAO,GAAG;YAC1B;QACF;QACA,IAAIF,gBAAgBE,OAAO,KAAKuB,UAAU;YACxCxB;YAEAD,gBAAgBE,OAAO,GAAGuB;YAC1B,IAAIzB,gBAAgBE,OAAO,EAAE;gBAC3BF,gBAAgBE,OAAO,CAACwB,gBAAgB,CAAC,UAAUtB;YACrD;QACF;IACF,GACA,uDAAuD;IACvD;QAACA;QAAUE;QAAab;KAAkB;IAG5C,OAAO+B;AACT,EAAE"}
@@ -0,0 +1,46 @@
1
+ import * as React from 'react';
2
+ import { useResizeObserverRef_unstable } from './useResizeObserverRef';
3
+ /**
4
+ * React hook that measures virtualized space based on a static size to ensure optimized virtualization length.
5
+ */ export const useStaticVirtualizerMeasure = (virtualizerProps)=>{
6
+ const { defaultItemSize, direction = 'vertical' } = virtualizerProps;
7
+ const [state, setState] = React.useState({
8
+ virtualizerLength: 0,
9
+ bufferSize: 0,
10
+ bufferItems: 0
11
+ });
12
+ const { virtualizerLength, bufferItems, bufferSize } = state;
13
+ const resizeCallback = React.useCallback((_entries, // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
14
+ // eslint-disable-next-line no-restricted-globals
15
+ _observer, scrollRef)=>{
16
+ if (!(scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current)) {
17
+ return;
18
+ }
19
+ const containerSize = direction === 'vertical' ? scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current.getBoundingClientRect().height : scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current.getBoundingClientRect().width;
20
+ /*
21
+ * Number of items required to cover viewport.
22
+ */ const length = Math.ceil(containerSize / defaultItemSize + 1);
23
+ /*
24
+ * Number of items to append at each end, i.e. 'preload' each side before entering view.
25
+ */ const newBufferItems = Math.max(Math.floor(length / 4), 2);
26
+ /*
27
+ * This is how far we deviate into the bufferItems to detect a redraw.
28
+ */ const newBufferSize = Math.max(Math.floor(length / 8 * defaultItemSize), 1);
29
+ const totalLength = length + newBufferItems * 2 + 1;
30
+ setState({
31
+ virtualizerLength: totalLength,
32
+ bufferItems: newBufferItems,
33
+ bufferSize: newBufferSize
34
+ });
35
+ }, [
36
+ defaultItemSize,
37
+ direction
38
+ ]);
39
+ const scrollRef = useResizeObserverRef_unstable(resizeCallback);
40
+ return {
41
+ virtualizerLength,
42
+ bufferItems,
43
+ bufferSize,
44
+ scrollRef
45
+ };
46
+ };
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["useVirtualizerMeasure.ts"],"sourcesContent":["import * as React from 'react';\nimport { VirtualizerMeasureProps } from './hooks.types';\nimport { useResizeObserverRef_unstable } from './useResizeObserverRef';\n\n/**\n * React hook that measures virtualized space based on a static size to ensure optimized virtualization length.\n */\nexport const useStaticVirtualizerMeasure = <TElement extends HTMLElement>(\n virtualizerProps: VirtualizerMeasureProps,\n): {\n virtualizerLength: number;\n bufferItems: number;\n bufferSize: number;\n scrollRef: (instance: TElement | null) => void;\n} => {\n const { defaultItemSize, direction = 'vertical' } = virtualizerProps;\n\n const [state, setState] = React.useState({\n virtualizerLength: 0,\n bufferSize: 0,\n bufferItems: 0,\n });\n\n const { virtualizerLength, bufferItems, bufferSize } = state;\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?.current) {\n return;\n }\n\n const containerSize =\n direction === 'vertical'\n ? scrollRef?.current.getBoundingClientRect().height\n : scrollRef?.current.getBoundingClientRect().width;\n\n /*\n * Number of items required to cover viewport.\n */\n const length = Math.ceil(containerSize / defaultItemSize + 1);\n\n /*\n * Number of items to append at each end, i.e. 'preload' each side before entering view.\n */\n const newBufferItems = Math.max(Math.floor(length / 4), 2);\n\n /*\n * This is how far we deviate into the bufferItems to detect a redraw.\n */\n const newBufferSize = Math.max(Math.floor((length / 8) * defaultItemSize), 1);\n\n const totalLength = length + newBufferItems * 2 + 1;\n\n setState({\n virtualizerLength: totalLength,\n bufferItems: newBufferItems,\n bufferSize: newBufferSize,\n });\n },\n [defaultItemSize, direction],\n );\n\n const scrollRef = useResizeObserverRef_unstable(resizeCallback);\n\n return {\n virtualizerLength,\n bufferItems,\n bufferSize,\n scrollRef,\n };\n};\n"],"names":["React","useResizeObserverRef_unstable","useStaticVirtualizerMeasure","virtualizerProps","defaultItemSize","direction","state","setState","useState","virtualizerLength","bufferSize","bufferItems","resizeCallback","useCallback","_entries","_observer","scrollRef","current","containerSize","getBoundingClientRect","height","width","length","Math","ceil","newBufferItems","max","floor","newBufferSize","totalLength"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,YAAYA,WAAW,QAAQ;AAE/B,SAASC,6BAA6B,QAAQ,yBAAyB;AAEvE;;CAEC,GACD,OAAO,MAAMC,8BAA8B,CACzCC;IAOA,MAAM,EAAEC,eAAe,EAAEC,YAAY,UAAU,EAAE,GAAGF;IAEpD,MAAM,CAACG,OAAOC,SAAS,GAAGP,MAAMQ,QAAQ,CAAC;QACvCC,mBAAmB;QACnBC,YAAY;QACZC,aAAa;IACf;IAEA,MAAM,EAAEF,iBAAiB,EAAEE,WAAW,EAAED,UAAU,EAAE,GAAGJ;IAEvD,MAAMM,iBAAiBZ,MAAMa,WAAW,CACtC,CACEC,UACA,8FAA8F;IAC9F,iDAAiD;IACjDC,WACAC;QAEA,IAAI,EAACA,sBAAAA,gCAAAA,UAAWC,OAAO,GAAE;YACvB;QACF;QAEA,MAAMC,gBACJb,cAAc,aACVW,sBAAAA,gCAAAA,UAAWC,OAAO,CAACE,qBAAqB,GAAGC,MAAM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