@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
|
@@ -0,0 +1,103 @@
|
|
|
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 uniform/static, we round to the nearest item on long scrolls
|
|
6
|
+
* On short scrolls, we will go at minimum to the next/previous item so that arrow pagination works
|
|
7
|
+
* All VirtualizerStaticPaginationProps can be grabbed from Virtualizer hooks externally and passed in
|
|
8
|
+
*/ export const useStaticVirtualizerPagination = (virtualizerProps, paginationEnabled = true)=>{
|
|
9
|
+
const { itemSize, axis = 'vertical' } = virtualizerProps;
|
|
10
|
+
const timeoutRef = useRef(null);
|
|
11
|
+
const lastScrollPos = useRef(0);
|
|
12
|
+
const lastIndexScrolled = useRef(0);
|
|
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
|
+
*/ const onScrollEnd = React.useCallback(()=>{
|
|
34
|
+
if (!scrollContainer.current || !paginationEnabled) {
|
|
35
|
+
// No container found
|
|
36
|
+
return;
|
|
37
|
+
}
|
|
38
|
+
const currentScrollPos = Math.round(axis === 'vertical' ? scrollContainer.current.scrollTop : scrollContainer.current.scrollLeft);
|
|
39
|
+
const closestItem = Math.round(currentScrollPos / itemSize);
|
|
40
|
+
let nextItem = 0;
|
|
41
|
+
if (Math.round(closestItem - lastIndexScrolled.current) === 0) {
|
|
42
|
+
// Special case for go to next/previous with minimum amount of scroll needed
|
|
43
|
+
const nextTarget = lastScrollPos.current < currentScrollPos ? 1 : -1;
|
|
44
|
+
const isSecondaryScroll = lastScrollPos.current === currentScrollPos;
|
|
45
|
+
const posMod = isSecondaryScroll ? 0 : nextTarget;
|
|
46
|
+
nextItem = closestItem + posMod;
|
|
47
|
+
} else {
|
|
48
|
+
// Pagination for anything else can just jump to the closest!
|
|
49
|
+
nextItem = closestItem;
|
|
50
|
+
}
|
|
51
|
+
const nextItemPos = nextItem * itemSize;
|
|
52
|
+
if (axis === 'vertical') {
|
|
53
|
+
scrollContainer.current.scrollTo({
|
|
54
|
+
top: nextItemPos,
|
|
55
|
+
behavior: 'smooth'
|
|
56
|
+
});
|
|
57
|
+
} else {
|
|
58
|
+
scrollContainer.current.scrollTo({
|
|
59
|
+
left: nextItemPos,
|
|
60
|
+
behavior: 'smooth'
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
lastScrollPos.current = nextItemPos;
|
|
64
|
+
lastIndexScrolled.current = nextItem;
|
|
65
|
+
}, [
|
|
66
|
+
paginationEnabled,
|
|
67
|
+
axis,
|
|
68
|
+
itemSize
|
|
69
|
+
]);
|
|
70
|
+
/**
|
|
71
|
+
* On scroll timer that will continuously delay callback until scrolling stops
|
|
72
|
+
*/ const onScroll = React.useCallback((event)=>{
|
|
73
|
+
if (timeoutRef.current) {
|
|
74
|
+
clearTimeout(timeoutRef.current);
|
|
75
|
+
}
|
|
76
|
+
timeoutRef.current = setTimeout(onScrollEnd, 100);
|
|
77
|
+
}, [
|
|
78
|
+
onScrollEnd
|
|
79
|
+
]);
|
|
80
|
+
/**
|
|
81
|
+
* Pagination ref will ensure we attach listeners to containers on change
|
|
82
|
+
* It is returned from hook and merged into the scroll container externally
|
|
83
|
+
*/ const paginationRef = React.useCallback((instance)=>{
|
|
84
|
+
if (!paginationEnabled) {
|
|
85
|
+
clearListeners();
|
|
86
|
+
scrollContainer.current = null;
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
if (scrollContainer.current !== instance) {
|
|
90
|
+
clearListeners();
|
|
91
|
+
scrollContainer.current = instance;
|
|
92
|
+
if (scrollContainer.current) {
|
|
93
|
+
scrollContainer.current.addEventListener('scroll', onScroll);
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
}, // eslint-disable-next-line react-hooks/exhaustive-deps
|
|
97
|
+
[
|
|
98
|
+
onScroll,
|
|
99
|
+
onScrollEnd,
|
|
100
|
+
paginationEnabled
|
|
101
|
+
]);
|
|
102
|
+
return paginationRef;
|
|
103
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useStaticPagination.ts"],"sourcesContent":["import * as React from 'react';\nimport { VirtualizerStaticPaginationProps } from './hooks.types';\nimport { useRef } from 'react';\n\n/**\n * Optional hook that will enable pagination on the virtualizer so that it 'autoscrolls' to an items exact position\n * Sizes are uniform/static, we round to the nearest item on long scrolls\n * On short scrolls, we will go at minimum to the next/previous item so that arrow pagination works\n * All VirtualizerStaticPaginationProps can be grabbed from Virtualizer hooks externally and passed in\n */\nexport const useStaticVirtualizerPagination = (\n virtualizerProps: VirtualizerStaticPaginationProps,\n paginationEnabled: Boolean = true,\n) => {\n const { itemSize, axis = 'vertical' } = virtualizerProps;\n\n const timeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);\n const lastScrollPos = useRef<number>(0);\n const lastIndexScrolled = useRef<number>(0);\n\n const scrollContainer = React.useRef<HTMLElement | null>(null);\n\n const clearListeners = () => {\n if (scrollContainer.current) {\n scrollContainer.current.removeEventListener('scroll', onScroll);\n\n scrollContainer.current = null;\n 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 const onScrollEnd = React.useCallback(() => {\n if (!scrollContainer.current || !paginationEnabled) {\n // No container found\n return;\n }\n\n const currentScrollPos = Math.round(\n axis === 'vertical' ? scrollContainer.current.scrollTop : scrollContainer.current.scrollLeft,\n );\n const closestItem = Math.round(currentScrollPos / itemSize);\n\n let nextItem = 0;\n if (Math.round(closestItem - lastIndexScrolled.current) === 0) {\n // Special case for go to next/previous with minimum amount of scroll needed\n const nextTarget = lastScrollPos.current < currentScrollPos ? 1 : -1;\n const isSecondaryScroll = lastScrollPos.current === currentScrollPos;\n const posMod = isSecondaryScroll ? 0 : nextTarget;\n\n nextItem = closestItem + posMod;\n } else {\n // Pagination for anything else can just jump to the closest!\n nextItem = closestItem;\n }\n\n const nextItemPos = nextItem * itemSize;\n\n if (axis === 'vertical') {\n scrollContainer.current.scrollTo({ top: nextItemPos, behavior: 'smooth' });\n } else {\n scrollContainer.current.scrollTo({ left: nextItemPos, behavior: 'smooth' });\n }\n lastScrollPos.current = nextItemPos;\n lastIndexScrolled.current = nextItem;\n }, [paginationEnabled, axis, itemSize]);\n\n /**\n * On scroll timer that will continuously delay callback until scrolling stops\n */\n const onScroll = React.useCallback(\n event => {\n 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","useStaticVirtualizerPagination","virtualizerProps","paginationEnabled","itemSize","axis","timeoutRef","lastScrollPos","lastIndexScrolled","scrollContainer","clearListeners","current","removeEventListener","onScroll","clearTimeout","useEffect","onScrollEnd","useCallback","currentScrollPos","Math","round","scrollTop","scrollLeft","closestItem","nextItem","nextTarget","isSecondaryScroll","posMod","nextItemPos","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,iCAAiC,CAC5CC,kBACAC,oBAA6B,IAAI;IAEjC,MAAM,EAAEC,QAAQ,EAAEC,OAAO,UAAU,EAAE,GAAGH;IAExC,MAAMI,aAAaN,OAA6C;IAChE,MAAMO,gBAAgBP,OAAe;IACrC,MAAMQ,oBAAoBR,OAAe;IAEzC,MAAMS,kBAAkBV,MAAMC,MAAM,CAAqB;IAEzD,MAAMU,iBAAiB;QACrB,IAAID,gBAAgBE,OAAO,EAAE;YAC3BF,gBAAgBE,OAAO,CAACC,mBAAmB,CAAC,UAAUC;YAEtDJ,gBAAgBE,OAAO,GAAG;YAC1B,IAAIL,WAAWK,OAAO,EAAE;gBACtBG,aAAaR,WAAWK,OAAO;YACjC;QACF;IACF;IAEAZ,MAAMgB,SAAS,CAAC;QACd,OAAO;YACLL;QACF;IACA,uDAAuD;IACzD,GAAG,EAAE;IAEL;;;;GAIC,GACD,MAAMM,cAAcjB,MAAMkB,WAAW,CAAC;QACpC,IAAI,CAACR,gBAAgBE,OAAO,IAAI,CAACR,mBAAmB;YAClD,qBAAqB;YACrB;QACF;QAEA,MAAMe,mBAAmBC,KAAKC,KAAK,CACjCf,SAAS,aAAaI,gBAAgBE,OAAO,CAACU,SAAS,GAAGZ,gBAAgBE,OAAO,CAACW,UAAU;QAE9F,MAAMC,cAAcJ,KAAKC,KAAK,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ab;KAAkB;IAG5C,OAAOgC;AACT,EAAE"}
|
|
@@ -14,8 +14,9 @@ const _reactutilities = require("@fluentui/react-utilities");
|
|
|
14
14
|
const _useVirtualizer = require("../Virtualizer/useVirtualizer");
|
|
15
15
|
const _Hooks = require("../../Hooks");
|
|
16
16
|
const _Utilities = require("../../Utilities");
|
|
17
|
+
const _useStaticPagination = require("../../hooks/useStaticPagination");
|
|
17
18
|
function useVirtualizerScrollView_unstable(props) {
|
|
18
|
-
const { imperativeRef, itemSize, numItems, axis = 'vertical', reversed } = props;
|
|
19
|
+
const { imperativeRef, itemSize, numItems, axis = 'vertical', reversed, enablePagination = false } = props;
|
|
19
20
|
var _props_axis;
|
|
20
21
|
const { virtualizerLength, bufferItems, bufferSize, scrollRef } = (0, _Hooks.useStaticVirtualizerMeasure)({
|
|
21
22
|
defaultItemSize: props.itemSize,
|
|
@@ -26,7 +27,11 @@ function useVirtualizerScrollView_unstable(props) {
|
|
|
26
27
|
if (virtualizerLengthRef.current !== virtualizerLength) {
|
|
27
28
|
virtualizerLengthRef.current = virtualizerLength;
|
|
28
29
|
}
|
|
29
|
-
const
|
|
30
|
+
const paginationRef = (0, _useStaticPagination.useStaticVirtualizerPagination)({
|
|
31
|
+
axis,
|
|
32
|
+
itemSize
|
|
33
|
+
}, enablePagination);
|
|
34
|
+
const scrollViewRef = (0, _reactutilities.useMergedRefs)(props.scrollViewRef, scrollRef, paginationRef);
|
|
30
35
|
const imperativeVirtualizerRef = _react.useRef(null);
|
|
31
36
|
const scrollCallbackRef = _react.useRef(null);
|
|
32
37
|
(0, _react.useImperativeHandle)(imperativeRef, ()=>{
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useVirtualizerScrollView.js"],"sourcesContent":["import * as React from 'react';\nimport { slot, useMergedRefs } from '@fluentui/react-utilities';\nimport { useVirtualizer_unstable } from '../Virtualizer/useVirtualizer';\nimport { useStaticVirtualizerMeasure } from '../../Hooks';\nimport { useImperativeHandle } from 'react';\nimport { scrollToItemStatic } from '../../Utilities';\nexport function useVirtualizerScrollView_unstable(props) {\n const { imperativeRef, itemSize, numItems, axis = 'vertical', reversed } = props;\n var _props_axis;\n const { virtualizerLength, bufferItems, bufferSize, scrollRef } = useStaticVirtualizerMeasure({\n defaultItemSize: props.itemSize,\n direction: (_props_axis = props.axis) !== null && _props_axis !== void 0 ? _props_axis : 'vertical'\n });\n // Store the virtualizer length as a ref for imperative ref access\n const virtualizerLengthRef = React.useRef(virtualizerLength);\n if (virtualizerLengthRef.current !== virtualizerLength) {\n virtualizerLengthRef.current = virtualizerLength;\n }\n const scrollViewRef = useMergedRefs(props.scrollViewRef, scrollRef);\n const imperativeVirtualizerRef = React.useRef(null);\n const scrollCallbackRef = React.useRef(null);\n useImperativeHandle(imperativeRef, ()=>{\n var _imperativeVirtualizerRef_current;\n return {\n scrollTo (index, behavior = 'auto', callback) {\n var _imperativeVirtualizerRef_current;\n scrollCallbackRef.current = callback !== null && callback !== void 0 ? callback : null;\n (_imperativeVirtualizerRef_current = imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _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 = imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.currentIndex,\n virtualizerLength: virtualizerLengthRef\n };\n }, [\n axis,\n scrollViewRef,\n itemSize,\n numItems,\n reversed\n ]);\n const handleRenderedIndex = (index)=>{\n if (scrollCallbackRef.current) {\n scrollCallbackRef.current(index);\n }\n };\n const virtualizerState = useVirtualizer_unstable({\n ...props,\n virtualizerLength,\n bufferItems,\n bufferSize,\n scrollViewRef,\n onRenderedFlaggedIndex: handleRenderedIndex,\n imperativeVirtualizerRef\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":["useVirtualizerScrollView_unstable","props","imperativeRef","itemSize","numItems","axis","reversed","_props_axis","virtualizerLength","bufferItems","bufferSize","scrollRef","useStaticVirtualizerMeasure","defaultItemSize","direction","virtualizerLengthRef","React","useRef","current","scrollViewRef","useMergedRefs","imperativeVirtualizerRef","scrollCallbackRef","useImperativeHandle","_imperativeVirtualizerRef_current","scrollTo","index","behavior","callback","setFlaggedIndex","scrollToItemStatic","totalItems","currentIndex","handleRenderedIndex","virtualizerState","useVirtualizer_unstable","onRenderedFlaggedIndex","components","container","slot","always","defaultProps","ref","elementType"],"mappings":";;;;+
|
|
1
|
+
{"version":3,"sources":["useVirtualizerScrollView.js"],"sourcesContent":["import * as React from 'react';\nimport { slot, useMergedRefs } from '@fluentui/react-utilities';\nimport { useVirtualizer_unstable } from '../Virtualizer/useVirtualizer';\nimport { useStaticVirtualizerMeasure } from '../../Hooks';\nimport { useImperativeHandle } from 'react';\nimport { scrollToItemStatic } from '../../Utilities';\nimport { useStaticVirtualizerPagination } from '../../hooks/useStaticPagination';\nexport function useVirtualizerScrollView_unstable(props) {\n const { imperativeRef, itemSize, numItems, axis = 'vertical', reversed, enablePagination = false } = props;\n var _props_axis;\n const { virtualizerLength, bufferItems, bufferSize, scrollRef } = useStaticVirtualizerMeasure({\n defaultItemSize: props.itemSize,\n direction: (_props_axis = props.axis) !== null && _props_axis !== void 0 ? _props_axis : 'vertical'\n });\n // Store the virtualizer length as a ref for imperative ref access\n const virtualizerLengthRef = React.useRef(virtualizerLength);\n if (virtualizerLengthRef.current !== virtualizerLength) {\n virtualizerLengthRef.current = virtualizerLength;\n }\n const paginationRef = useStaticVirtualizerPagination({\n axis,\n itemSize\n }, enablePagination);\n const scrollViewRef = useMergedRefs(props.scrollViewRef, scrollRef, paginationRef);\n const imperativeVirtualizerRef = React.useRef(null);\n const scrollCallbackRef = React.useRef(null);\n useImperativeHandle(imperativeRef, ()=>{\n var _imperativeVirtualizerRef_current;\n return {\n scrollTo (index, behavior = 'auto', callback) {\n var _imperativeVirtualizerRef_current;\n scrollCallbackRef.current = callback !== null && callback !== void 0 ? callback : null;\n (_imperativeVirtualizerRef_current = imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _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 = imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.currentIndex,\n virtualizerLength: virtualizerLengthRef\n };\n }, [\n axis,\n scrollViewRef,\n itemSize,\n numItems,\n reversed\n ]);\n const handleRenderedIndex = (index)=>{\n if (scrollCallbackRef.current) {\n scrollCallbackRef.current(index);\n }\n };\n const virtualizerState = useVirtualizer_unstable({\n ...props,\n virtualizerLength,\n bufferItems,\n bufferSize,\n scrollViewRef,\n onRenderedFlaggedIndex: handleRenderedIndex,\n imperativeVirtualizerRef\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":["useVirtualizerScrollView_unstable","props","imperativeRef","itemSize","numItems","axis","reversed","enablePagination","_props_axis","virtualizerLength","bufferItems","bufferSize","scrollRef","useStaticVirtualizerMeasure","defaultItemSize","direction","virtualizerLengthRef","React","useRef","current","paginationRef","useStaticVirtualizerPagination","scrollViewRef","useMergedRefs","imperativeVirtualizerRef","scrollCallbackRef","useImperativeHandle","_imperativeVirtualizerRef_current","scrollTo","index","behavior","callback","setFlaggedIndex","scrollToItemStatic","totalItems","currentIndex","handleRenderedIndex","virtualizerState","useVirtualizer_unstable","onRenderedFlaggedIndex","components","container","slot","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B,YAAY9B;oBACZkB;oBACAjB;oBACAC;oBACAwB;gBACJ;YACJ;YACAK,cAAc,AAACR,CAAAA,oCAAoCH,yBAAyBL,OAAO,AAAD,MAAO,QAAQQ,sCAAsC,KAAK,IAAI,KAAK,IAAIA,kCAAkCQ,YAAY;YACvM1B,mBAAmBO;QACvB;IACJ,GAAG;QACCX;QACAiB;QACAnB;QACAC;QACAE;KACH;IACD,MAAM8B,sBAAsB,CAACP;QACzB,IAAIJ,kBAAkBN,OAAO,EAAE;YAC3BM,kBAAkBN,OAAO,CAACU;QAC9B;IACJ;IACA,MAAMQ,mBAAmBC,IAAAA,uCAAuB,EAAC;QAC7C,GAAGrC,KAAK;QACRQ;QACAC;QACAC;QACAW;QACAiB,wBAAwBH;QACxBZ;IACJ;IACA,OAAO;QACH,GAAGa,gBAAgB;QACnBG,YAAY;YACR,GAAGH,iBAAiBG,UAAU;YAC9BC,WAAW;QACf;QACAA,WAAWC,oBAAI,CAACC,MAAM,CAAC1C,MAAMwC,SAAS,EAAE;YACpCG,cAAc;gBACVC,KAAKvB;YACT;YACAwB,aAAa;QACjB;IACJ;AACJ"}
|
package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js
CHANGED
|
@@ -15,10 +15,14 @@ const _useVirtualizer = require("../Virtualizer/useVirtualizer");
|
|
|
15
15
|
const _Hooks = require("../../Hooks");
|
|
16
16
|
const _Utilities = require("../../Utilities");
|
|
17
17
|
const _useMeasureList = require("../../hooks/useMeasureList");
|
|
18
|
+
const _useDynamicPagination = require("../../hooks/useDynamicPagination");
|
|
18
19
|
function useVirtualizerScrollViewDynamic_unstable(props) {
|
|
20
|
+
var _imperativeVirtualizerRef_current;
|
|
19
21
|
const contextState = (0, _Utilities.useVirtualizerContextState_unstable)(props.virtualizerContext);
|
|
20
|
-
const { imperativeRef, axis = 'vertical', reversed, imperativeVirtualizerRef } = props;
|
|
22
|
+
const { imperativeRef, axis = 'vertical', reversed, imperativeVirtualizerRef, enablePagination = false } = props;
|
|
21
23
|
let sizeTrackingArray = _react.useRef(new Array(props.numItems).fill(props.itemSize));
|
|
24
|
+
// This lets us trigger updates when a size change occurs.
|
|
25
|
+
const [sizeUpdateCount, setSizeUpdateCount] = (0, _react.useState)(0);
|
|
22
26
|
const getChildSizeAuto = _react.useCallback((index)=>{
|
|
23
27
|
if (sizeTrackingArray.current.length <= index || sizeTrackingArray.current[index] <= 0) {
|
|
24
28
|
// Default size for initial state or untracked
|
|
@@ -29,7 +33,8 @@ function useVirtualizerScrollViewDynamic_unstable(props) {
|
|
|
29
33
|
*/ return sizeTrackingArray.current[index];
|
|
30
34
|
}, [
|
|
31
35
|
sizeTrackingArray,
|
|
32
|
-
props.itemSize
|
|
36
|
+
props.itemSize,
|
|
37
|
+
sizeUpdateCount
|
|
33
38
|
]);
|
|
34
39
|
var _props_axis, _props_getItemSize, _contextState_contextIndex;
|
|
35
40
|
const { virtualizerLength, bufferItems, bufferSize, scrollRef } = (0, _Hooks.useDynamicVirtualizerMeasure)({
|
|
@@ -39,14 +44,21 @@ function useVirtualizerScrollViewDynamic_unstable(props) {
|
|
|
39
44
|
currentIndex: (_contextState_contextIndex = contextState === null || contextState === void 0 ? void 0 : contextState.contextIndex) !== null && _contextState_contextIndex !== void 0 ? _contextState_contextIndex : 0,
|
|
40
45
|
numItems: props.numItems
|
|
41
46
|
});
|
|
47
|
+
const _imperativeVirtualizerRef = (0, _reactutilities.useMergedRefs)(_react.useRef(null), imperativeVirtualizerRef);
|
|
48
|
+
var _contextState_contextIndex1;
|
|
49
|
+
const paginationRef = (0, _useDynamicPagination.useDynamicVirtualizerPagination)({
|
|
50
|
+
axis,
|
|
51
|
+
progressiveItemSizes: (_imperativeVirtualizerRef_current = _imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.progressiveSizes,
|
|
52
|
+
virtualizerLength,
|
|
53
|
+
currentIndex: (_contextState_contextIndex1 = contextState === null || contextState === void 0 ? void 0 : contextState.contextIndex) !== null && _contextState_contextIndex1 !== void 0 ? _contextState_contextIndex1 : 0
|
|
54
|
+
}, enablePagination);
|
|
42
55
|
// Store the virtualizer length as a ref for imperative ref access
|
|
43
56
|
const virtualizerLengthRef = _react.useRef(virtualizerLength);
|
|
44
57
|
if (virtualizerLengthRef.current !== virtualizerLength) {
|
|
45
58
|
virtualizerLengthRef.current = virtualizerLength;
|
|
46
59
|
}
|
|
47
|
-
const scrollViewRef = (0, _reactutilities.useMergedRefs)(props.scrollViewRef, scrollRef);
|
|
60
|
+
const scrollViewRef = (0, _reactutilities.useMergedRefs)(props.scrollViewRef, scrollRef, paginationRef);
|
|
48
61
|
const scrollCallbackRef = _react.useRef(null);
|
|
49
|
-
const _imperativeVirtualizerRef = (0, _reactutilities.useMergedRefs)(_react.useRef(null), imperativeVirtualizerRef);
|
|
50
62
|
(0, _react.useImperativeHandle)(imperativeRef, ()=>{
|
|
51
63
|
var _imperativeVirtualizerRef_current;
|
|
52
64
|
return {
|
|
@@ -95,6 +107,10 @@ function useVirtualizerScrollViewDynamic_unstable(props) {
|
|
|
95
107
|
onRenderedFlaggedIndex: handleRenderedIndex
|
|
96
108
|
});
|
|
97
109
|
const measureObject = (0, _useMeasureList.useMeasureList)(virtualizerState.virtualizerStartIndex, virtualizerLength, props.numItems, props.itemSize);
|
|
110
|
+
if (enablePagination && measureObject.sizeUpdateCount !== sizeUpdateCount) {
|
|
111
|
+
/* This enables us to let callback know that the sizes have been updated
|
|
112
|
+
triggers a re-render but is only required on pagination (else index change handles) */ setSizeUpdateCount(measureObject.sizeUpdateCount);
|
|
113
|
+
}
|
|
98
114
|
if (axis === 'horizontal') {
|
|
99
115
|
sizeTrackingArray = measureObject.widthArray;
|
|
100
116
|
} else {
|
|
@@ -108,12 +124,6 @@ function useVirtualizerScrollViewDynamic_unstable(props) {
|
|
|
108
124
|
...child.props,
|
|
109
125
|
key: child.key,
|
|
110
126
|
ref: (element)=>{
|
|
111
|
-
// If a ref exists in props, call it
|
|
112
|
-
if (typeof child.props.ref === 'function') {
|
|
113
|
-
child.props.ref(element);
|
|
114
|
-
} else if (child.props.ref) {
|
|
115
|
-
child.props.ref.current = element;
|
|
116
|
-
}
|
|
117
127
|
if (child.hasOwnProperty('ref')) {
|
|
118
128
|
// We must access this from the child directly, not props (forward ref).
|
|
119
129
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
package/lib-commonjs/components/VirtualizerScrollViewDynamic/useVirtualizerScrollViewDynamic.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useVirtualizerScrollViewDynamic.js"],"sourcesContent":["import * as React from 'react';\nimport { slot, useMergedRefs } from '@fluentui/react-utilities';\nimport { useVirtualizer_unstable } from '../Virtualizer/useVirtualizer';\nimport { useDynamicVirtualizerMeasure } from '../../Hooks';\nimport { useVirtualizerContextState_unstable, scrollToItemDynamic } from '../../Utilities';\nimport { useImperativeHandle } from 'react';\nimport { useMeasureList } from '../../hooks/useMeasureList';\nexport function useVirtualizerScrollViewDynamic_unstable(props) {\n const contextState = useVirtualizerContextState_unstable(props.virtualizerContext);\n const { imperativeRef, axis = 'vertical', reversed, imperativeVirtualizerRef } = props;\n let sizeTrackingArray = React.useRef(new Array(props.numItems).fill(props.itemSize));\n const getChildSizeAuto = React.useCallback((index)=>{\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 */ return sizeTrackingArray.current[index];\n }, [\n sizeTrackingArray,\n props.itemSize\n ]);\n var _props_axis, _props_getItemSize, _contextState_contextIndex;\n const { virtualizerLength, bufferItems, bufferSize, scrollRef } = useDynamicVirtualizerMeasure({\n defaultItemSize: props.itemSize,\n direction: (_props_axis = props.axis) !== null && _props_axis !== void 0 ? _props_axis : 'vertical',\n getItemSize: (_props_getItemSize = props.getItemSize) !== null && _props_getItemSize !== void 0 ? _props_getItemSize : getChildSizeAuto,\n currentIndex: (_contextState_contextIndex = contextState === null || contextState === void 0 ? void 0 : contextState.contextIndex) !== null && _contextState_contextIndex !== void 0 ? _contextState_contextIndex : 0,\n numItems: props.numItems\n });\n // Store the virtualizer length as a ref for imperative ref access\n const virtualizerLengthRef = React.useRef(virtualizerLength);\n if (virtualizerLengthRef.current !== virtualizerLength) {\n virtualizerLengthRef.current = virtualizerLength;\n }\n const scrollViewRef = useMergedRefs(props.scrollViewRef, scrollRef);\n const scrollCallbackRef = React.useRef(null);\n const _imperativeVirtualizerRef = useMergedRefs(React.useRef(null), imperativeVirtualizerRef);\n useImperativeHandle(imperativeRef, ()=>{\n var _imperativeVirtualizerRef_current;\n return {\n scrollTo (index, behavior = 'auto', callback) {\n scrollCallbackRef.current = callback !== null && callback !== void 0 ? callback : null;\n if (_imperativeVirtualizerRef.current) {\n var _imperativeVirtualizerRef_current;\n const progressiveSizes = _imperativeVirtualizerRef.current.progressiveSizes.current;\n const totalSize = progressiveSizes && (progressiveSizes === null || progressiveSizes === void 0 ? void 0 : progressiveSizes.length) > 0 ? progressiveSizes[Math.max(progressiveSizes.length - 1, 0)] : 0;\n _imperativeVirtualizerRef.current.setFlaggedIndex(index);\n scrollToItemDynamic({\n index,\n itemSizes: (_imperativeVirtualizerRef_current = _imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.nodeSizes,\n totalSize,\n scrollViewRef,\n axis,\n reversed,\n behavior\n });\n }\n },\n currentIndex: (_imperativeVirtualizerRef_current = _imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.currentIndex,\n virtualizerLength: virtualizerLengthRef\n };\n }, [\n axis,\n scrollViewRef,\n reversed,\n _imperativeVirtualizerRef\n ]);\n const handleRenderedIndex = (index)=>{\n if (scrollCallbackRef.current) {\n scrollCallbackRef.current(index);\n }\n };\n var _props_getItemSize1;\n const virtualizerState = useVirtualizer_unstable({\n ...props,\n getItemSize: (_props_getItemSize1 = props.getItemSize) !== null && _props_getItemSize1 !== void 0 ? _props_getItemSize1 : getChildSizeAuto,\n virtualizerLength,\n bufferItems,\n bufferSize,\n scrollViewRef,\n virtualizerContext: contextState,\n imperativeVirtualizerRef: _imperativeVirtualizerRef,\n onRenderedFlaggedIndex: handleRenderedIndex\n });\n const measureObject = useMeasureList(virtualizerState.virtualizerStartIndex, virtualizerLength, props.numItems, props.itemSize);\n if (axis === 'horizontal') {\n sizeTrackingArray = measureObject.widthArray;\n } else {\n sizeTrackingArray = measureObject.heightArray;\n }\n if (!props.getItemSize) {\n // Auto-measuring is required\n React.Children.map(virtualizerState.virtualizedChildren, (child, index)=>{\n if (/*#__PURE__*/ React.isValidElement(child)) {\n virtualizerState.virtualizedChildren[index] = /*#__PURE__*/ React.createElement(child.type, {\n ...child.props,\n key: child.key,\n ref: (element)=>{\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 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 === null || child === void 0 ? void 0 : child.ref;\n if (typeof localRef === 'function') {\n localRef(element);\n } else if (localRef) {\n localRef.current = element;\n }\n }\n // Call the auto-measure ref attachment.\n measureObject.createIndexedRef(index)(element);\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":["useVirtualizerScrollViewDynamic_unstable","props","contextState","useVirtualizerContextState_unstable","virtualizerContext","imperativeRef","axis","reversed","imperativeVirtualizerRef","sizeTrackingArray","React","useRef","Array","numItems","fill","itemSize","getChildSizeAuto","useCallback","index","current","length","_props_axis","_props_getItemSize","_contextState_contextIndex","virtualizerLength","bufferItems","bufferSize","scrollRef","useDynamicVirtualizerMeasure","defaultItemSize","direction","getItemSize","currentIndex","contextIndex","virtualizerLengthRef","scrollViewRef","useMergedRefs","scrollCallbackRef","_imperativeVirtualizerRef","useImperativeHandle","_imperativeVirtualizerRef_current","scrollTo","behavior","callback","progressiveSizes","totalSize","Math","max","setFlaggedIndex","scrollToItemDynamic","itemSizes","nodeSizes","handleRenderedIndex","_props_getItemSize1","virtualizerState","useVirtualizer_unstable","onRenderedFlaggedIndex","measureObject","useMeasureList","virtualizerStartIndex","widthArray","heightArray","Children","map","virtualizedChildren","child","isValidElement","createElement","type","key","ref","element","hasOwnProperty","localRef","createIndexedRef","components","container","slot","always","defaultProps","elementType"],"mappings":";;;;+BAOgBA;;;eAAAA;;;;iEAPO;gCACa;gCACI;uBACK;2BAC4B;gCAE1C;AACxB,SAASA,yCAAyCC,KAAK;IAC1D,MAAMC,eAAeC,IAAAA,8CAAmC,EAACF,MAAMG,kBAAkB;IACjF,MAAM,EAAEC,aAAa,EAAEC,OAAO,UAAU,EAAEC,QAAQ,EAAEC,wBAAwB,EAAE,GAAGP;IACjF,IAAIQ,oBAAoBC,OAAMC,MAAM,CAAC,IAAIC,MAAMX,MAAMY,QAAQ,EAAEC,IAAI,CAACb,MAAMc,QAAQ;IAClF,MAAMC,mBAAmBN,OAAMO,WAAW,CAAC,CAACC;QACxC,IAAIT,kBAAkBU,OAAO,CAACC,MAAM,IAAIF,SAAST,kBAAkBU,OAAO,CAACD,MAAM,IAAI,GAAG;YACpF,8CAA8C;YAC9C,OAAOjB,MAAMc,QAAQ;QACzB;QACA;;OAED,GAAG,OAAON,kBAAkBU,OAAO,CAACD,MAAM;IAC7C,GAAG;QACCT;QACAR,MAAMc,QAAQ;KACjB;IACD,IAAIM,aAAaC,oBAAoBC;IACrC,MAAM,EAAEC,iBAAiB,EAAEC,WAAW,EAAEC,UAAU,EAAEC,SAAS,EAAE,GAAGC,IAAAA,mCAA4B,EAAC;QAC3F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;YAC7D,IAAI,WAAW,GAAGR,OAAMwD,cAAc,CAACD,QAAQ;gBAC3CX,iBAAiBU,mBAAmB,CAAC9C,MAAM,GAAG,WAAW,GAAGR,OAAMyD,aAAa,CAACF,MAAMG,IAAI,EAAE;oBACxF,GAAGH,MAAMhE,KAAK;oBACdoE,KAAKJ,MAAMI,GAAG;oBACdC,KAAK,CAACC;wBACF,oCAAoC;wBACpC,IAAI,OAAON,MAAMhE,KAAK,CAACqE,GAAG,KAAK,YAAY;4BACvCL,MAAMhE,KAAK,CAACqE,GAAG,CAACC;wBACpB,OAAO,IAAIN,MAAMhE,KAAK,CAACqE,GAAG,EAAE;4BACxBL,MAAMhE,KAAK,CAACqE,GAAG,CAACnD,OAAO,GAAGoD;wBAC9B;wBACA,IAAIN,MAAMO,cAAc,CAAC,QAAQ;4BAC7B,wEAAwE;4BACxE,+DAA+D;4BAC/D,MAAMC,WAAWR,UAAU,QAAQA,UAAU,KAAK,IAAI,KAAK,IAAIA,MAAMK,GAAG;4BACxE,IAAI,OAAOG,aAAa,YAAY;gCAChCA,SAASF;4BACb,OAAO,IAAIE,UAAU;gCACjBA,SAAStD,OAAO,GAAGoD;4BACvB;wBACJ;wBACA,wCAAwC;wBACxCd,cAAciB,gBAAgB,CAACxD,OAAOqD;oBAC1C;gBACJ;YACJ;QACJ;IACJ;IACA,OAAO;QACH,GAAGjB,gBAAgB;QACnBqB,YAAY;YACR,GAAGrB,iBAAiBqB,UAAU;YAC9BC,WAAW;QACf;QACAA,WAAWC,oBAAI,CAACC,MAAM,CAAC7E,MAAM2E,SAAS,EAAE;YACpCG,cAAc;gBACVT,KAAKnC;YACT;YACA6C,aAAa;QACjB;IACJ;AACJ"}
|
|
1
|
+
{"version":3,"sources":["useVirtualizerScrollViewDynamic.js"],"sourcesContent":["import * as React from 'react';\nimport { slot, useMergedRefs } from '@fluentui/react-utilities';\nimport { useVirtualizer_unstable } from '../Virtualizer/useVirtualizer';\nimport { useDynamicVirtualizerMeasure } from '../../Hooks';\nimport { useVirtualizerContextState_unstable, scrollToItemDynamic } from '../../Utilities';\nimport { useImperativeHandle, useState } from 'react';\nimport { useMeasureList } from '../../hooks/useMeasureList';\nimport { useDynamicVirtualizerPagination } from '../../hooks/useDynamicPagination';\nexport function useVirtualizerScrollViewDynamic_unstable(props) {\n var _imperativeVirtualizerRef_current;\n const contextState = useVirtualizerContextState_unstable(props.virtualizerContext);\n const { imperativeRef, axis = 'vertical', reversed, imperativeVirtualizerRef, enablePagination = false } = props;\n let sizeTrackingArray = React.useRef(new Array(props.numItems).fill(props.itemSize));\n // This lets us trigger updates when a size change occurs.\n const [sizeUpdateCount, setSizeUpdateCount] = useState(0);\n const getChildSizeAuto = React.useCallback((index)=>{\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 */ return sizeTrackingArray.current[index];\n }, // eslint-disable-next-line react-hooks/exhaustive-deps\n [\n sizeTrackingArray,\n props.itemSize,\n sizeUpdateCount\n ]);\n var _props_axis, _props_getItemSize, _contextState_contextIndex;\n const { virtualizerLength, bufferItems, bufferSize, scrollRef } = useDynamicVirtualizerMeasure({\n defaultItemSize: props.itemSize,\n direction: (_props_axis = props.axis) !== null && _props_axis !== void 0 ? _props_axis : 'vertical',\n getItemSize: (_props_getItemSize = props.getItemSize) !== null && _props_getItemSize !== void 0 ? _props_getItemSize : getChildSizeAuto,\n currentIndex: (_contextState_contextIndex = contextState === null || contextState === void 0 ? void 0 : contextState.contextIndex) !== null && _contextState_contextIndex !== void 0 ? _contextState_contextIndex : 0,\n numItems: props.numItems\n });\n const _imperativeVirtualizerRef = useMergedRefs(React.useRef(null), imperativeVirtualizerRef);\n var _contextState_contextIndex1;\n const paginationRef = useDynamicVirtualizerPagination({\n axis,\n progressiveItemSizes: (_imperativeVirtualizerRef_current = _imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.progressiveSizes,\n virtualizerLength,\n currentIndex: (_contextState_contextIndex1 = contextState === null || contextState === void 0 ? void 0 : contextState.contextIndex) !== null && _contextState_contextIndex1 !== void 0 ? _contextState_contextIndex1 : 0\n }, enablePagination);\n // Store the virtualizer length as a ref for imperative ref access\n const virtualizerLengthRef = React.useRef(virtualizerLength);\n if (virtualizerLengthRef.current !== virtualizerLength) {\n virtualizerLengthRef.current = virtualizerLength;\n }\n const scrollViewRef = useMergedRefs(props.scrollViewRef, scrollRef, paginationRef);\n const scrollCallbackRef = React.useRef(null);\n useImperativeHandle(imperativeRef, ()=>{\n var _imperativeVirtualizerRef_current;\n return {\n scrollTo (index, behavior = 'auto', callback) {\n scrollCallbackRef.current = callback !== null && callback !== void 0 ? callback : null;\n if (_imperativeVirtualizerRef.current) {\n var _imperativeVirtualizerRef_current;\n const progressiveSizes = _imperativeVirtualizerRef.current.progressiveSizes.current;\n const totalSize = progressiveSizes && (progressiveSizes === null || progressiveSizes === void 0 ? void 0 : progressiveSizes.length) > 0 ? progressiveSizes[Math.max(progressiveSizes.length - 1, 0)] : 0;\n _imperativeVirtualizerRef.current.setFlaggedIndex(index);\n scrollToItemDynamic({\n index,\n itemSizes: (_imperativeVirtualizerRef_current = _imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.nodeSizes,\n totalSize,\n scrollViewRef,\n axis,\n reversed,\n behavior\n });\n }\n },\n currentIndex: (_imperativeVirtualizerRef_current = _imperativeVirtualizerRef.current) === null || _imperativeVirtualizerRef_current === void 0 ? void 0 : _imperativeVirtualizerRef_current.currentIndex,\n virtualizerLength: virtualizerLengthRef\n };\n }, [\n axis,\n scrollViewRef,\n reversed,\n _imperativeVirtualizerRef\n ]);\n const handleRenderedIndex = (index)=>{\n if (scrollCallbackRef.current) {\n scrollCallbackRef.current(index);\n }\n };\n var _props_getItemSize1;\n const virtualizerState = useVirtualizer_unstable({\n ...props,\n getItemSize: (_props_getItemSize1 = props.getItemSize) !== null && _props_getItemSize1 !== void 0 ? _props_getItemSize1 : getChildSizeAuto,\n virtualizerLength,\n bufferItems,\n bufferSize,\n scrollViewRef,\n virtualizerContext: contextState,\n imperativeVirtualizerRef: _imperativeVirtualizerRef,\n onRenderedFlaggedIndex: handleRenderedIndex\n });\n const measureObject = useMeasureList(virtualizerState.virtualizerStartIndex, virtualizerLength, props.numItems, props.itemSize);\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) */ setSizeUpdateCount(measureObject.sizeUpdateCount);\n }\n if (axis === 'horizontal') {\n sizeTrackingArray = measureObject.widthArray;\n } else {\n sizeTrackingArray = measureObject.heightArray;\n }\n if (!props.getItemSize) {\n // Auto-measuring is required\n React.Children.map(virtualizerState.virtualizedChildren, (child, index)=>{\n if (/*#__PURE__*/ React.isValidElement(child)) {\n virtualizerState.virtualizedChildren[index] = /*#__PURE__*/ React.createElement(child.type, {\n ...child.props,\n key: child.key,\n ref: (element)=>{\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 === null || child === void 0 ? void 0 : child.ref;\n if (typeof localRef === 'function') {\n localRef(element);\n } else if (localRef) {\n localRef.current = element;\n }\n }\n // Call the auto-measure ref attachment.\n measureObject.createIndexedRef(index)(element);\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":["useVirtualizerScrollViewDynamic_unstable","props","_imperativeVirtualizerRef_current","contextState","useVirtualizerContextState_unstable","virtualizerContext","imperativeRef","axis","reversed","imperativeVirtualizerRef","enablePagination","sizeTrackingArray","React","useRef","Array","numItems","fill","itemSize","sizeUpdateCount","setSizeUpdateCount","useState","getChildSizeAuto","useCallback","index","current","length","_props_axis","_props_getItemSize","_contextState_contextIndex","virtualizerLength","bufferItems","bufferSize","scrollRef","useDynamicVirtualizerMeasure","defaultItemSize","direction","getItemSize","currentIndex","contextIndex","_imperativeVirtualizerRef","useMergedRefs","_contextState_contextIndex1","paginationRef","useDynamicVirtualizerPagination","progressiveItemSizes","progressiveSizes","virtualizerLengthRef","scrollViewRef","scrollCallbackRef","useImperativeHandle","scrollTo","behavior","callback","totalSize","Math","max","setFlaggedIndex","scrollToItemDynamic","itemSizes","nodeSizes","handleRenderedIndex","_props_getItemSize1","virtualizerState","useVirtualizer_unstable","onRenderedFlaggedIndex","measureObject","useMeasureList","virtualizerStartIndex","widthArray","heightArray","Children","map","virtualizedChildren","child","isValidElement","createElement","type","key","ref","element","hasOwnProperty","localRef","createIndexedRef","components","container","slot","always","defaultProps","elementType"],"mappings":";;;;+BAQgBA;;;eAAAA;;;;iEARO;gCACa;gCACI;uBACK;2BAC4B;gCAE1C;sCACiB;AACzC,SAASA,yCAAyCC,KAAK;IAC1D,IAAIC;IACJ,MAAMC,eAAeC,IAAAA,8CAAmC,EAACH,MAAMI,kBAAkB;IACjF,MAAM,EAAEC,aAAa,EAAEC,OAAO,UAAU,EAAEC,QAAQ,EAAEC,wBAAwB,EAAEC,mBAAmB,KAAK,EAAE,GAAGT;IAC3G,IAAIU,oBAAoBC,OAAMC,MAAM,CAAC,IAAIC,MAAMb,MAAMc,QAAQ,EAAEC,IAAI,CAACf,MAAMgB,QAAQ;IAClF,0DAA0D;IAC1D,MAAM,CAACC,iBAAiBC,mBAAmB,GAAGC,IAAAA,eAAQ,EAAC;IACvD,MAAMC,mBAAmBT,OAAMU,WAAW,CAAC,CAACC;QACxC,IAAIZ,kBAAkBa,OAAO,CAACC,MAAM,IAAIF,SAASZ,kBAAkBa,OAAO,CAACD,MAAM,IAAI,GAAG;YACpF,8CAA8C;YAC9C,OAAOtB,MAAMgB,QAAQ;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GAAGuD;4BACvB;wBACJ;wBACA,wCAAwC;wBACxCd,cAAciB,gBAAgB,CAAC3D,OAAOwD;oBAC1C;gBACJ;YACJ;QACJ;IACJ;IACA,OAAO;QACH,GAAGjB,gBAAgB;QACnBqB,YAAY;YACR,GAAGrB,iBAAiBqB,UAAU;YAC9BC,WAAW;QACf;QACAA,WAAWC,oBAAI,CAACC,MAAM,CAACrF,MAAMmF,SAAS,EAAE;YACpCG,cAAc;gBACVT,KAAK/B;YACT;YACAyC,aAAa;QACjB;IACJ;AACJ"}
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", {
|
|
3
|
+
value: true
|
|
4
|
+
});
|
|
5
|
+
Object.defineProperty(exports, "useDynamicVirtualizerPagination", {
|
|
6
|
+
enumerable: true,
|
|
7
|
+
get: function() {
|
|
8
|
+
return useDynamicVirtualizerPagination;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
|
|
12
|
+
const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
|
|
13
|
+
const useDynamicVirtualizerPagination = (virtualizerProps, paginationEnabled = true)=>{
|
|
14
|
+
const { axis = 'vertical', currentIndex, progressiveItemSizes, virtualizerLength } = virtualizerProps;
|
|
15
|
+
const timeoutRef = (0, _react.useRef)(null);
|
|
16
|
+
const lastScrollPos = (0, _react.useRef)(-1);
|
|
17
|
+
const lastIndexScrolled = (0, _react.useRef)(-1);
|
|
18
|
+
const scrollContainer = _react.useRef(null);
|
|
19
|
+
const clearListeners = ()=>{
|
|
20
|
+
if (scrollContainer.current) {
|
|
21
|
+
scrollContainer.current.removeEventListener('scroll', onScroll);
|
|
22
|
+
scrollContainer.current = null;
|
|
23
|
+
if (timeoutRef.current) {
|
|
24
|
+
clearTimeout(timeoutRef.current);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
_react.useEffect(()=>{
|
|
29
|
+
return ()=>{
|
|
30
|
+
clearListeners();
|
|
31
|
+
};
|
|
32
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
33
|
+
}, []);
|
|
34
|
+
/**
|
|
35
|
+
* Handle scroll stop event and paginate to the closest item
|
|
36
|
+
* If the closest item is the same as the previous scroll end
|
|
37
|
+
* we paginate to the next/previous one based on direction
|
|
38
|
+
*
|
|
39
|
+
* Users/Virtualizer-Hooks must pass in a cumulative array of sizes
|
|
40
|
+
* This prevents the need to recalculate and ensures size arrays are synced externally
|
|
41
|
+
*/ const onScrollEnd = _react.useCallback(()=>{
|
|
42
|
+
if (!scrollContainer.current || !paginationEnabled || !(progressiveItemSizes === null || progressiveItemSizes === void 0 ? void 0 : progressiveItemSizes.current)) {
|
|
43
|
+
// No container found
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
const currentScrollPos = Math.round(axis === 'vertical' ? scrollContainer.current.scrollTop : scrollContainer.current.scrollLeft);
|
|
47
|
+
let closestItemPos = 0;
|
|
48
|
+
let closestItem = 0;
|
|
49
|
+
const endItem = Math.min(currentIndex + virtualizerLength, progressiveItemSizes.current.length);
|
|
50
|
+
for(let i = currentIndex; i < endItem - 1; i++){
|
|
51
|
+
if (currentScrollPos <= progressiveItemSizes.current[i + 1] && currentScrollPos >= progressiveItemSizes.current[i]) {
|
|
52
|
+
// Found our in between position
|
|
53
|
+
const distanceToPrev = Math.abs(currentScrollPos - progressiveItemSizes.current[i]);
|
|
54
|
+
const distanceToNext = Math.abs(progressiveItemSizes.current[i + 1] - currentScrollPos);
|
|
55
|
+
if (distanceToPrev < distanceToNext) {
|
|
56
|
+
closestItem = i;
|
|
57
|
+
} else {
|
|
58
|
+
closestItem = i + 1;
|
|
59
|
+
}
|
|
60
|
+
break;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
let nextItem;
|
|
64
|
+
if (Math.round(closestItem - lastIndexScrolled.current) === 0) {
|
|
65
|
+
// Special case for go to next/previous with minimum amount of scroll needed
|
|
66
|
+
const nextTarget = lastScrollPos.current < currentScrollPos ? 1 : -1;
|
|
67
|
+
// This will also handle a case where we scrolled to the exact correct position (noop)
|
|
68
|
+
const isSecondaryScroll = Math.round(lastScrollPos.current - currentScrollPos) === 0;
|
|
69
|
+
const posMod = isSecondaryScroll ? 0 : nextTarget;
|
|
70
|
+
nextItem = closestItem + posMod;
|
|
71
|
+
} else {
|
|
72
|
+
// Pagination for anything else can just jump to the closest!
|
|
73
|
+
nextItem = closestItem;
|
|
74
|
+
}
|
|
75
|
+
// Safeguard nextItem
|
|
76
|
+
nextItem = Math.min(Math.max(0, nextItem), progressiveItemSizes.current.length);
|
|
77
|
+
closestItemPos = progressiveItemSizes.current[nextItem];
|
|
78
|
+
if (axis === 'vertical') {
|
|
79
|
+
scrollContainer.current.scrollTo({
|
|
80
|
+
top: closestItemPos,
|
|
81
|
+
behavior: 'smooth'
|
|
82
|
+
});
|
|
83
|
+
} else {
|
|
84
|
+
scrollContainer.current.scrollTo({
|
|
85
|
+
left: closestItemPos,
|
|
86
|
+
behavior: 'smooth'
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
lastScrollPos.current = progressiveItemSizes.current[nextItem];
|
|
90
|
+
lastIndexScrolled.current = nextItem;
|
|
91
|
+
}, [
|
|
92
|
+
paginationEnabled,
|
|
93
|
+
currentIndex,
|
|
94
|
+
scrollContainer,
|
|
95
|
+
virtualizerLength,
|
|
96
|
+
axis,
|
|
97
|
+
progressiveItemSizes
|
|
98
|
+
]);
|
|
99
|
+
/**
|
|
100
|
+
* On scroll timer that will continuously delay callback until scrolling stops
|
|
101
|
+
*/ const onScroll = _react.useCallback((event)=>{
|
|
102
|
+
if (timeoutRef.current) {
|
|
103
|
+
clearTimeout(timeoutRef.current);
|
|
104
|
+
}
|
|
105
|
+
timeoutRef.current = setTimeout(onScrollEnd, 100);
|
|
106
|
+
}, [
|
|
107
|
+
onScrollEnd
|
|
108
|
+
]);
|
|
109
|
+
/**
|
|
110
|
+
* Pagination ref will ensure we attach listeners to containers on change
|
|
111
|
+
* It is returned from hook and merged into the scroll container externally
|
|
112
|
+
*/ const paginationRef = _react.useCallback((instance)=>{
|
|
113
|
+
if (!paginationEnabled) {
|
|
114
|
+
clearListeners();
|
|
115
|
+
scrollContainer.current = null;
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (scrollContainer.current !== instance) {
|
|
119
|
+
clearListeners();
|
|
120
|
+
scrollContainer.current = instance;
|
|
121
|
+
if (scrollContainer.current) {
|
|
122
|
+
scrollContainer.current.addEventListener('scroll', onScroll);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
}, [
|
|
126
|
+
onScroll,
|
|
127
|
+
onScrollEnd,
|
|
128
|
+
paginationEnabled
|
|
129
|
+
]);
|
|
130
|
+
return paginationRef;
|
|
131
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["useDynamicPagination.js"],"sourcesContent":["import * as React from 'react';\nimport { useRef } from 'react';\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 */ export const useDynamicVirtualizerPagination = (virtualizerProps, paginationEnabled = true)=>{\n const { axis = 'vertical', currentIndex, progressiveItemSizes, virtualizerLength } = virtualizerProps;\n const timeoutRef = useRef(null);\n const lastScrollPos = useRef(-1);\n const lastIndexScrolled = useRef(-1);\n const scrollContainer = React.useRef(null);\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 React.useEffect(()=>{\n return ()=>{\n clearListeners();\n };\n // eslint-disable-next-line react-hooks/exhaustive-deps\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 */ const onScrollEnd = React.useCallback(()=>{\n if (!scrollContainer.current || !paginationEnabled || !(progressiveItemSizes === null || progressiveItemSizes === void 0 ? void 0 : progressiveItemSizes.current)) {\n // No container found\n return;\n }\n const currentScrollPos = Math.round(axis === 'vertical' ? scrollContainer.current.scrollTop : scrollContainer.current.scrollLeft);\n let closestItemPos = 0;\n let closestItem = 0;\n const endItem = Math.min(currentIndex + virtualizerLength, progressiveItemSizes.current.length);\n for(let i = currentIndex; i < endItem - 1; i++){\n if (currentScrollPos <= progressiveItemSizes.current[i + 1] && currentScrollPos >= progressiveItemSizes.current[i]) {\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 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 // Safeguard nextItem\n nextItem = Math.min(Math.max(0, nextItem), progressiveItemSizes.current.length);\n closestItemPos = progressiveItemSizes.current[nextItem];\n if (axis === 'vertical') {\n scrollContainer.current.scrollTo({\n top: closestItemPos,\n behavior: 'smooth'\n });\n } else {\n scrollContainer.current.scrollTo({\n left: closestItemPos,\n behavior: 'smooth'\n });\n }\n lastScrollPos.current = progressiveItemSizes.current[nextItem];\n lastIndexScrolled.current = nextItem;\n }, [\n paginationEnabled,\n currentIndex,\n scrollContainer,\n virtualizerLength,\n axis,\n progressiveItemSizes\n ]);\n /**\n * On scroll timer that will continuously delay callback until scrolling stops\n */ const onScroll = React.useCallback((event)=>{\n if (timeoutRef.current) {\n clearTimeout(timeoutRef.current);\n }\n timeoutRef.current = setTimeout(onScrollEnd, 100);\n }, [\n onScrollEnd\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 */ const paginationRef = React.useCallback((instance)=>{\n if (!paginationEnabled) {\n clearListeners();\n scrollContainer.current = null;\n return;\n }\n if (scrollContainer.current !== instance) {\n clearListeners();\n scrollContainer.current = instance;\n if (scrollContainer.current) {\n scrollContainer.current.addEventListener('scroll', onScroll);\n }\n }\n }, // eslint-disable-next-line react-hooks/exhaustive-deps\n [\n onScroll,\n onScrollEnd,\n paginationEnabled\n ]);\n return paginationRef;\n};\n"],"names":["useDynamicVirtualizerPagination","virtualizerProps","paginationEnabled","axis","currentIndex","progressiveItemSizes","virtualizerLength","timeoutRef","useRef","lastScrollPos","lastIndexScrolled","scrollContainer","React","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":";;;;+BAOiBA;;;eAAAA;;;;iEAPM;AAOZ,MAAMA,kCAAkC,CAACC,kBAAkBC,oBAAoB,IAAI;IAC1F,MAAM,EAAEC,OAAO,UAAU,EAAEC,YAAY,EAAEC,oBAAoB,EAAEC,iBAAiB,EAAE,GAAGL;IACrF,MAAMM,aAAaC,IAAAA,aAAM,EAAC;IAC1B,MAAMC,gBAAgBD,IAAAA,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Q,WAAW,CAAC,CAAC2B;QACvC,IAAI,CAAC7C,mBAAmB;YACpBW;YACAF,gBAAgBG,OAAO,GAAG;YAC1B;QACJ;QACA,IAAIH,gBAAgBG,OAAO,KAAKiC,UAAU;YACtClC;YACAF,gBAAgBG,OAAO,GAAGiC;YAC1B,IAAIpC,gBAAgBG,OAAO,EAAE;gBACzBH,gBAAgBG,OAAO,CAACkC,gBAAgB,CAAC,UAAUhC;YACvD;QACJ;IACJ,GACA;QACIA;QACAG;QACAjB;KACH;IACD,OAAO4C;AACX"}
|
|
@@ -24,17 +24,30 @@ function useMeasureList(currentIndex, refLength, totalLength, defaultItemSize) {
|
|
|
24
24
|
const heightArray = _react.useRef(new Array(totalLength).fill(defaultItemSize));
|
|
25
25
|
const refArray = _react.useRef([]);
|
|
26
26
|
const { targetDocument } = (0, _reactsharedcontexts.useFluent_unstable)();
|
|
27
|
+
// This lets us trigger updates when a size change occurs.
|
|
28
|
+
const sizeUpdateCount = (0, _react.useRef)(0);
|
|
27
29
|
// the handler for resize observer
|
|
28
30
|
const handleIndexUpdate = _react.useCallback((index)=>{
|
|
29
31
|
var _refArray_current_index;
|
|
32
|
+
let isChanged = false;
|
|
30
33
|
const boundClientRect = (_refArray_current_index = refArray.current[index]) === null || _refArray_current_index === void 0 ? void 0 : _refArray_current_index.getBoundingClientRect();
|
|
31
34
|
const containerWidth = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.width;
|
|
35
|
+
if (containerWidth !== widthArray.current[currentIndex + index]) {
|
|
36
|
+
isChanged = true;
|
|
37
|
+
}
|
|
32
38
|
widthArray.current[currentIndex + index] = containerWidth || defaultItemSize;
|
|
33
39
|
const containerHeight = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.height;
|
|
40
|
+
if (containerHeight !== heightArray.current[currentIndex + index]) {
|
|
41
|
+
isChanged = true;
|
|
42
|
+
}
|
|
34
43
|
heightArray.current[currentIndex + index] = containerHeight || defaultItemSize;
|
|
44
|
+
if (isChanged) {
|
|
45
|
+
sizeUpdateCount.current = sizeUpdateCount.current + 1;
|
|
46
|
+
}
|
|
35
47
|
}, [
|
|
36
48
|
currentIndex,
|
|
37
|
-
defaultItemSize
|
|
49
|
+
defaultItemSize,
|
|
50
|
+
sizeUpdateCount
|
|
38
51
|
]);
|
|
39
52
|
const handleElementResizeCallback = (entries)=>{
|
|
40
53
|
for (const entry of entries){
|
|
@@ -96,7 +109,8 @@ function useMeasureList(currentIndex, refLength, totalLength, defaultItemSize) {
|
|
|
96
109
|
widthArray,
|
|
97
110
|
heightArray,
|
|
98
111
|
createIndexedRef,
|
|
99
|
-
refArray
|
|
112
|
+
refArray,
|
|
113
|
+
sizeUpdateCount: sizeUpdateCount.current
|
|
100
114
|
};
|
|
101
115
|
}
|
|
102
116
|
function createResizeObserverFromDocument(targetDocument, callback) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["useMeasureList.js"],"sourcesContent":["import * as React from 'react';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\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 */ export function useMeasureList(currentIndex, refLength, totalLength, defaultItemSize) {\n const widthArray = React.useRef(new Array(totalLength).fill(defaultItemSize));\n const heightArray = React.useRef(new Array(totalLength).fill(defaultItemSize));\n const refArray = React.useRef([]);\n const { targetDocument } = useFluent();\n // the handler for resize observer\n const handleIndexUpdate = React.useCallback((index)=>{\n var _refArray_current_index;\n const boundClientRect = (_refArray_current_index = refArray.current[index]) === null || _refArray_current_index === void 0 ? void 0 : _refArray_current_index.getBoundingClientRect();\n const containerWidth = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.width;\n widthArray.current[currentIndex + index] = containerWidth || defaultItemSize;\n const containerHeight = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.height;\n heightArray.current[currentIndex + index] = containerHeight || defaultItemSize;\n }, [\n currentIndex,\n defaultItemSize\n ]);\n const handleElementResizeCallback = (entries)=>{\n for (const entry of entries){\n const target = entry.target;\n // Call the elements own resize handler (indexed)\n target.handleResize();\n }\n };\n React.useEffect(()=>{\n widthArray.current = new Array(totalLength).fill(defaultItemSize);\n heightArray.current = new Array(totalLength).fill(defaultItemSize);\n }, [\n defaultItemSize,\n 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 /* 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 */ const createIndexedRef = React.useCallback((index)=>{\n const measureElementRef = (el)=>{\n if (!targetDocument || !resizeObserver.current) {\n return;\n }\n if (el) {\n el.handleResize = ()=>{\n handleIndexUpdate(index);\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 refArray.current[index] = undefined;\n if (el) {\n refArray.current[index] = el;\n resizeObserver.current.observe(el);\n handleIndexUpdate(index);\n }\n };\n return measureElementRef;\n }, [\n handleIndexUpdate,\n resizeObserver,\n targetDocument\n ]);\n React.useEffect(()=>{\n const _resizeObserver = resizeObserver;\n return ()=>{\n var _resizeObserver_current;\n return (_resizeObserver_current = _resizeObserver.current) === null || _resizeObserver_current === void 0 ? void 0 : _resizeObserver_current.disconnect();\n };\n }, [\n resizeObserver\n ]);\n return {\n widthArray,\n heightArray,\n createIndexedRef,\n 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 */ export function createResizeObserverFromDocument(targetDocument, callback) {\n var _targetDocument_defaultView;\n if (!(targetDocument === null || targetDocument === void 0 ? void 0 : (_targetDocument_defaultView = targetDocument.defaultView) === null || _targetDocument_defaultView === void 0 ? void 0 : _targetDocument_defaultView.ResizeObserver)) {\n return null;\n }\n return new targetDocument.defaultView.ResizeObserver(callback);\n}\n"],"names":["useMeasureList","createResizeObserverFromDocument","currentIndex","refLength","totalLength","defaultItemSize","widthArray","React","useRef","Array","fill","heightArray","refArray","targetDocument","useFluent","handleIndexUpdate","useCallback","index","_refArray_current_index","boundClientRect","current","getBoundingClientRect","containerWidth","width","containerHeight","height","handleElementResizeCallback","entries","entry","target","handleResize","useEffect","resizeObserver","createIndexedRef","measureElementRef","el","undefined","unobserve","observe","_resizeObserver","_resizeObserver_current","disconnect","callback","_targetDocument_defaultView","defaultView","ResizeObserver"],"mappings":";;;;;;;;;;;
|
|
1
|
+
{"version":3,"sources":["useMeasureList.js"],"sourcesContent":["import * as React from 'react';\nimport { useRef } from 'react';\nimport { useFluent_unstable as useFluent } from '@fluentui/react-shared-contexts';\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 */ export function useMeasureList(currentIndex, refLength, totalLength, defaultItemSize) {\n const widthArray = React.useRef(new Array(totalLength).fill(defaultItemSize));\n const heightArray = React.useRef(new Array(totalLength).fill(defaultItemSize));\n const refArray = React.useRef([]);\n const { targetDocument } = useFluent();\n // This lets us trigger updates when a size change occurs.\n const sizeUpdateCount = useRef(0);\n // the handler for resize observer\n const handleIndexUpdate = React.useCallback((index)=>{\n var _refArray_current_index;\n let isChanged = false;\n const boundClientRect = (_refArray_current_index = refArray.current[index]) === null || _refArray_current_index === void 0 ? void 0 : _refArray_current_index.getBoundingClientRect();\n const containerWidth = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.width;\n if (containerWidth !== widthArray.current[currentIndex + index]) {\n isChanged = true;\n }\n widthArray.current[currentIndex + index] = containerWidth || defaultItemSize;\n const containerHeight = boundClientRect === null || boundClientRect === void 0 ? void 0 : boundClientRect.height;\n if (containerHeight !== heightArray.current[currentIndex + index]) {\n isChanged = true;\n }\n heightArray.current[currentIndex + index] = containerHeight || defaultItemSize;\n if (isChanged) {\n sizeUpdateCount.current = sizeUpdateCount.current + 1;\n }\n }, [\n currentIndex,\n defaultItemSize,\n sizeUpdateCount\n ]);\n const handleElementResizeCallback = (entries)=>{\n for (const entry of entries){\n const target = entry.target;\n // Call the elements own resize handler (indexed)\n target.handleResize();\n }\n };\n React.useEffect(()=>{\n widthArray.current = new Array(totalLength).fill(defaultItemSize);\n heightArray.current = new Array(totalLength).fill(defaultItemSize);\n }, [\n defaultItemSize,\n 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 /* 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 */ const createIndexedRef = React.useCallback((index)=>{\n const measureElementRef = (el)=>{\n if (!targetDocument || !resizeObserver.current) {\n return;\n }\n if (el) {\n el.handleResize = ()=>{\n handleIndexUpdate(index);\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 refArray.current[index] = undefined;\n if (el) {\n refArray.current[index] = el;\n resizeObserver.current.observe(el);\n handleIndexUpdate(index);\n }\n };\n return measureElementRef;\n }, [\n handleIndexUpdate,\n resizeObserver,\n targetDocument\n ]);\n React.useEffect(()=>{\n const _resizeObserver = resizeObserver;\n return ()=>{\n var _resizeObserver_current;\n return (_resizeObserver_current = _resizeObserver.current) === null || _resizeObserver_current === void 0 ? void 0 : _resizeObserver_current.disconnect();\n };\n }, [\n resizeObserver\n ]);\n return {\n widthArray,\n heightArray,\n createIndexedRef,\n refArray,\n 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 */ export function createResizeObserverFromDocument(targetDocument, callback) {\n var _targetDocument_defaultView;\n if (!(targetDocument === null || targetDocument === void 0 ? void 0 : (_targetDocument_defaultView = targetDocument.defaultView) === null || _targetDocument_defaultView === void 0 ? void 0 : _targetDocument_defaultView.ResizeObserver)) {\n return null;\n }\n return new targetDocument.defaultView.ResizeObserver(callback);\n}\n"],"names":["useMeasureList","createResizeObserverFromDocument","currentIndex","refLength","totalLength","defaultItemSize","widthArray","React","useRef","Array","fill","heightArray","refArray","targetDocument","useFluent","sizeUpdateCount","handleIndexUpdate","useCallback","index","_refArray_current_index","isChanged","boundClientRect","current","getBoundingClientRect","containerWidth","width","containerHeight","height","handleElementResizeCallback","entries","entry","target","handleResize","useEffect","resizeObserver","createIndexedRef","measureElementRef","el","undefined","unobserve","observe","_resizeObserver","_resizeObserver_current","disconnect","callback","_targetDocument_defaultView","defaultView","ResizeObserver"],"mappings":";;;;;;;;;;;IASoBA,cAAc;eAAdA;;IAoGAC,gCAAgC;eAAhCA;;;;iEA7GG;qCAEyB;AAOrC,SAASD,eAAeE,YAAY,EAAEC,SAAS,EAAEC,WAAW,EAAEC,eAAe;IACpF,MAAMC,aAAaC,OAAMC,MAAM,CAAC,IAAIC,MAAML,aAAaM,IAAI,CAACL;IAC5D,MAAMM,cAAcJ,OAAMC,MAAM,CAAC,IAAIC,MAAML,aAAaM,I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