@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
@@ -0,0 +1,149 @@
1
+ import * as React from 'react';
2
+ import { slot, useMergedRefs } from '@fluentui/react-utilities';
3
+ import { useVirtualizer_unstable } from '../Virtualizer/useVirtualizer';
4
+ import { useDynamicVirtualizerMeasure } from '../../Hooks';
5
+ import { useVirtualizerContextState_unstable, scrollToItemDynamic } from '../../Utilities';
6
+ import { useImperativeHandle, useState } from 'react';
7
+ import { useMeasureList } from '../../hooks/useMeasureList';
8
+ import { useDynamicVirtualizerPagination } from '../../hooks/useDynamicPagination';
9
+ export function useVirtualizerScrollViewDynamic_unstable(props) {
10
+ 'use no memo';
11
+ var _imperativeVirtualizerRef_current;
12
+ const contextState = useVirtualizerContextState_unstable(props.virtualizerContext);
13
+ const { imperativeRef, axis = 'vertical', reversed, imperativeVirtualizerRef, enablePagination = false } = props;
14
+ let sizeTrackingArray = React.useRef(new Array(props.numItems).fill(props.itemSize));
15
+ // This lets us trigger updates when a size change occurs.
16
+ const [sizeUpdateCount, setSizeUpdateCount] = useState(0);
17
+ const getChildSizeAuto = React.useCallback((index)=>{
18
+ if (sizeTrackingArray.current.length <= index || sizeTrackingArray.current[index] <= 0) {
19
+ // Default size for initial state or untracked
20
+ return props.itemSize;
21
+ }
22
+ /* Required to be defined prior to our measure function
23
+ * we use a sizing array ref that we will update post-render
24
+ */ return sizeTrackingArray.current[index];
25
+ }, // eslint-disable-next-line react-hooks/exhaustive-deps
26
+ [
27
+ sizeTrackingArray,
28
+ props.itemSize,
29
+ sizeUpdateCount
30
+ ]);
31
+ var _props_axis, _props_getItemSize, _contextState_contextIndex;
32
+ const { virtualizerLength, bufferItems, bufferSize, scrollRef } = useDynamicVirtualizerMeasure({
33
+ defaultItemSize: props.itemSize,
34
+ direction: (_props_axis = props.axis) !== null && _props_axis !== void 0 ? _props_axis : 'vertical',
35
+ getItemSize: (_props_getItemSize = props.getItemSize) !== null && _props_getItemSize !== void 0 ? _props_getItemSize : getChildSizeAuto,
36
+ currentIndex: (_contextState_contextIndex = contextState === null || contextState === void 0 ? void 0 : contextState.contextIndex) !== null && _contextState_contextIndex !== void 0 ? _contextState_contextIndex : 0,
37
+ numItems: props.numItems
38
+ });
39
+ const _imperativeVirtualizerRef = useMergedRefs(React.useRef(null), imperativeVirtualizerRef);
40
+ var _contextState_contextIndex1;
41
+ const paginationRef = useDynamicVirtualizerPagination({
42
+ axis,
43
+ progressiveItemSizes: (_imperativeVirtualizerRef_current = _imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.progressiveSizes,
44
+ virtualizerLength,
45
+ currentIndex: (_contextState_contextIndex1 = contextState === null || contextState === void 0 ? void 0 : contextState.contextIndex) !== null && _contextState_contextIndex1 !== void 0 ? _contextState_contextIndex1 : 0
46
+ }, enablePagination);
47
+ // Store the virtualizer length as a ref for imperative ref access
48
+ const virtualizerLengthRef = React.useRef(virtualizerLength);
49
+ if (virtualizerLengthRef.current !== virtualizerLength) {
50
+ virtualizerLengthRef.current = virtualizerLength;
51
+ }
52
+ const scrollViewRef = useMergedRefs(props.scrollViewRef, scrollRef, paginationRef);
53
+ const scrollCallbackRef = React.useRef(null);
54
+ useImperativeHandle(imperativeRef, ()=>{
55
+ var _imperativeVirtualizerRef_current;
56
+ return {
57
+ scrollTo (index, behavior = 'auto', callback) {
58
+ scrollCallbackRef.current = callback !== null && callback !== void 0 ? callback : null;
59
+ if (_imperativeVirtualizerRef.current) {
60
+ var _imperativeVirtualizerRef_current;
61
+ const progressiveSizes = _imperativeVirtualizerRef.current.progressiveSizes.current;
62
+ const totalSize = progressiveSizes && (progressiveSizes === null || progressiveSizes === void 0 ? void 0 : progressiveSizes.length) > 0 ? progressiveSizes[Math.max(progressiveSizes.length - 1, 0)] : 0;
63
+ _imperativeVirtualizerRef.current.setFlaggedIndex(index);
64
+ scrollToItemDynamic({
65
+ index,
66
+ itemSizes: (_imperativeVirtualizerRef_current = _imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.nodeSizes,
67
+ totalSize,
68
+ scrollViewRef,
69
+ axis,
70
+ reversed,
71
+ behavior
72
+ });
73
+ }
74
+ },
75
+ currentIndex: (_imperativeVirtualizerRef_current = _imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.currentIndex,
76
+ virtualizerLength: virtualizerLengthRef
77
+ };
78
+ }, [
79
+ axis,
80
+ scrollViewRef,
81
+ reversed,
82
+ _imperativeVirtualizerRef
83
+ ]);
84
+ const handleRenderedIndex = (index)=>{
85
+ if (scrollCallbackRef.current) {
86
+ scrollCallbackRef.current(index);
87
+ }
88
+ };
89
+ var _props_getItemSize1;
90
+ const virtualizerState = useVirtualizer_unstable({
91
+ ...props,
92
+ getItemSize: (_props_getItemSize1 = props.getItemSize) !== null && _props_getItemSize1 !== void 0 ? _props_getItemSize1 : getChildSizeAuto,
93
+ virtualizerLength,
94
+ bufferItems,
95
+ bufferSize,
96
+ scrollViewRef,
97
+ virtualizerContext: contextState,
98
+ imperativeVirtualizerRef: _imperativeVirtualizerRef,
99
+ onRenderedFlaggedIndex: handleRenderedIndex
100
+ });
101
+ const measureObject = useMeasureList(virtualizerState.virtualizerStartIndex, virtualizerLength, props.numItems, props.itemSize);
102
+ if (enablePagination && measureObject.sizeUpdateCount !== sizeUpdateCount) {
103
+ /* This enables us to let callback know that the sizes have been updated
104
+ triggers a re-render but is only required on pagination (else index change handles) */ setSizeUpdateCount(measureObject.sizeUpdateCount);
105
+ }
106
+ if (axis === 'horizontal') {
107
+ sizeTrackingArray = measureObject.widthArray;
108
+ } else {
109
+ sizeTrackingArray = measureObject.heightArray;
110
+ }
111
+ if (!props.getItemSize) {
112
+ // Auto-measuring is required
113
+ React.Children.map(virtualizerState.virtualizedChildren, (child, index)=>{
114
+ if (/*#__PURE__*/ React.isValidElement(child)) {
115
+ virtualizerState.virtualizedChildren[index] = /*#__PURE__*/ React.createElement(child.type, {
116
+ ...child.props,
117
+ key: child.key,
118
+ ref: (element)=>{
119
+ if (child.hasOwnProperty('ref')) {
120
+ // We must access this from the child directly, not props (forward ref).
121
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
122
+ const localRef = child === null || child === void 0 ? void 0 : child.ref;
123
+ if (typeof localRef === 'function') {
124
+ localRef(element);
125
+ } else if (localRef) {
126
+ localRef.current = element;
127
+ }
128
+ }
129
+ // Call the auto-measure ref attachment.
130
+ measureObject.createIndexedRef(index)(element);
131
+ }
132
+ });
133
+ }
134
+ });
135
+ }
136
+ return {
137
+ ...virtualizerState,
138
+ components: {
139
+ ...virtualizerState.components,
140
+ container: 'div'
141
+ },
142
+ container: slot.always(props.container, {
143
+ defaultProps: {
144
+ ref: scrollViewRef
145
+ },
146
+ elementType: 'div'
147
+ })
148
+ };
149
+ }
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["useVirtualizerScrollViewDynamic.tsx"],"sourcesContent":["import * as React from 'react';\nimport { slot, useMergedRefs } from '@fluentui/react-utilities';\nimport { useVirtualizer_unstable } from '../Virtualizer/useVirtualizer';\nimport type {\n VirtualizerScrollViewDynamicProps,\n VirtualizerScrollViewDynamicState,\n} from './VirtualizerScrollViewDynamic.types';\nimport { useDynamicVirtualizerMeasure } from '../../Hooks';\nimport { useVirtualizerContextState_unstable, scrollToItemDynamic } from '../../Utilities';\nimport type { VirtualizerDataRef } from '../Virtualizer/Virtualizer.types';\nimport { useImperativeHandle, useState } from 'react';\nimport { useMeasureList } from '../../hooks/useMeasureList';\nimport type { IndexedResizeCallbackElement } from '../../hooks/useMeasureList';\nimport { useDynamicVirtualizerPagination } from '../../hooks/useDynamicPagination';\n\nexport function useVirtualizerScrollViewDynamic_unstable(\n props: VirtualizerScrollViewDynamicProps,\n): VirtualizerScrollViewDynamicState {\n 'use no memo';\n\n const contextState = useVirtualizerContextState_unstable(props.virtualizerContext);\n const { imperativeRef, axis = 'vertical', reversed, imperativeVirtualizerRef, enablePagination = false } = props;\n\n let sizeTrackingArray = React.useRef<number[]>(new Array(props.numItems).fill(props.itemSize));\n\n // This lets us trigger updates when a size change occurs.\n const [sizeUpdateCount, setSizeUpdateCount] = useState(0);\n\n const getChildSizeAuto = React.useCallback(\n (index: number) => {\n if (sizeTrackingArray.current.length <= index || sizeTrackingArray.current[index] <= 0) {\n // Default size for initial state or untracked\n return props.itemSize;\n }\n /* Required to be defined prior to our measure function\n * we use a sizing array ref that we will update post-render\n */\n return sizeTrackingArray.current[index];\n },\n // eslint-disable-next-line react-hooks/exhaustive-deps\n [sizeTrackingArray, props.itemSize, sizeUpdateCount],\n );\n\n const { virtualizerLength, bufferItems, bufferSize, scrollRef } = useDynamicVirtualizerMeasure({\n defaultItemSize: props.itemSize,\n direction: props.axis ?? 'vertical',\n getItemSize: props.getItemSize ?? getChildSizeAuto,\n currentIndex: contextState?.contextIndex ?? 0,\n numItems: props.numItems,\n });\n\n const _imperativeVirtualizerRef = useMergedRefs(React.useRef<VirtualizerDataRef>(null), imperativeVirtualizerRef);\n\n const paginationRef = useDynamicVirtualizerPagination(\n {\n axis,\n progressiveItemSizes: _imperativeVirtualizerRef.current?.progressiveSizes,\n virtualizerLength,\n currentIndex: contextState?.contextIndex ?? 0,\n },\n enablePagination,\n );\n\n // Store the virtualizer length as a ref for imperative ref access\n const virtualizerLengthRef = React.useRef<number>(virtualizerLength);\n if (virtualizerLengthRef.current !== virtualizerLength) {\n virtualizerLengthRef.current = virtualizerLength;\n }\n const scrollViewRef = useMergedRefs(props.scrollViewRef, scrollRef, paginationRef) as React.RefObject<HTMLDivElement>;\n const scrollCallbackRef = React.useRef<null | ((index: number) => void)>(null);\n\n useImperativeHandle(\n imperativeRef,\n () => {\n return {\n scrollTo(index: number, behavior = 'auto', callback: undefined | ((index: number) => void)) {\n scrollCallbackRef.current = callback ?? null;\n if (_imperativeVirtualizerRef.current) {\n const progressiveSizes = _imperativeVirtualizerRef.current.progressiveSizes.current;\n const totalSize =\n progressiveSizes && progressiveSizes?.length > 0\n ? progressiveSizes[Math.max(progressiveSizes.length - 1, 0)]\n : 0;\n\n _imperativeVirtualizerRef.current.setFlaggedIndex(index);\n scrollToItemDynamic({\n index,\n itemSizes: _imperativeVirtualizerRef.current?.nodeSizes,\n totalSize,\n scrollViewRef,\n axis,\n reversed,\n behavior,\n });\n }\n },\n currentIndex: _imperativeVirtualizerRef.current?.currentIndex,\n virtualizerLength: virtualizerLengthRef,\n };\n },\n [axis, scrollViewRef, reversed, _imperativeVirtualizerRef],\n );\n\n const handleRenderedIndex = (index: number) => {\n if (scrollCallbackRef.current) {\n scrollCallbackRef.current(index);\n }\n };\n\n const virtualizerState = useVirtualizer_unstable({\n ...props,\n getItemSize: props.getItemSize ?? getChildSizeAuto,\n virtualizerLength,\n bufferItems,\n bufferSize,\n scrollViewRef,\n virtualizerContext: contextState,\n imperativeVirtualizerRef: _imperativeVirtualizerRef,\n onRenderedFlaggedIndex: handleRenderedIndex,\n });\n\n const measureObject = useMeasureList(\n virtualizerState.virtualizerStartIndex,\n virtualizerLength,\n props.numItems,\n props.itemSize,\n );\n\n if (enablePagination && measureObject.sizeUpdateCount !== sizeUpdateCount) {\n /* This enables us to let callback know that the sizes have been updated\n triggers a re-render but is only required on pagination (else index change handles) */\n setSizeUpdateCount(measureObject.sizeUpdateCount);\n }\n\n if (axis === 'horizontal') {\n sizeTrackingArray = measureObject.widthArray;\n } else {\n sizeTrackingArray = measureObject.heightArray;\n }\n\n if (!props.getItemSize) {\n // Auto-measuring is required\n React.Children.map(virtualizerState.virtualizedChildren, (child, index) => {\n if (React.isValidElement(child)) {\n virtualizerState.virtualizedChildren[index] = (\n <child.type\n {...child.props}\n key={child.key}\n ref={(element: HTMLElement & IndexedResizeCallbackElement) => {\n if (child.hasOwnProperty('ref')) {\n // We must access this from the child directly, not props (forward ref).\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n const localRef = (child as any)?.ref;\n\n if (typeof localRef === 'function') {\n localRef(element);\n } else if (localRef) {\n localRef.current = element;\n }\n }\n\n // Call the auto-measure ref attachment.\n measureObject.createIndexedRef(index)(element);\n }}\n />\n );\n }\n });\n }\n\n return {\n ...virtualizerState,\n components: {\n ...virtualizerState.components,\n container: 'div',\n },\n container: slot.always(props.container, {\n defaultProps: {\n ref: scrollViewRef,\n },\n elementType: 'div',\n }),\n };\n}\n"],"names":["React","slot","useMergedRefs","useVirtualizer_unstable","useDynamicVirtualizerMeasure","useVirtualizerContextState_unstable","scrollToItemDynamic","useImperativeHandle","useState","useMeasureList","useDynamicVirtualizerPagination","useVirtualizerScrollViewDynamic_unstable","props","_imperativeVirtualizerRef","contextState","virtualizerContext","imperativeRef","axis","reversed","imperativeVirtualizerRef","enablePagination","sizeTrackingArray","useRef","Array","numItems","fill","itemSize","sizeUpdateCount","setSizeUpdateCount","getChildSizeAuto","useCallback","index","current","length","virtualizerLength","bufferItems","bufferSize","scrollRef","defaultItemSize","direction","getItemSize","currentIndex","contextIndex","paginationRef","progressiveItemSizes","progressiveSizes","virtualizerLengthRef","scrollViewRef","scrollCallbackRef","scrollTo","behavior","callback","totalSize","Math","max","setFlaggedIndex","itemSizes","nodeSizes","handleRenderedIndex","virtualizerState","onRenderedFlaggedIndex","measureObject","virtualizerStartIndex","widthArray","heightArray","Children","map","virtualizedChildren","child","isValidElement","type","key","ref","element","hasOwnProperty","localRef","createIndexedRef","components","container","always","defaultProps","elementType"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,IAAI,EAAEC,aAAa,QAAQ,4BAA4B;AAChE,SAASC,uBAAuB,QAAQ,gCAAgC;AAKxE,SAASC,4BAA4B,QAAQ,cAAc;AAC3D,SAASC,mCAAmC,EAAEC,mBAAmB,QAAQ,kBAAkB;AAE3F,SAASC,mBAAmB,EAAEC,QAAQ,QAAQ,QAAQ;AACtD,SAASC,cAAc,QAAQ,6BAA6B;AAE5D,SAASC,+BAA+B,QAAQ,mCAAmC;AAEnF,OAAO,SAASC,yCACdC,KAAwC;IAExC;QAsC0BC;IApC1B,MAAMC,eAAeT,oCAAoCO,MAAMG,kBAAkB;IACjF,MAAM,EAAEC,aAAa,EAAEC,OAAO,UAAU,EAAEC,QAAQ,EAAEC,wBAAwB,EAAEC,mBAAmB,KAAK,EAAE,GAAGR;IAE3G,IAAIS,oBAAoBrB,MAAMsB,MAAM,CAAW,IAAIC,MAAMX,MAAMY,QAAQ,EAAEC,IAAI,CAACb,MAAMc,QAAQ;IAE5F,0DAA0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izerStyles_unstable, virtualizerClassNames } from '../Virtualizer/useVirtualizerStyles.styles';
2
+ import { __styles, mergeClasses } from '@griffel/react';
3
+ const virtualizerScrollViewDynamicClassName = 'fui-Virtualizer-Scroll-View-Dynamic';
4
+ export const virtualizerScrollViewDynamicClassNames = {
5
+ ...virtualizerClassNames,
6
+ container: `${virtualizerScrollViewDynamicClassName}__container`
7
+ };
8
+ const useStyles = /*#__PURE__*/__styles({
9
+ base: {
10
+ mc9l5x: "f22iagw",
11
+ a9b677: "fly5x3f",
12
+ Bqenvij: "f1l02sjl"
13
+ },
14
+ vertical: {
15
+ Beiy3e4: "f1vx9l62",
16
+ Bmxbyg5: "f5zp4f"
17
+ },
18
+ horizontal: {
19
+ Beiy3e4: "f1063pyq",
20
+ B68tc82: "f1oy3dpc"
21
+ },
22
+ verticalReversed: {
23
+ Beiy3e4: "f1gkdon0",
24
+ Bmxbyg5: "f5zp4f"
25
+ },
26
+ horizontalReversed: {
27
+ Beiy3e4: "f1oztnx0",
28
+ B68tc82: "f1oy3dpc"
29
+ }
30
+ }, {
31
+ d: [".f22iagw{display:flex;}", ".fly5x3f{width:100%;}", ".f1l02sjl{height:100%;}", ".f1vx9l62{flex-direction:column;}", ".f5zp4f{overflow-y:auto;}", ".f1063pyq{flex-direction:row;}", ".f1oy3dpc{overflow-x:auto;}", ".f1gkdon0{flex-direction:column-reverse;}", ".f1oztnx0{flex-direction:row-reverse;}"]
32
+ });
33
+ /**
34
+ * Apply styling to the Virtualizer states
35
+ */
36
+ export const useVirtualizerScrollViewDynamicStyles_unstable = state => {
37
+ 'use no memo';
38
+
39
+ const styles = useStyles();
40
+ // Default virtualizer styles base
41
+ useVirtualizerStyles_unstable(state);
42
+ const containerStyle = state.axis === 'horizontal' ? state.reversed ? styles.horizontalReversed : styles.horizontal : state.reversed ? styles.verticalReversed : styles.vertical;
43
+ // Add container styles
44
+ state.container.className = mergeClasses(virtualizerScrollViewDynamicClassNames.container, styles.base, containerStyle, state.container.className);
45
+ return state;
46
+ };
47
+ //# sourceMappingURL=useVirtualizerScrollViewDynamicStyles.styles.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"names":["useVirtualizerStyles_unstable","virtualizerClassNames","__styles","mergeClasses","virtualizerScrollViewDynamicClassName","virtualizerScrollViewDynamicClassNames","container","useStyles","base","mc9l5x","a9b677","Bqenvij","vertical","Beiy3e4","Bmxbyg5","horizontal","B68tc82","verticalReversed","horizontalReversed","d","useVirtualizerScrollViewDynamicStyles_unstable","state","styles","containerStyle","axis","reversed","className"],"sources":["useVirtualizerScrollViewDynamicStyles.styles.js"],"sourcesContent":["import { useVirtualizerStyles_unstable, virtualizerClassNames } from '../Virtualizer/useVirtualizerStyles.styles';\nimport { makeStyles, mergeClasses } from '@griffel/react';\nconst virtualizerScrollViewDynamicClassName = 'fui-Virtualizer-Scroll-View-Dynamic';\nexport const virtualizerScrollViewDynamicClassNames = {\n ...virtualizerClassNames,\n container: `${virtualizerScrollViewDynamicClassName}__container`\n};\nconst useStyles = makeStyles({\n base: {\n display: 'flex',\n width: '100%',\n height: '100%'\n },\n vertical: {\n flexDirection: 'column',\n overflowY: 'auto'\n },\n horizontal: {\n flexDirection: 'row',\n overflowX: 'auto'\n },\n verticalReversed: {\n flexDirection: 'column-reverse',\n overflowY: 'auto'\n },\n horizontalReversed: {\n flexDirection: 'row-reverse',\n overflowX: 'auto'\n }\n});\n/**\n * Apply styling to the Virtualizer states\n */ export const useVirtualizerScrollViewDynamicStyles_unstable = (state)=>{\n 'use no memo';\n const styles = useStyles();\n // Default virtualizer styles base\n useVirtualizerStyles_unstable(state);\n const containerStyle = state.axis === 'horizontal' ? state.reversed ? styles.horizontalReversed : styles.horizontal : state.reversed ? styles.verticalReversed : styles.vertical;\n // Add container styles\n state.container.className = mergeClasses(virtualizerScrollViewDynamicClassNames.container, styles.base, containerStyle, state.container.className);\n return state;\n};\n"],"mappings":"AAAA,SAASA,6BAA6B,EAAEC,qBAAqB,QAAQ,4CAA4C;AACjH,SAAAC,QAAA,EAAqBC,YAAY,QAAQ,gBAAgB;AACzD,MAAMC,qCAAqC,GAAG,qCAAqC;AACnF,OAAO,MAAMC,sCAAsC,GAAG;EAClD,GAAGJ,qBAAqB;EACxBK,SAAS,EAAE,GAAGF,qCAAqC;AACvD,CAAC;AACD,MAAMG,SAAS,gBAAGL,QAAA;EAAAM,IAAA;IAAAC,MAAA;IAAAC,MAAA;IAAAC,OAAA;EAAA;EAAAC,QAAA;IAAAC,OAAA;IAAAC,OAAA;EAAA;EAAAC,UAAA;IAAAF,OAAA;IAAAG,OAAA;EAAA;EAAAC,gBAAA;IAAAJ,OAAA;IAAAC,OAAA;EAAA;EAAAI,kBAAA;IAAAL,OAAA;IAAAG,OAAA;EAAA;AAAA;EAAAG,CAAA;AAAA,CAsBjB,CAAC;AACF;AACA;AACA;AAAI,OAAO,MAAMC,8CAA8C,GAAIC,KAAK,IAAG;EACvE,aAAa;;EACb,MAAMC,MAAM,GAAGf,SAAS,CAAC,CAAC;EAC1B;EACAP,6BAA6B,CAACqB,KAAK,CAAC;EACpC,MAAME,cAAc,GAAGF,KAAK,CAACG,IAAI,KAAK,YAAY,GAAGH,KAAK,CAACI,QAAQ,GAAGH,MAAM,CAACJ,kBAAkB,GAAGI,MAAM,CAACP,UAAU,GAAGM,KAAK,CAACI,QAAQ,GAAGH,MAAM,CAACL,gBAAgB,GAAGK,MAAM,CAACV,QAAQ;EAChL;EACAS,KAAK,CAACf,SAAS,CAACoB,SAAS,GAAGvB,YAAY,CAACE,sCAAsC,CAACC,SAAS,EAAEgB,MAAM,CAACd,IAAI,EAAEe,cAAc,EAAEF,KAAK,CAACf,SAAS,CAACoB,SAAS,CAAC;EAClJ,OAAOL,KAAK;AAChB,CAAC","ignoreList":[]}
@@ -0,0 +1,4 @@
1
+ /**
2
+ * Additional direct Ref prevents reading old resize entry data
3
+ * Backwards compatible with ResizeObserverCallback if preferred
4
+ */ export { };
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["hooks.types.ts"],"sourcesContent":["import { MutableRefObject, RefObject } from 'react';\n\nexport type VirtualizerMeasureProps = {\n defaultItemSize: number;\n direction?: 'vertical' | 'horizontal';\n};\n\nexport type VirtualizerMeasureDynamicProps = {\n defaultItemSize: number;\n currentIndex: number;\n numItems: number;\n getItemSize: (index: number) => number;\n direction?: 'vertical' | 'horizontal';\n};\n\nexport type VirtualizerStaticPaginationProps = {\n itemSize: number;\n axis?: 'vertical' | 'horizontal';\n};\n\n/**\n * Props to be passed into dynamic virtualization hooks\n * All props can be acquired from useVirtualizer hooks themselves and passed in\n */\nexport type VirtualizerDynamicPaginationProps = {\n /**\n * An array that tracks the sizing of each item in virtualizer cumulatively\n */\n progressiveItemSizes: RefObject<number[]> | undefined;\n /**\n * The current starting index of the virtualizer's DOM elements\n */\n currentIndex: number;\n /**\n * The axis we should paginate on (should match virtualizer's axis)\n */\n axis?: 'vertical' | 'horizontal';\n /**\n * The current length of Virtualizer's actual DOM elements\n */\n virtualizerLength: number;\n};\n\n/**\n * Additional direct Ref prevents reading old resize entry data\n * Backwards compatible with ResizeObserverCallback if preferred\n */\nexport interface ResizeCallbackWithRef {\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?: MutableRefObject<HTMLElement | null>,\n ): void;\n}\n"],"names":[],"rangeMappings":";;;","mappings":"AA2CA;;;CAGC,GACD,WAQC"}
@@ -1,2 +1,5 @@
1
1
  export * from './useIntersectionObserver';
2
- //# sourceMappingURL=index.js.map
2
+ export * from './useVirtualizerMeasure';
3
+ export * from './useDynamicVirtualizerMeasure';
4
+ export * from './useResizeObserverRef';
5
+ export * from './hooks.types';
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"../src/","sources":["packages/react-components/react-virtualizer/src/hooks/index.ts"],"names":[],"mappings":"AAAA,cAAc,2BAA2B,CAAC","sourcesContent":["export * from './useIntersectionObserver';\n"]}
1
+ {"version":3,"sources":["index.ts"],"sourcesContent":["export * from './useIntersectionObserver';\nexport * from './useVirtualizerMeasure';\nexport * from './useDynamicVirtualizerMeasure';\nexport * from './useResizeObserverRef';\nexport * from './hooks.types';\n"],"names":[],"rangeMappings":";;;;","mappings":"AAAA,cAAc,4BAA4B;AAC1C,cAAc,0BAA0B;AACxC,cAAc,iCAAiC;AAC/C,cAAc,yBAAyB;AACvC,cAAc,gBAAgB"}
@@ -0,0 +1,127 @@
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 dynamic so we require a progressive sizing array (passed in from Dynamic virtualizer hooks)
7
+ * On short scrolls, we will go at minimum to the next/previous item so that arrow pagination works
8
+ * All VirtualizerDynamicPaginationProps can be grabbed from dynamic Virtualizer hooks externally and passed in
9
+ */ export const useDynamicVirtualizerPagination = (virtualizerProps, paginationEnabled = true)=>{
10
+ 'use no memo';
11
+ const { axis = 'vertical', currentIndex, progressiveItemSizes, virtualizerLength } = virtualizerProps;
12
+ const [setScrollTimer, clearScrollTimer] = useTimeout();
13
+ const lastScrollPos = useRef(-1);
14
+ const lastIndexScrolled = useRef(-1);
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
+ *
34
+ * Users/Virtualizer-Hooks must pass in a cumulative array of sizes
35
+ * This prevents the need to recalculate and ensures size arrays are synced externally
36
+ */ const onScrollEnd = React.useCallback(()=>{
37
+ if (!scrollContainer.current || !paginationEnabled || !(progressiveItemSizes === null || progressiveItemSizes === void 0 ? void 0 : progressiveItemSizes.current)) {
38
+ // No container found
39
+ return;
40
+ }
41
+ const currentScrollPos = Math.round(axis === 'vertical' ? scrollContainer.current.scrollTop : scrollContainer.current.scrollLeft);
42
+ let closestItemPos = 0;
43
+ let closestItem = 0;
44
+ const endItem = Math.min(currentIndex + virtualizerLength, progressiveItemSizes.current.length);
45
+ for(let i = currentIndex; i < endItem - 1; i++){
46
+ if (currentScrollPos <= progressiveItemSizes.current[i + 1] && currentScrollPos >= progressiveItemSizes.current[i]) {
47
+ // Found our in between position
48
+ const distanceToPrev = Math.abs(currentScrollPos - progressiveItemSizes.current[i]);
49
+ const distanceToNext = Math.abs(progressiveItemSizes.current[i + 1] - currentScrollPos);
50
+ if (distanceToPrev < distanceToNext) {
51
+ closestItem = i;
52
+ } else {
53
+ closestItem = i + 1;
54
+ }
55
+ break;
56
+ }
57
+ }
58
+ let nextItem;
59
+ if (Math.round(closestItem - lastIndexScrolled.current) === 0) {
60
+ // Special case for go to next/previous with minimum amount of scroll needed
61
+ const nextTarget = lastScrollPos.current < currentScrollPos ? 1 : -1;
62
+ // This will also handle a case where we scrolled to the exact correct position (noop)
63
+ const isSecondaryScroll = Math.round(lastScrollPos.current - currentScrollPos) === 0;
64
+ const posMod = isSecondaryScroll ? 0 : nextTarget;
65
+ nextItem = closestItem + posMod;
66
+ } else {
67
+ // Pagination for anything else can just jump to the closest!
68
+ nextItem = closestItem;
69
+ }
70
+ // Safeguard nextItem
71
+ nextItem = Math.min(Math.max(0, nextItem), progressiveItemSizes.current.length);
72
+ closestItemPos = progressiveItemSizes.current[nextItem];
73
+ if (axis === 'vertical') {
74
+ scrollContainer.current.scrollTo({
75
+ top: closestItemPos,
76
+ behavior: 'smooth'
77
+ });
78
+ } else {
79
+ scrollContainer.current.scrollTo({
80
+ left: closestItemPos,
81
+ behavior: 'smooth'
82
+ });
83
+ }
84
+ lastScrollPos.current = progressiveItemSizes.current[nextItem];
85
+ lastIndexScrolled.current = nextItem;
86
+ }, [
87
+ paginationEnabled,
88
+ currentIndex,
89
+ scrollContainer,
90
+ virtualizerLength,
91
+ axis,
92
+ progressiveItemSizes
93
+ ]);
94
+ /**
95
+ * On scroll timer that will continuously delay callback until scrolling stops
96
+ */ const onScroll = React.useCallback((event)=>{
97
+ clearScrollTimer();
98
+ setScrollTimer(onScrollEnd, 100);
99
+ }, [
100
+ onScrollEnd,
101
+ clearScrollTimer,
102
+ setScrollTimer
103
+ ]);
104
+ /**
105
+ * Pagination ref will ensure we attach listeners to containers on change
106
+ * It is returned from hook and merged into the scroll container externally
107
+ */ const paginationRef = React.useCallback((instance)=>{
108
+ if (!paginationEnabled) {
109
+ clearListeners();
110
+ scrollContainer.current = null;
111
+ return;
112
+ }
113
+ if (scrollContainer.current !== instance) {
114
+ clearListeners();
115
+ scrollContainer.current = instance;
116
+ if (scrollContainer.current) {
117
+ scrollContainer.current.addEventListener('scroll', onScroll);
118
+ }
119
+ }
120
+ }, // eslint-disable-next-line react-hooks/exhaustive-deps
121
+ [
122
+ onScroll,
123
+ onScrollEnd,
124
+ paginationEnabled
125
+ ]);
126
+ return paginationRef;
127
+ };
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["useDynamicPagination.ts"],"sourcesContent":["import * as React from 'react';\nimport { VirtualizerDynamicPaginationProps } 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 dynamic so we require a progressive sizing array (passed in from Dynamic virtualizer hooks)\n * On short scrolls, we will go at minimum to the next/previous item so that arrow pagination works\n * All VirtualizerDynamicPaginationProps can be grabbed from dynamic Virtualizer hooks externally and passed in\n */\nexport const useDynamicVirtualizerPagination = (\n virtualizerProps: VirtualizerDynamicPaginationProps,\n paginationEnabled: Boolean = true,\n) => {\n 'use no memo';\n\n const { axis = 'vertical', currentIndex, progressiveItemSizes, virtualizerLength } = virtualizerProps;\n\n const [setScrollTimer, clearScrollTimer] = useTimeout();\n const lastScrollPos = useRef<number>(-1);\n const lastIndexScrolled = useRef<number>(-1);\n\n const scrollContainer = React.useRef<HTMLElement | null>(null);\n\n const clearListeners = () => {\n if (scrollContainer.current) {\n scrollContainer.current.removeEventListener('scroll', onScroll);\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 * Users/Virtualizer-Hooks must pass in a cumulative array of sizes\n * This prevents the need to recalculate and ensures size arrays are synced externally\n */\n const onScrollEnd = React.useCallback(() => {\n if (!scrollContainer.current || !paginationEnabled || !progressiveItemSizes?.current) {\n // No container found\n return;\n }\n\n const currentScrollPos = Math.round(\n axis === 'vertical' ? scrollContainer.current.scrollTop : scrollContainer.current.scrollLeft,\n );\n let closestItemPos = 0;\n let closestItem = 0;\n const endItem = Math.min(currentIndex + virtualizerLength, progressiveItemSizes.current.length);\n\n for (let i = currentIndex; i < endItem - 1; i++) {\n if (\n currentScrollPos <= progressiveItemSizes.current[i + 1] &&\n currentScrollPos >= progressiveItemSizes.current[i]\n ) {\n // Found our in between position\n const distanceToPrev = Math.abs(currentScrollPos - progressiveItemSizes.current[i]);\n const distanceToNext = Math.abs(progressiveItemSizes.current[i + 1] - currentScrollPos);\n if (distanceToPrev < distanceToNext) {\n closestItem = i;\n } else {\n closestItem = i + 1;\n }\n break;\n }\n }\n\n let nextItem;\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 // This will also handle a case where we scrolled to the exact correct position (noop)\n const isSecondaryScroll = Math.round(lastScrollPos.current - currentScrollPos) === 0;\n const posMod = isSecondaryScroll ? 0 : nextTarget;\n nextItem = closestItem + posMod;\n } else {\n // Pagination for anything else can just jump to the closest!\n nextItem = closestItem;\n }\n\n // Safeguard nextItem\n nextItem = Math.min(Math.max(0, nextItem), progressiveItemSizes.current.length);\n closestItemPos = progressiveItemSizes.current[nextItem];\n\n if (axis === 'vertical') {\n scrollContainer.current.scrollTo({ top: closestItemPos, behavior: 'smooth' });\n } else {\n scrollContainer.current.scrollTo({ left: closestItemPos, behavior: 'smooth' });\n }\n lastScrollPos.current = progressiveItemSizes.current[nextItem];\n lastIndexScrolled.current = nextItem;\n }, [paginationEnabled, currentIndex, scrollContainer, virtualizerLength, axis, progressiveItemSizes]);\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","useDynamicVirtualizerPagination","virtualizerProps","paginationEnabled","axis","currentIndex","progressiveItemSizes","virtualizerLength","setScrollTimer","clearScrollTimer","lastScrollPos","lastIndexScrolled","scrollContainer","clearListeners","current","removeEventListener","onScroll","useEffect","onScrollEnd","useCallback","currentScrollPos","Math","round","scrollTop","scrollLeft","closestItemPos","closestItem","endItem","min","length","i","distanceToPrev","abs","distanceToNext","nextItem","nextTarget","isSecondaryScroll","posMod","max","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,kCAAkC,CAC7CC,kBACAC,oBAA6B,IAAI;IAEjC;IAEA,MAAM,EAAEC,OAAO,UAAU,EAAEC,YAAY,EAAEC,oBAAoB,EAAEC,iBAAiB,EAAE,GAAGL;IAErF,MAAM,CAACM,gBAAgBC,iBAAiB,GAAGT;IAC3C,MAAMU,gBAAgBX,OAAe,CAAC;IACtC,MAAMY,oBAAoBZ,OAAe,CAAC;IAE1C,MAAMa,kBAAkBd,MAAMC,MAAM,CAAqB;IAEzD,MAAMc,iBAAiB;QACrB,IAAID,gBAAgBE,OAAO,EAAE;YAC3BF,gBAAgBE,OAAO,CAACC,mBAAmB,CAAC,UAAUC;YACtDJ,gBAAgBE,OAAO,GAAG;YAC1BL;QACF;IACF;IAEAX,MAAMmB,SAAS,CAAC;QACd,OAAO;YACLJ;QACF;IACA,uDAAuD;IACzD,GAAG,EAAE;IAEL;;;;;;;GAOC,GACD,MAAMK,cAAcpB,MAAMqB,WAAW,CAAC;QACpC,IAAI,CAACP,gBAAgBE,OAAO,IAAI,CAACX,qBAAqB,EAACG,iCAAAA,2CAAAA,qBAAsBQ,OAAO,GAAE;YACpF,qBAAqB;YACrB;QACF;QAEA,MAAMM,mBAAmBC,KAAKC,KAAK,CACjClB,SAAS,aAAaQ,gBAAgBE,OAAO,CAACS,SAAS,GAAGX,gBAAgBE,OAAO,CAACU,UAAU;QAE9F,IAA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7B,gBAAgBE,OAAO,CAACyB,QAAQ,CAAC;gBAAEG,MAAMjB;gBAAgBgB,UAAU;YAAS;QAC9E;QACA/B,cAAcI,OAAO,GAAGR,qBAAqBQ,OAAO,CAACoB,SAAS;QAC9DvB,kBAAkBG,OAAO,GAAGoB;IAC9B,GAAG;QAAC/B;QAAmBE;QAAcO;QAAiBL;QAAmBH;QAAME;KAAqB;IAEpG;;GAEC,GACD,MAAMU,WAAWlB,MAAMqB,WAAW,CAChCwB,CAAAA;QACElC;QACAD,eAAeU,aAAa;IAC9B,GACA;QAACA;QAAaT;QAAkBD;KAAe;IAGjD;;;GAGC,GACD,MAAMoC,gBAAgB9C,MAAMqB,WAAW,CACrC,CAAC0B;QACC,IAAI,CAAC1C,mBAAmB;YACtBU;YACAD,gBAAgBE,OAAO,GAAG;YAC1B;QACF;QACA,IAAIF,gBAAgBE,OAAO,KAAK+B,UAAU;YACxChC;YAEAD,gBAAgBE,OAAO,GAAG+B;YAC1B,IAAIjC,gBAAgBE,OAAO,EAAE;gBAC3BF,gBAAgBE,OAAO,CAACgC,gBAAgB,CAAC,UAAU9B;YACrD;QACF;IACF,GACA,uDAAuD;IACvD;QAACA;QAAUE;QAAaf;KAAkB;IAG5C,OAAOyC;AACT,EAAE"}
@@ -0,0 +1,101 @@
1
+ import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';
2
+ import * as React from 'react';
3
+ import { useResizeObserverRef_unstable } from './useResizeObserverRef';
4
+ import { useRef } from 'react';
5
+ /**
6
+ * React hook that measures virtualized space dynamically to ensure optimized virtualization length.
7
+ */ export const useDynamicVirtualizerMeasure = (virtualizerProps)=>{
8
+ const { defaultItemSize, direction = 'vertical', numItems, getItemSize, currentIndex } = virtualizerProps;
9
+ const indexRef = useRef(currentIndex);
10
+ indexRef.current = currentIndex;
11
+ const [state, setState] = React.useState({
12
+ virtualizerLength: 0,
13
+ virtualizerBufferItems: 0,
14
+ virtualizerBufferSize: 0
15
+ });
16
+ const { virtualizerLength, virtualizerBufferItems, virtualizerBufferSize } = state;
17
+ const container = React.useRef(null);
18
+ const handleScrollResize = React.useCallback((scrollRef)=>{
19
+ if (!(scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current)) {
20
+ // Error? ignore?
21
+ return;
22
+ }
23
+ if (scrollRef.current !== container.current) {
24
+ container.current = scrollRef.current;
25
+ }
26
+ const containerSize = direction === 'vertical' ? scrollRef.current.getBoundingClientRect().height : scrollRef.current.getBoundingClientRect().width;
27
+ let indexSizer = 0;
28
+ let length = 0;
29
+ while(indexSizer <= containerSize && length < numItems){
30
+ const iItemSize = getItemSize(indexRef.current + length);
31
+ // Increment
32
+ indexSizer += iItemSize;
33
+ length++;
34
+ }
35
+ /*
36
+ * Number of items to append at each end, i.e. 'preload' each side before entering view.
37
+ */ const bufferItems = Math.max(Math.floor(length / 4), 4);
38
+ /*
39
+ * This is how far we deviate into the bufferItems to detect a redraw.
40
+ */ const bufferSize = Math.max(Math.floor(length / 8 * defaultItemSize), 1);
41
+ const totalLength = length + bufferItems * 2 + 1;
42
+ setState({
43
+ virtualizerLength: totalLength,
44
+ virtualizerBufferSize: bufferSize,
45
+ virtualizerBufferItems: bufferItems
46
+ });
47
+ }, [
48
+ defaultItemSize,
49
+ direction,
50
+ getItemSize,
51
+ numItems
52
+ ]);
53
+ const resizeCallback = React.useCallback((_entries, // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286
54
+ // eslint-disable-next-line no-restricted-globals
55
+ _observer, scrollRef)=>{
56
+ if (scrollRef) {
57
+ handleScrollResize(scrollRef);
58
+ }
59
+ }, [
60
+ handleScrollResize
61
+ ]);
62
+ const scrollRef = useResizeObserverRef_unstable(resizeCallback);
63
+ useIsomorphicLayoutEffect(()=>{
64
+ var _container_current, _container_current1;
65
+ if (!container.current) {
66
+ return;
67
+ }
68
+ const containerSize = direction === 'vertical' ? ((_container_current = container.current) === null || _container_current === void 0 ? void 0 : _container_current.getBoundingClientRect().height) * 1.5 : ((_container_current1 = container.current) === null || _container_current1 === void 0 ? void 0 : _container_current1.getBoundingClientRect().width) * 1.5;
69
+ let couldBeSmaller = false;
70
+ let recheckTotal = 0;
71
+ for(let i = currentIndex; i < currentIndex + virtualizerLength; i++){
72
+ const newItemSize = getItemSize(i);
73
+ recheckTotal += newItemSize;
74
+ const newLength = i - currentIndex;
75
+ const bufferItems = Math.max(Math.floor(newLength / 4), 2);
76
+ const totalNewLength = newLength + bufferItems * 2 + 4;
77
+ const compareLengths = totalNewLength < virtualizerLength;
78
+ if (recheckTotal > containerSize && compareLengths) {
79
+ couldBeSmaller = true;
80
+ break;
81
+ }
82
+ }
83
+ // Check if the render has caused us to need a re-calc of virtualizer length
84
+ if (recheckTotal < containerSize || couldBeSmaller) {
85
+ handleScrollResize(container);
86
+ }
87
+ }, [
88
+ getItemSize,
89
+ currentIndex,
90
+ direction,
91
+ virtualizerLength,
92
+ resizeCallback,
93
+ handleScrollResize
94
+ ]);
95
+ return {
96
+ virtualizerLength,
97
+ bufferItems: virtualizerBufferItems,
98
+ bufferSize: virtualizerBufferSize,
99
+ scrollRef
100
+ };
101
+ };
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["useDynamicVirtualizerMeasure.ts"],"sourcesContent":["import { useIsomorphicLayoutEffect } from '@fluentui/react-utilities';\nimport * as React from 'react';\nimport { VirtualizerMeasureDynamicProps } from './hooks.types';\nimport { useResizeObserverRef_unstable } from './useResizeObserverRef';\nimport { useRef } from 'react';\n\n/**\n * React hook that measures virtualized space dynamically to ensure optimized virtualization length.\n */\nexport const useDynamicVirtualizerMeasure = <TElement extends HTMLElement>(\n virtualizerProps: VirtualizerMeasureDynamicProps,\n): {\n virtualizerLength: number;\n bufferItems: number;\n bufferSize: number;\n scrollRef: (instance: TElement | null) => void;\n} => {\n const { defaultItemSize, direction = 'vertical', numItems, getItemSize, currentIndex } = virtualizerProps;\n const indexRef = useRef<number>(currentIndex);\n indexRef.current = currentIndex;\n\n const [state, setState] = React.useState({\n virtualizerLength: 0,\n virtualizerBufferItems: 0,\n virtualizerBufferSize: 0,\n });\n\n const { virtualizerLength, virtualizerBufferItems, virtualizerBufferSize } = state;\n\n const container = React.useRef<HTMLElement | null>(null);\n const handleScrollResize = React.useCallback(\n (scrollRef: React.MutableRefObject<HTMLElement | null>) => {\n if (!scrollRef?.current) {\n // Error? ignore?\n return;\n }\n\n if (scrollRef.current !== container.current) {\n container.current = scrollRef.current;\n }\n\n const containerSize =\n direction === 'vertical'\n ? scrollRef.current.getBoundingClientRect().height\n : scrollRef.current.getBoundingClientRect().width;\n\n let indexSizer = 0;\n let length = 0;\n\n while (indexSizer <= containerSize && length < numItems) {\n const iItemSize = getItemSize(indexRef.current + length);\n\n // Increment\n indexSizer += iItemSize;\n length++;\n }\n\n /*\n * Number of items to append at each end, i.e. 'preload' each side before entering view.\n */\n const bufferItems = Math.max(Math.floor(length / 4), 4);\n\n /*\n * This is how far we deviate into the bufferItems to detect a redraw.\n */\n const bufferSize = Math.max(Math.floor((length / 8) * defaultItemSize), 1);\n\n const totalLength = length + bufferItems * 2 + 1;\n setState({\n virtualizerLength: totalLength,\n virtualizerBufferSize: bufferSize,\n virtualizerBufferItems: bufferItems,\n });\n },\n [defaultItemSize, direction, getItemSize, numItems],\n );\n\n const resizeCallback = React.useCallback(\n (\n _entries: ResizeObserverEntry[],\n // TODO: exclude types from this lint rule: https://github.com/microsoft/fluentui/issues/31286\n // eslint-disable-next-line no-restricted-globals\n _observer: ResizeObserver,\n scrollRef?: React.MutableRefObject<HTMLElement | null>,\n ) => {\n if (scrollRef) {\n handleScrollResize(scrollRef);\n }\n },\n [handleScrollResize],\n );\n\n const scrollRef = useResizeObserverRef_unstable(resizeCallback);\n\n useIsomorphicLayoutEffect(() => {\n if (!container.current) {\n return;\n }\n\n const containerSize =\n direction === 'vertical'\n ? container.current?.getBoundingClientRect().height * 1.5\n : container.current?.getBoundingClientRect().width * 1.5;\n\n let couldBeSmaller = false;\n let recheckTotal = 0;\n for (let i = currentIndex; i < currentIndex + virtualizerLength; i++) {\n const newItemSize = getItemSize(i);\n recheckTotal += newItemSize;\n\n const newLength = i - currentIndex;\n\n const bufferItems = Math.max(Math.floor(newLength / 4), 2);\n const totalNewLength = newLength + bufferItems * 2 + 4;\n const compareLengths = totalNewLength < virtualizerLength;\n\n if (recheckTotal > containerSize && compareLengths) {\n couldBeSmaller = true;\n break;\n }\n }\n\n // Check if the render has caused us to need a re-calc of virtualizer length\n if (recheckTotal < containerSize || couldBeSmaller) {\n handleScrollResize(container);\n }\n }, [getItemSize, currentIndex, direction, virtualizerLength, resizeCallback, handleScrollResize]);\n\n return {\n virtualizerLength,\n bufferItems: virtualizerBufferItems,\n bufferSize: virtualizerBufferSize,\n scrollRef,\n };\n};\n"],"names":["useIsomorphicLayoutEffect","React","useResizeObserverRef_unstable","useRef","useDynamicVirtualizerMeasure","virtualizerProps","defaultItemSize","direction","numItems","getItemSize","currentIndex","indexRef","current","state","setState","useState","virtualizerLength","virtualizerBufferItems","virtualizerBufferSize","container","handleScrollResize","useCallback","scrollRef","containerSize","getBoundingClientRect","height","width","indexSizer","length","iItemSize","bufferItems","Math","max","floor","bufferSize","totalLength","resizeCallback","_entries","_observer","couldBeSmaller","recheckTotal","i","newItemSize","newLength","totalNewLength","compareLengths"],"rangeMappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;","mappings":"AAAA,SAASA,yBAAyB,QAAQ,4BAA4B;AACtE,YAAYC,WAAW,QAAQ;AAE/B,SAASC,6BAA6B,QAAQ,yBAAyB;AACvE,SAASC,MAAM,QAAQ,QAAQ;AAE/B;;CAEC,GACD,OAAO,MAAMC,+BAA+B,CAC1CC;IAOA,MAAM,EAAEC,eAAe,EAAEC,YAAY,UAAU,EAAEC,QAAQ,EAAEC,WAAW,EAAEC,YAAY,EAAE,GAAGL;IACzF,MAAMM,WAAWR,OAAeO;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