@fluentui/react-virtualizer 9.0.0-alpha.69 → 9.0.0-alpha.70
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.
- package/CHANGELOG.md +14 -2
- package/dist/index.d.ts +8 -0
- package/lib/components/VirtualizerScrollView/VirtualizerScrollView.types.js.map +1 -1
- package/lib/components/VirtualizerScrollView/useVirtualizerScrollView.js +7 -2
- package/lib/components/VirtualizerScrollView/useVirtualizerScrollView.js.map +1 -1
- package/lib/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.types.js.map +1 -1
- package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js +23 -12
- package/lib/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js.map +1 -1
- package/lib/hooks/hooks.types.js.map +1 -1
- package/lib/hooks/useDynamicPagination.js +127 -0
- package/lib/hooks/useDynamicPagination.js.map +1 -0
- package/lib/hooks/useMeasureList.js +17 -2
- package/lib/hooks/useMeasureList.js.map +1 -1
- package/lib/hooks/useStaticPagination.js +103 -0
- package/lib/hooks/useStaticPagination.js.map +1 -0
- package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollView.js +7 -2
- package/lib-commonjs/components/VirtualizerScrollView/useVirtualizerScrollView.js.map +1 -1
- package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js +20 -10
- package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js.map +1 -1
- package/lib-commonjs/hooks/useDynamicPagination.js +131 -0
- package/lib-commonjs/hooks/useDynamicPagination.js.map +1 -0
- package/lib-commonjs/hooks/useMeasureList.js +16 -2
- package/lib-commonjs/hooks/useMeasureList.js.map +1 -1
- package/lib-commonjs/hooks/useStaticPagination.js +107 -0
- package/lib-commonjs/hooks/useStaticPagination.js.map +1 -0
- package/package.json +4 -4
package/CHANGELOG.md
CHANGED
|
@@ -1,12 +1,24 @@
|
|
|
1
1
|
# Change Log - @fluentui/react-virtualizer
|
|
2
2
|
|
|
3
|
-
This log was last generated on
|
|
3
|
+
This log was last generated on Wed, 28 Feb 2024 02:28:40 GMT and should not be manually modified.
|
|
4
4
|
|
|
5
5
|
<!-- Start content -->
|
|
6
6
|
|
|
7
|
+
## [9.0.0-alpha.70](https://github.com/microsoft/fluentui/tree/@fluentui/react-virtualizer_v9.0.0-alpha.70)
|
|
8
|
+
|
|
9
|
+
Wed, 28 Feb 2024 02:28:40 GMT
|
|
10
|
+
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-virtualizer_v9.0.0-alpha.69..@fluentui/react-virtualizer_v9.0.0-alpha.70)
|
|
11
|
+
|
|
12
|
+
### Changes
|
|
13
|
+
|
|
14
|
+
- feat: Add pagination for both static and dynamic virtualizers ([PR #30560](https://github.com/microsoft/fluentui/pull/30560) by mifraser@microsoft.com)
|
|
15
|
+
- Bump @fluentui/react-jsx-runtime to v9.0.31 ([PR #30639](https://github.com/microsoft/fluentui/pull/30639) by beachball)
|
|
16
|
+
- Bump @fluentui/react-utilities to v9.18.2 ([PR #30639](https://github.com/microsoft/fluentui/pull/30639) by beachball)
|
|
17
|
+
- Bump @fluentui/react-shared-contexts to v9.14.1 ([PR #30639](https://github.com/microsoft/fluentui/pull/30639) by beachball)
|
|
18
|
+
|
|
7
19
|
## [9.0.0-alpha.69](https://github.com/microsoft/fluentui/tree/@fluentui/react-virtualizer_v9.0.0-alpha.69)
|
|
8
20
|
|
|
9
|
-
Tue, 20 Feb 2024 14:
|
|
21
|
+
Tue, 20 Feb 2024 14:22:29 GMT
|
|
10
22
|
[Compare changes](https://github.com/microsoft/fluentui/compare/@fluentui/react-virtualizer_v9.0.0-alpha.68..@fluentui/react-virtualizer_v9.0.0-alpha.69)
|
|
11
23
|
|
|
12
24
|
### Changes
|
package/dist/index.d.ts
CHANGED
|
@@ -321,6 +321,10 @@ export declare type VirtualizerScrollViewDynamicProps = ComponentProps<Partial<V
|
|
|
321
321
|
* Imperative ref contains our scrollTo index functionality for user control.
|
|
322
322
|
*/
|
|
323
323
|
imperativeRef?: RefObject<ScrollToInterface>;
|
|
324
|
+
/**
|
|
325
|
+
* Imperative ref contains our scrollTo index functionality for user control.
|
|
326
|
+
*/
|
|
327
|
+
enablePagination?: boolean;
|
|
324
328
|
};
|
|
325
329
|
|
|
326
330
|
export declare type VirtualizerScrollViewDynamicSlots = VirtualizerScrollViewSlots;
|
|
@@ -348,6 +352,10 @@ export declare type VirtualizerScrollViewProps = ComponentProps<Partial<Virtuali
|
|
|
348
352
|
* Imperative ref contains our scrollTo index functionality for user control.
|
|
349
353
|
*/
|
|
350
354
|
imperativeRef?: RefObject<ScrollToInterface>;
|
|
355
|
+
/**
|
|
356
|
+
* Imperative ref contains our scrollTo index functionality for user control.
|
|
357
|
+
*/
|
|
358
|
+
enablePagination?: boolean;
|
|
351
359
|
};
|
|
352
360
|
|
|
353
361
|
export declare type VirtualizerScrollViewSlots = VirtualizerSlots & {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["VirtualizerScrollView.types.ts"],"sourcesContent":["import type { ComponentProps, ComponentState, Slot } from '@fluentui/react-utilities';\nimport type {\n VirtualizerSlots,\n VirtualizerConfigProps,\n VirtualizerConfigState,\n VirtualizerChildRenderFunction,\n} from '../Virtualizer/Virtualizer.types';\nimport type { ScrollToInterface } from '../../Utilities';\nimport type { RefObject } from 'react';\n\nexport type VirtualizerScrollViewSlots = VirtualizerSlots & {\n /**\n * The root container that provides embedded scrolling.\n */\n container: NonNullable<Slot<'div'>>;\n};\n\nexport type VirtualizerScrollViewProps = ComponentProps<Partial<VirtualizerScrollViewSlots>> &\n Partial<\n Omit<\n VirtualizerConfigProps,\n 'itemSize' | 'numItems' | 'getItemSize' | 'children' | 'flagIndex' | 'imperativeVirtualizerRef'\n >\n > & {\n /**\n * Virtualizer item size in pixels - static.\n * Axis: 'vertical' = Height\n * Axis: 'horizontal' = Width\n */\n itemSize: number;\n /**\n * The total number of items to be virtualized.\n */\n numItems: number;\n /**\n * Child render function.\n * Iteratively called to return current virtualizer DOM children.\n * Will act as a row or column indexer depending on Virtualizer settings.\n */\n children: VirtualizerChildRenderFunction;\n /**\n * Imperative ref contains our scrollTo index functionality for user control.\n */\n imperativeRef?: RefObject<ScrollToInterface>;\n };\n\nexport type VirtualizerScrollViewState = ComponentState<VirtualizerScrollViewSlots> & VirtualizerConfigState;\n"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"sources":["VirtualizerScrollView.types.ts"],"sourcesContent":["import type { ComponentProps, ComponentState, Slot } from '@fluentui/react-utilities';\nimport type {\n VirtualizerSlots,\n VirtualizerConfigProps,\n VirtualizerConfigState,\n VirtualizerChildRenderFunction,\n} from '../Virtualizer/Virtualizer.types';\nimport type { ScrollToInterface } from '../../Utilities';\nimport type { RefObject } from 'react';\n\nexport type VirtualizerScrollViewSlots = VirtualizerSlots & {\n /**\n * The root container that provides embedded scrolling.\n */\n container: NonNullable<Slot<'div'>>;\n};\n\nexport type VirtualizerScrollViewProps = ComponentProps<Partial<VirtualizerScrollViewSlots>> &\n Partial<\n Omit<\n VirtualizerConfigProps,\n 'itemSize' | 'numItems' | 'getItemSize' | 'children' | 'flagIndex' | 'imperativeVirtualizerRef'\n >\n > & {\n /**\n * Virtualizer item size in pixels - static.\n * Axis: 'vertical' = Height\n * Axis: 'horizontal' = Width\n */\n itemSize: number;\n /**\n * The total number of items to be virtualized.\n */\n numItems: number;\n /**\n * Child render function.\n * Iteratively called to return current virtualizer DOM children.\n * Will act as a row or column indexer depending on Virtualizer settings.\n */\n children: VirtualizerChildRenderFunction;\n /**\n * Imperative ref contains our scrollTo index functionality for user control.\n */\n imperativeRef?: RefObject<ScrollToInterface>;\n /**\n * Imperative ref contains our scrollTo index functionality for user control.\n */\n enablePagination?: boolean;\n };\n\nexport type VirtualizerScrollViewState = ComponentState<VirtualizerScrollViewSlots> & VirtualizerConfigState;\n"],"names":[],"mappings":"AAAA,WAkD6G"}
|
|
@@ -4,8 +4,9 @@ import { useVirtualizer_unstable } from '../Virtualizer/useVirtualizer';
|
|
|
4
4
|
import { useStaticVirtualizerMeasure } from '../../Hooks';
|
|
5
5
|
import { useImperativeHandle } from 'react';
|
|
6
6
|
import { scrollToItemStatic } from '../../Utilities';
|
|
7
|
+
import { useStaticVirtualizerPagination } from '../../hooks/useStaticPagination';
|
|
7
8
|
export function useVirtualizerScrollView_unstable(props) {
|
|
8
|
-
const { imperativeRef, itemSize, numItems, axis = 'vertical', reversed } = props;
|
|
9
|
+
const { imperativeRef, itemSize, numItems, axis = 'vertical', reversed, enablePagination = false } = props;
|
|
9
10
|
var _props_axis;
|
|
10
11
|
const { virtualizerLength, bufferItems, bufferSize, scrollRef } = useStaticVirtualizerMeasure({
|
|
11
12
|
defaultItemSize: props.itemSize,
|
|
@@ -16,7 +17,11 @@ export function useVirtualizerScrollView_unstable(props) {
|
|
|
16
17
|
if (virtualizerLengthRef.current !== virtualizerLength) {
|
|
17
18
|
virtualizerLengthRef.current = virtualizerLength;
|
|
18
19
|
}
|
|
19
|
-
const
|
|
20
|
+
const paginationRef = useStaticVirtualizerPagination({
|
|
21
|
+
axis,
|
|
22
|
+
itemSize
|
|
23
|
+
}, enablePagination);
|
|
24
|
+
const scrollViewRef = useMergedRefs(props.scrollViewRef, scrollRef, paginationRef);
|
|
20
25
|
const imperativeVirtualizerRef = React.useRef(null);
|
|
21
26
|
const scrollCallbackRef = React.useRef(null);
|
|
22
27
|
useImperativeHandle(imperativeRef, ()=>{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useVirtualizerScrollView.ts"],"sourcesContent":["import * as React from 'react';\nimport { slot, useMergedRefs } from '@fluentui/react-utilities';\nimport { useVirtualizer_unstable } from '../Virtualizer/useVirtualizer';\nimport type { VirtualizerScrollViewProps, VirtualizerScrollViewState } from './VirtualizerScrollView.types';\nimport { useStaticVirtualizerMeasure } from '../../Hooks';\nimport { useImperativeHandle } from 'react';\nimport { scrollToItemStatic } from '../../Utilities';\nimport type { VirtualizerDataRef } from '../Virtualizer/Virtualizer.types';\n\nexport function useVirtualizerScrollView_unstable(props: VirtualizerScrollViewProps): VirtualizerScrollViewState {\n const { imperativeRef, itemSize, numItems, axis = 'vertical', reversed } = props;\n const { virtualizerLength, bufferItems, bufferSize, scrollRef } = useStaticVirtualizerMeasure({\n defaultItemSize: props.itemSize,\n direction: props.axis ?? 'vertical',\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) as React.RefObject<HTMLDivElement>;\n const imperativeVirtualizerRef = React.useRef<VirtualizerDataRef | null>(null);\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: ((index: number) => void) | undefined) {\n scrollCallbackRef.current = callback ?? null;\n imperativeVirtualizerRef.current?.setFlaggedIndex(index);\n scrollToItemStatic({\n index,\n itemSize,\n totalItems: numItems,\n scrollViewRef,\n axis,\n reversed,\n behavior,\n });\n },\n currentIndex: imperativeVirtualizerRef.current?.currentIndex,\n virtualizerLength: virtualizerLengthRef,\n };\n },\n [axis, scrollViewRef, itemSize, numItems, reversed],\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 virtualizerLength,\n bufferItems,\n bufferSize,\n scrollViewRef,\n onRenderedFlaggedIndex: handleRenderedIndex,\n imperativeVirtualizerRef,\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 as React.RefObject<HTMLDivElement>,\n },\n elementType: 'div',\n }),\n };\n}\n"],"names":["React","slot","useMergedRefs","useVirtualizer_unstable","useStaticVirtualizerMeasure","useImperativeHandle","scrollToItemStatic","useVirtualizerScrollView_unstable","props","imperativeRef","itemSize","numItems","axis","reversed","virtualizerLength","bufferItems","bufferSize","scrollRef","defaultItemSize","direction","virtualizerLengthRef","useRef","current","scrollViewRef","imperativeVirtualizerRef","scrollCallbackRef","scrollTo","index","behavior","callback","setFlaggedIndex","totalItems","currentIndex","handleRenderedIndex","virtualizerState","onRenderedFlaggedIndex","components","container","always","defaultProps","ref","elementType"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,IAAI,EAAEC,aAAa,QAAQ,4BAA4B;AAChE,SAASC,uBAAuB,QAAQ,gCAAgC;AAExE,SAASC,2BAA2B,QAAQ,cAAc;AAC1D,SAASC,mBAAmB,QAAQ,QAAQ;AAC5C,SAASC,kBAAkB,QAAQ,kBAAkB;
|
|
1
|
+
{"version":3,"sources":["useVirtualizerScrollView.ts"],"sourcesContent":["import * as React from 'react';\nimport { slot, useMergedRefs } from '@fluentui/react-utilities';\nimport { useVirtualizer_unstable } from '../Virtualizer/useVirtualizer';\nimport type { VirtualizerScrollViewProps, VirtualizerScrollViewState } from './VirtualizerScrollView.types';\nimport { useStaticVirtualizerMeasure } from '../../Hooks';\nimport { useImperativeHandle } from 'react';\nimport { scrollToItemStatic } from '../../Utilities';\nimport type { VirtualizerDataRef } from '../Virtualizer/Virtualizer.types';\nimport { useStaticVirtualizerPagination } from '../../hooks/useStaticPagination';\n\nexport function useVirtualizerScrollView_unstable(props: VirtualizerScrollViewProps): VirtualizerScrollViewState {\n const { imperativeRef, itemSize, numItems, axis = 'vertical', reversed, enablePagination = false } = props;\n const { virtualizerLength, bufferItems, bufferSize, scrollRef } = useStaticVirtualizerMeasure({\n defaultItemSize: props.itemSize,\n direction: props.axis ?? 'vertical',\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\n const paginationRef = useStaticVirtualizerPagination({ axis, itemSize }, enablePagination);\n const scrollViewRef = useMergedRefs(props.scrollViewRef, scrollRef, paginationRef) as React.RefObject<HTMLDivElement>;\n const imperativeVirtualizerRef = React.useRef<VirtualizerDataRef | null>(null);\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: ((index: number) => void) | undefined) {\n scrollCallbackRef.current = callback ?? null;\n imperativeVirtualizerRef.current?.setFlaggedIndex(index);\n scrollToItemStatic({\n index,\n itemSize,\n totalItems: numItems,\n scrollViewRef,\n axis,\n reversed,\n behavior,\n });\n },\n currentIndex: imperativeVirtualizerRef.current?.currentIndex,\n virtualizerLength: virtualizerLengthRef,\n };\n },\n [axis, scrollViewRef, itemSize, numItems, reversed],\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 virtualizerLength,\n bufferItems,\n bufferSize,\n scrollViewRef,\n onRenderedFlaggedIndex: handleRenderedIndex,\n imperativeVirtualizerRef,\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 as React.RefObject<HTMLDivElement>,\n },\n elementType: 'div',\n }),\n };\n}\n"],"names":["React","slot","useMergedRefs","useVirtualizer_unstable","useStaticVirtualizerMeasure","useImperativeHandle","scrollToItemStatic","useStaticVirtualizerPagination","useVirtualizerScrollView_unstable","props","imperativeRef","itemSize","numItems","axis","reversed","enablePagination","virtualizerLength","bufferItems","bufferSize","scrollRef","defaultItemSize","direction","virtualizerLengthRef","useRef","current","paginationRef","scrollViewRef","imperativeVirtualizerRef","scrollCallbackRef","scrollTo","index","behavior","callback","setFlaggedIndex","totalItems","currentIndex","handleRenderedIndex","virtualizerState","onRenderedFlaggedIndex","components","container","always","defaultProps","ref","elementType"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,IAAI,EAAEC,aAAa,QAAQ,4BAA4B;AAChE,SAASC,uBAAuB,QAAQ,gCAAgC;AAExE,SAASC,2BAA2B,QAAQ,cAAc;AAC1D,SAASC,mBAAmB,QAAQ,QAAQ;AAC5C,SAASC,kBAAkB,QAAQ,kBAAkB;AAErD,SAASC,8BAA8B,QAAQ,kCAAkC;AAEjF,OAAO,SAASC,kCAAkCC,KAAiC;IACjF,MAAM,EAAEC,aAAa,EAAEC,QAAQ,EAAEC,QAAQ,EAAEC,OAAO,UAAU,EAAEC,QAAQ,EAAEC,mBAAmB,KAAK,EAAE,GAAGN;QAGxFA;IAFb,MAAM,EAAEO,iBAAiB,EAAEC,WAAW,EAAEC,UAAU,EAAEC,SAAS,EAAE,GAAGf,4BAA4B;QAC5FgB,iBAAiBX,MAAME,QAAQ;QAC/BU,WAAWZ,CAAAA,cAAAA,MAAMI,IAAI,cAAVJ,yBAAAA,cAAc;IAC3B;IAEA,kEAAkE;IAClE,MAAMa,uBAAuBtB,MAAMuB,MAAM,CAASP;IAClD,IAAIM,qBAAqBE,OAAO,KAAKR,mBAAmB;QACtDM,qBAAqBE,OAAO,GAAGR;IACjC;IAEA,MAAMS,gBAAgBlB,+BAA+B;QAAEM;QAAMF;IAAS,GAAGI;IACzE,MAAMW,gBAAgBxB,cAAcO,MAAMiB,aAAa,EAAEP,WAAWM;IACpE,MAAME,2BAA2B3B,MAAMuB,MAAM,CAA4B;IACzE,MAAMK,oBAAoB5B,MAAMuB,MAAM,CAAmC;IAEzElB,oBACEK,eACA;YAekBiB;QAdhB,OAAO;YACLE,UAASC,KAAa,EAAEC,WAAW,MAAM,EAAEC,QAA+C;oBAExFL;gBADAC,kBAAkBJ,OAAO,GAAGQ,qBAAAA,sBAAAA,WAAY;iBACxCL,oCAAAA,yBAAyBH,OAAO,cAAhCG,wDAAAA,kCAAkCM,eAAe,CAACH;gBAClDxB,mBAAmB;oBACjBwB;oBACAnB;oBACAuB,YAAYtB;oBACZc;oBACAb;oBACAC;oBACAiB;gBACF;YACF;YACAI,YAAY,GAAER,oCAAAA,yBAAyBH,OAAO,cAAhCG,wDAAAA,kCAAkCQ,YAAY;YAC5DnB,mBAAmBM;QACrB;IACF,GACA;QAACT;QAAMa;QAAef;QAAUC;QAAUE;KAAS;IAGrD,MAAMsB,sBAAsB,CAACN;QAC3B,IAAIF,kBAAkBJ,OAAO,EAAE;YAC7BI,kBAAkBJ,OAAO,CAACM;QAC5B;IACF;IAEA,MAAMO,mBAAmBlC,wBAAwB;QAC/C,GAAGM,KAAK;QACRO;QACAC;QACAC;QACAQ;QACAY,wBAAwBF;QACxBT;IACF;IAEA,OAAO;QACL,GAAGU,gBAAgB;QACnBE,YAAY;YACV,GAAGF,iBAAiBE,UAAU;YAC9BC,WAAW;QACb;QACAA,WAAWvC,KAAKwC,MAAM,CAAChC,MAAM+B,SAAS,EAAE;YACtCE,cAAc;gBACZC,KAAKjB;YACP;YACAkB,aAAa;QACf;IACF;AACF"}
|
package/lib/components/VirtualizerScrollViewDynamic/VirtualizerScrollViewDynamic.types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["VirtualizerScrollViewDynamic.types.ts"],"sourcesContent":["import type { ComponentProps, ComponentState } from '@fluentui/react-utilities';\nimport type {\n VirtualizerConfigProps,\n VirtualizerConfigState,\n VirtualizerChildRenderFunction,\n} from '../Virtualizer/Virtualizer.types';\n\nimport type { VirtualizerScrollViewSlots } from '../VirtualizerScrollView/VirtualizerScrollView.types';\nimport type { RefObject } from 'react';\nimport type { ScrollToInterface } from '../../Utilities';\n\nexport type VirtualizerScrollViewDynamicSlots = VirtualizerScrollViewSlots;\n\nexport type VirtualizerScrollViewDynamicProps = ComponentProps<Partial<VirtualizerScrollViewDynamicSlots>> &\n Partial<Omit<VirtualizerConfigProps, 'itemSize' | 'numItems' | 'getItemSize' | 'children' | 'flagIndex'>> & {\n /**\n * Set as the minimum item size.\n * Axis: 'vertical' = Height\n * Axis: 'horizontal' = Width\n */\n itemSize: number;\n /**\n * Callback for acquiring size of individual items\n * @param index - the index of the requested size's child\n * If undefined, Virtualizer will auto-measure by default (performance tradeoff)\n */\n getItemSize?: (index: number) => number;\n /**\n * The total number of items to be virtualized.\n */\n numItems: number;\n /**\n * Child render function.\n * Iteratively called to return current virtualizer DOM children.\n * Will act as a row or column indexer depending on Virtualizer settings.\n */\n children: VirtualizerChildRenderFunction;\n /**\n * Imperative ref contains our scrollTo index functionality for user control.\n */\n imperativeRef?: RefObject<ScrollToInterface>;\n };\n\nexport type VirtualizerScrollViewDynamicState = ComponentState<VirtualizerScrollViewDynamicSlots> &\n VirtualizerConfigState;\n"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"sources":["VirtualizerScrollViewDynamic.types.ts"],"sourcesContent":["import type { ComponentProps, ComponentState } from '@fluentui/react-utilities';\nimport type {\n VirtualizerConfigProps,\n VirtualizerConfigState,\n VirtualizerChildRenderFunction,\n} from '../Virtualizer/Virtualizer.types';\n\nimport type { VirtualizerScrollViewSlots } from '../VirtualizerScrollView/VirtualizerScrollView.types';\nimport type { RefObject } from 'react';\nimport type { ScrollToInterface } from '../../Utilities';\n\nexport type VirtualizerScrollViewDynamicSlots = VirtualizerScrollViewSlots;\n\nexport type VirtualizerScrollViewDynamicProps = ComponentProps<Partial<VirtualizerScrollViewDynamicSlots>> &\n Partial<Omit<VirtualizerConfigProps, 'itemSize' | 'numItems' | 'getItemSize' | 'children' | 'flagIndex'>> & {\n /**\n * Set as the minimum item size.\n * Axis: 'vertical' = Height\n * Axis: 'horizontal' = Width\n */\n itemSize: number;\n /**\n * Callback for acquiring size of individual items\n * @param index - the index of the requested size's child\n * If undefined, Virtualizer will auto-measure by default (performance tradeoff)\n */\n getItemSize?: (index: number) => number;\n /**\n * The total number of items to be virtualized.\n */\n numItems: number;\n /**\n * Child render function.\n * Iteratively called to return current virtualizer DOM children.\n * Will act as a row or column indexer depending on Virtualizer settings.\n */\n children: VirtualizerChildRenderFunction;\n /**\n * Imperative ref contains our scrollTo index functionality for user control.\n */\n imperativeRef?: RefObject<ScrollToInterface>;\n /**\n * Imperative ref contains our scrollTo index functionality for user control.\n */\n enablePagination?: boolean;\n };\n\nexport type VirtualizerScrollViewDynamicState = ComponentState<VirtualizerScrollViewDynamicSlots> &\n VirtualizerConfigState;\n"],"names":[],"mappings":"AAAA,WAgDyB"}
|
|
@@ -3,12 +3,16 @@ import { slot, useMergedRefs } from '@fluentui/react-utilities';
|
|
|
3
3
|
import { useVirtualizer_unstable } from '../Virtualizer/useVirtualizer';
|
|
4
4
|
import { useDynamicVirtualizerMeasure } from '../../Hooks';
|
|
5
5
|
import { useVirtualizerContextState_unstable, scrollToItemDynamic } from '../../Utilities';
|
|
6
|
-
import { useImperativeHandle } from 'react';
|
|
6
|
+
import { useImperativeHandle, useState } from 'react';
|
|
7
7
|
import { useMeasureList } from '../../hooks/useMeasureList';
|
|
8
|
+
import { useDynamicVirtualizerPagination } from '../../hooks/useDynamicPagination';
|
|
8
9
|
export function useVirtualizerScrollViewDynamic_unstable(props) {
|
|
10
|
+
var _imperativeVirtualizerRef_current;
|
|
9
11
|
const contextState = useVirtualizerContextState_unstable(props.virtualizerContext);
|
|
10
|
-
const { imperativeRef, axis = 'vertical', reversed, imperativeVirtualizerRef } = props;
|
|
12
|
+
const { imperativeRef, axis = 'vertical', reversed, imperativeVirtualizerRef, enablePagination = false } = props;
|
|
11
13
|
let sizeTrackingArray = React.useRef(new Array(props.numItems).fill(props.itemSize));
|
|
14
|
+
// This lets us trigger updates when a size change occurs.
|
|
15
|
+
const [sizeUpdateCount, setSizeUpdateCount] = useState(0);
|
|
12
16
|
const getChildSizeAuto = React.useCallback((index)=>{
|
|
13
17
|
if (sizeTrackingArray.current.length <= index || sizeTrackingArray.current[index] <= 0) {
|
|
14
18
|
// Default size for initial state or untracked
|
|
@@ -17,9 +21,11 @@ export function useVirtualizerScrollViewDynamic_unstable(props) {
|
|
|
17
21
|
/* Required to be defined prior to our measure function
|
|
18
22
|
* we use a sizing array ref that we will update post-render
|
|
19
23
|
*/ return sizeTrackingArray.current[index];
|
|
20
|
-
},
|
|
24
|
+
}, // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
25
|
+
[
|
|
21
26
|
sizeTrackingArray,
|
|
22
|
-
props.itemSize
|
|
27
|
+
props.itemSize,
|
|
28
|
+
sizeUpdateCount
|
|
23
29
|
]);
|
|
24
30
|
var _props_axis, _props_getItemSize, _contextState_contextIndex;
|
|
25
31
|
const { virtualizerLength, bufferItems, bufferSize, scrollRef } = useDynamicVirtualizerMeasure({
|
|
@@ -29,14 +35,21 @@ export function useVirtualizerScrollViewDynamic_unstable(props) {
|
|
|
29
35
|
currentIndex: (_contextState_contextIndex = contextState === null || contextState === void 0 ? void 0 : contextState.contextIndex) !== null && _contextState_contextIndex !== void 0 ? _contextState_contextIndex : 0,
|
|
30
36
|
numItems: props.numItems
|
|
31
37
|
});
|
|
38
|
+
const _imperativeVirtualizerRef = useMergedRefs(React.useRef(null), imperativeVirtualizerRef);
|
|
39
|
+
var _contextState_contextIndex1;
|
|
40
|
+
const paginationRef = useDynamicVirtualizerPagination({
|
|
41
|
+
axis,
|
|
42
|
+
progressiveItemSizes: (_imperativeVirtualizerRef_current = _imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.progressiveSizes,
|
|
43
|
+
virtualizerLength,
|
|
44
|
+
currentIndex: (_contextState_contextIndex1 = contextState === null || contextState === void 0 ? void 0 : contextState.contextIndex) !== null && _contextState_contextIndex1 !== void 0 ? _contextState_contextIndex1 : 0
|
|
45
|
+
}, enablePagination);
|
|
32
46
|
// Store the virtualizer length as a ref for imperative ref access
|
|
33
47
|
const virtualizerLengthRef = React.useRef(virtualizerLength);
|
|
34
48
|
if (virtualizerLengthRef.current !== virtualizerLength) {
|
|
35
49
|
virtualizerLengthRef.current = virtualizerLength;
|
|
36
50
|
}
|
|
37
|
-
const scrollViewRef = useMergedRefs(props.scrollViewRef, scrollRef);
|
|
51
|
+
const scrollViewRef = useMergedRefs(props.scrollViewRef, scrollRef, paginationRef);
|
|
38
52
|
const scrollCallbackRef = React.useRef(null);
|
|
39
|
-
const _imperativeVirtualizerRef = useMergedRefs(React.useRef(null), imperativeVirtualizerRef);
|
|
40
53
|
useImperativeHandle(imperativeRef, ()=>{
|
|
41
54
|
var _imperativeVirtualizerRef_current;
|
|
42
55
|
return {
|
|
@@ -85,6 +98,10 @@ export function useVirtualizerScrollViewDynamic_unstable(props) {
|
|
|
85
98
|
onRenderedFlaggedIndex: handleRenderedIndex
|
|
86
99
|
});
|
|
87
100
|
const measureObject = useMeasureList(virtualizerState.virtualizerStartIndex, virtualizerLength, props.numItems, props.itemSize);
|
|
101
|
+
if (enablePagination && measureObject.sizeUpdateCount !== sizeUpdateCount) {
|
|
102
|
+
/* This enables us to let callback know that the sizes have been updated
|
|
103
|
+
triggers a re-render but is only required on pagination (else index change handles) */ setSizeUpdateCount(measureObject.sizeUpdateCount);
|
|
104
|
+
}
|
|
88
105
|
if (axis === 'horizontal') {
|
|
89
106
|
sizeTrackingArray = measureObject.widthArray;
|
|
90
107
|
} else {
|
|
@@ -98,12 +115,6 @@ export function useVirtualizerScrollViewDynamic_unstable(props) {
|
|
|
98
115
|
...child.props,
|
|
99
116
|
key: child.key,
|
|
100
117
|
ref: (element)=>{
|
|
101
|
-
// If a ref exists in props, call it
|
|
102
|
-
if (typeof child.props.ref === 'function') {
|
|
103
|
-
child.props.ref(element);
|
|
104
|
-
} else if (child.props.ref) {
|
|
105
|
-
child.props.ref.current = element;
|
|
106
|
-
}
|
|
107
118
|
if (child.hasOwnProperty('ref')) {
|
|
108
119
|
// We must access this from the child directly, not props (forward ref).
|
|
109
120
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
@@ -1 +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 } from 'react';\nimport { useMeasureList } from '../../hooks/useMeasureList';\nimport type { IndexedResizeCallbackElement } from '../../hooks/useMeasureList';\n\nexport function useVirtualizerScrollViewDynamic_unstable(\n props: VirtualizerScrollViewDynamicProps,\n): VirtualizerScrollViewDynamicState {\n const contextState = useVirtualizerContextState_unstable(props.virtualizerContext);\n const { imperativeRef, axis = 'vertical', reversed, imperativeVirtualizerRef } = props;\n\n let sizeTrackingArray = React.useRef<number[]>(new Array(props.numItems).fill(props.itemSize));\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 [sizeTrackingArray, props.itemSize],\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 // 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) as React.RefObject<HTMLDivElement>;\n const scrollCallbackRef = React.useRef<null | ((index: number) => void)>(null);\n\n const _imperativeVirtualizerRef = useMergedRefs(React.useRef<VirtualizerDataRef>(null), imperativeVirtualizerRef);\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 (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 a ref exists in props, call it\n if (typeof child.props.ref === 'function') {\n child.props.ref(element);\n } else if (child.props.ref) {\n child.props.ref.current = element;\n }\n\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","useMeasureList","useVirtualizerScrollViewDynamic_unstable","props","contextState","virtualizerContext","imperativeRef","axis","reversed","imperativeVirtualizerRef","sizeTrackingArray","useRef","Array","numItems","fill","itemSize","getChildSizeAuto","useCallback","index","current","length","virtualizerLength","bufferItems","bufferSize","scrollRef","defaultItemSize","direction","getItemSize","currentIndex","contextIndex","virtualizerLengthRef","scrollViewRef","scrollCallbackRef","_imperativeVirtualizerRef","scrollTo","behavior","callback","progressiveSizes","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"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAC/B,SAASC,IAAI,EAAEC,aAAa,QAAQ,4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OyC;oBACxC;;YAGN;QACF;IACF;IAEA,OAAO;QACL,GAAGd,gBAAgB;QACnBkB,YAAY;YACV,GAAGlB,iBAAiBkB,UAAU;YAC9BC,WAAW;QACb;QACAA,WAAWtE,KAAKuE,MAAM,CAAC9D,MAAM6D,SAAS,EAAE;YACtCE,cAAc;gBACZR,KAAK3B;YACP;YACAoC,aAAa;QACf;IACF;AACF"}
|
|
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 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"],"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;QAsCdC;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,0DAA0D;IAC1D,MAAM,CAACC,iB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"],"sourcesContent":["import { MutableRefObject } 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\n/**\n * Additional direct Ref prevents reading old resize entry data\n * Backwards compatible with ResizeObserverCallback if preferred\n */\nexport interface ResizeCallbackWithRef {\n (entries: ResizeObserverEntry[], observer: ResizeObserver, scrollRef?: MutableRefObject<HTMLElement | null>): void;\n}\n"],"names":[],"mappings":"AAAA,
|
|
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 (entries: ResizeObserverEntry[], observer: ResizeObserver, scrollRef?: MutableRefObject<HTMLElement | null>): void;\n}\n"],"names":[],"mappings":"AAAA,WAiDC"}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
import * as React from 'react';
|
|
2
|
+
import { useRef } from 'react';
|
|
3
|
+
/**
|
|
4
|
+
* Optional hook that will enable pagination on the virtualizer so that it 'autoscrolls' to an items exact position
|
|
5
|
+
* Sizes are dynamic so we require a progressive sizing array (passed in from Dynamic virtualizer hooks)
|
|
6
|
+
* On short scrolls, we will go at minimum to the next/previous item so that arrow pagination works
|
|
7
|
+
* All VirtualizerDynamicPaginationProps can be grabbed from dynamic Virtualizer hooks externally and passed in
|
|
8
|
+
*/ export const useDynamicVirtualizerPagination = (virtualizerProps, paginationEnabled = true)=>{
|
|
9
|
+
const { axis = 'vertical', currentIndex, progressiveItemSizes, virtualizerLength } = virtualizerProps;
|
|
10
|
+
const timeoutRef = useRef(null);
|
|
11
|
+
const lastScrollPos = useRef(-1);
|
|
12
|
+
const lastIndexScrolled = useRef(-1);
|
|
13
|
+
const scrollContainer = React.useRef(null);
|
|
14
|
+
const clearListeners = ()=>{
|
|
15
|
+
if (scrollContainer.current) {
|
|
16
|
+
scrollContainer.current.removeEventListener('scroll', onScroll);
|
|
17
|
+
scrollContainer.current = null;
|
|
18
|
+
if (timeoutRef.current) {
|
|
19
|
+
clearTimeout(timeoutRef.current);
|
|
20
|
+
}
|
|
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
|
+
if (timeoutRef.current) {
|
|
98
|
+
clearTimeout(timeoutRef.current);
|
|
99
|
+
}
|
|
100
|
+
timeoutRef.current = setTimeout(onScrollEnd, 100);
|
|
101
|
+
}, [
|
|
102
|
+
onScrollEnd
|
|
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';\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 const { axis = 'vertical', currentIndex, progressiveItemSizes, virtualizerLength } = virtualizerProps;\n\n const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);\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 if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\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 if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n timeoutRef.current = setTimeout(onScrollEnd, 100);\n },\n [onScrollEnd],\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","useDynamicVirtualizerPagination","virtualizerProps","paginationEnabled","axis","currentIndex","progressiveItemSizes","virtualizerLength","timeoutRef","lastScrollPos","lastIndexScrolled","scrollContainer","clearListeners","current","removeEventListener","onScroll","clearTimeout","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","setTimeout","paginationRef","instance","addEventListener"],"mappings":"AAAA,YAAYA,WAAW,QAAQ;AAE/B,SAASC,MAAM,QAAQ,QAAQ;AAE/B;;;;;CAKC,GACD,OAAO,MAAMC,kCAAkC,CAC7CC,kBACAC,oBAA6B,IAAI;IAEjC,MAAM,EAAEC,OAAO,UAAU,EAAEC,YAAY,EAAEC,oBAAoB,EAAEC,iBAAiB,EAAE,GAAGL;IAErF,MAAMM,aAAaR,OAA6C;IAChE,MAAMS,gBAAgBT,OAAe,CAAC;IACtC,MAAMU,oBAAoBV,OAAe,CAAC;IAE1C,MAAMW,kBAAkBZ,MAAMC,MAAM,CAAqB;IAEzD,MAAMY,iBAAiB;QACrB,IAAID,gBAAgBE,OAAO,EAAE;YAC3BF,gBAAgBE,OAAO,CAACC,mBAAmB,CAAC,UAAUC;YACtDJ,gBAA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,UAAUhC;YACrD;QACF;IACF,GACA,uDAAuD;IACvD;QAACA;QAAUG;QAAaf;KAAkB;IAG5C,OAAO0C;AACT,EAAE"}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import * as React from 'react';
|
|
2
|
+
import { useRef } from 'react';
|
|
2
3
|
import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';
|
|
3
4
|
/**
|
|
4
5
|
* Provides a way of automating size in the virtualizer
|
|
@@ -11,17 +12,30 @@ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts
|
|
|
11
12
|
const heightArray = React.useRef(new Array(totalLength).fill(defaultItemSize));
|
|
12
13
|
const refArray = React.useRef([]);
|
|
13
14
|
const { targetDocument } = useFluent();
|
|
15
|
+
// This lets us trigger updates when a size change occurs.
|
|
16
|
+
const sizeUpdateCount = useRef(0);
|
|
14
17
|
// the handler for resize observer
|
|
15
18
|
const handleIndexUpdate = React.useCallback((index)=>{
|
|
16
19
|
var _refArray_current_index;
|
|
20
|
+
let isChanged = false;
|
|
17
21
|
const boundClientRect = (_refArray_current_index = refArray.current[index]) === null || _refArray_current_index === void 0 ? void 0 : _refArray_current_index.getBoundingClientRect();
|
|
18
22
|
const containerWidth = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.width;
|
|
23
|
+
if (containerWidth !== widthArray.current[currentIndex + index]) {
|
|
24
|
+
isChanged = true;
|
|
25
|
+
}
|
|
19
26
|
widthArray.current[currentIndex + index] = containerWidth || defaultItemSize;
|
|
20
27
|
const containerHeight = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.height;
|
|
28
|
+
if (containerHeight !== heightArray.current[currentIndex + index]) {
|
|
29
|
+
isChanged = true;
|
|
30
|
+
}
|
|
21
31
|
heightArray.current[currentIndex + index] = containerHeight || defaultItemSize;
|
|
32
|
+
if (isChanged) {
|
|
33
|
+
sizeUpdateCount.current = sizeUpdateCount.current + 1;
|
|
34
|
+
}
|
|
22
35
|
}, [
|
|
23
36
|
currentIndex,
|
|
24
|
-
defaultItemSize
|
|
37
|
+
defaultItemSize,
|
|
38
|
+
sizeUpdateCount
|
|
25
39
|
]);
|
|
26
40
|
const handleElementResizeCallback = (entries)=>{
|
|
27
41
|
for (const entry of entries){
|
|
@@ -83,7 +97,8 @@ import { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts
|
|
|
83
97
|
widthArray,
|
|
84
98
|
heightArray,
|
|
85
99
|
createIndexedRef,
|
|
86
|
-
refArray
|
|
100
|
+
refArray,
|
|
101
|
+
sizeUpdateCount: sizeUpdateCount.current
|
|
87
102
|
};
|
|
88
103
|
}
|
|
89
104
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useMeasureList.ts"],"sourcesContent":["import * as React from 'react';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\n\nexport interface IndexedResizeCallbackElement {\n handleResize: () => void;\n}\n/**\n * Provides a way of automating size in the virtualizer\n * Returns\n * `width` - element width ref (0 by default),\n * `height` - element height ref (0 by default),\n * `measureElementRef` - a ref function to be passed as `ref` to the element you want to measure\n */\nexport function useMeasureList<\n TElement extends HTMLElement & IndexedResizeCallbackElement = HTMLElement & IndexedResizeCallbackElement,\n>(currentIndex: number, refLength: number, totalLength: number, defaultItemSize: number) {\n const widthArray = React.useRef(new Array(totalLength).fill(defaultItemSize));\n const heightArray = React.useRef(new Array(totalLength).fill(defaultItemSize));\n\n const refArray = React.useRef<Array<TElement | undefined | null>>([]);\n const { targetDocument } = useFluent();\n\n // the handler for resize observer\n const handleIndexUpdate = React.useCallback(\n (index: number) => {\n const boundClientRect = refArray.current[index]?.getBoundingClientRect();\n const containerWidth = boundClientRect?.width;\n widthArray.current[currentIndex + index] = containerWidth || defaultItemSize;\n\n const containerHeight = boundClientRect?.height;\n heightArray.current[currentIndex + index] = containerHeight || defaultItemSize;\n },\n [currentIndex, defaultItemSize],\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 };\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","useFluent_unstable","useFluent","useMeasureList","currentIndex","refLength","totalLength","defaultItemSize","widthArray","
|
|
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"],"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,EAAEa,cAAc,EAAE,GAAGX;IAE3B,0DAA0D;IAC1D,MAAMY,kBAAkBd,OAAO;IAE/B,kCAAkC;IAClC,MAAMe,oBAAoBhB,MAAMiB,WAAW,CACzC,CAACC;YAEyBL;QADxB,IAAIM,YAAY;QAChB,MAAMC,mBAAkBP,0BAAAA,SAASQ,OAAO,CAACH,MAAM,cAAvBL,8CAAAA,wBAAyBS,qBAAqB;QACtE,MAAMC,iBAAiBH,4BAAAA,sCAAAA,gBAAiBI,KAAK;QAC7C,IAAID,mBAAmBd,WAAWY,OAAO,CAAChB,eAAea,MAAM,EAAE;YAC/DC,YAAY;QACd;QACAV,WAAWY,OAAO,CAAChB,eAAea,MAAM,GAAGK,kBAAkBf;QAE7D,MAAMiB,kBAAkBL,4BAAAA,sCAAAA,gBAAiBM,MAAM;QAE/C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