109 lines
4.5 KiB
JavaScript
109 lines
4.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
Object.defineProperty(exports, "useDynamicVirtualizerMeasure", {
|
|
enumerable: true,
|
|
get: function() {
|
|
return useDynamicVirtualizerMeasure;
|
|
}
|
|
});
|
|
const _interop_require_wildcard = require("@swc/helpers/_/_interop_require_wildcard");
|
|
const _reactutilities = require("@fluentui/react-utilities");
|
|
const _react = /*#__PURE__*/ _interop_require_wildcard._(require("react"));
|
|
const _useResizeObserverRef = require("./useResizeObserverRef");
|
|
const useDynamicVirtualizerMeasure = (virtualizerProps)=>{
|
|
const { defaultItemSize, direction = 'vertical', numItems, getItemSize, currentIndex } = virtualizerProps;
|
|
const indexRef = (0, _react.useRef)(currentIndex);
|
|
indexRef.current = currentIndex;
|
|
const [state, setState] = _react.useState({
|
|
virtualizerLength: 0,
|
|
virtualizerBufferItems: 0,
|
|
virtualizerBufferSize: 0
|
|
});
|
|
const { virtualizerLength, virtualizerBufferItems, virtualizerBufferSize } = state;
|
|
const container = _react.useRef(null);
|
|
const handleScrollResize = _react.useCallback((scrollRef)=>{
|
|
if (!(scrollRef === null || scrollRef === void 0 ? void 0 : scrollRef.current)) {
|
|
// Error? ignore?
|
|
return;
|
|
}
|
|
if (scrollRef.current !== container.current) {
|
|
container.current = scrollRef.current;
|
|
}
|
|
const containerSize = direction === 'vertical' ? scrollRef.current.getBoundingClientRect().height : scrollRef.current.getBoundingClientRect().width;
|
|
let indexSizer = 0;
|
|
let length = 0;
|
|
while(indexSizer <= containerSize && length < numItems){
|
|
const iItemSize = getItemSize(indexRef.current + length);
|
|
// Increment
|
|
indexSizer += iItemSize;
|
|
length++;
|
|
}
|
|
/*
|
|
* Number of items to append at each end, i.e. 'preload' each side before entering view.
|
|
*/ const bufferItems = Math.max(Math.floor(length / 4), 4);
|
|
/*
|
|
* This is how far we deviate into the bufferItems to detect a redraw.
|
|
*/ const bufferSize = Math.max(Math.floor(length / 8 * defaultItemSize), 1);
|
|
const totalLength = length + bufferItems * 2 + 1;
|
|
setState({
|
|
virtualizerLength: totalLength,
|
|
virtualizerBufferSize: bufferSize,
|
|
virtualizerBufferItems: bufferItems
|
|
});
|
|
}, [
|
|
defaultItemSize,
|
|
direction,
|
|
getItemSize,
|
|
numItems
|
|
]);
|
|
const resizeCallback = _react.useCallback((_entries, // eslint-disable-next-line no-restricted-globals
|
|
_observer, scrollRef)=>{
|
|
if (scrollRef) {
|
|
handleScrollResize(scrollRef);
|
|
}
|
|
}, [
|
|
handleScrollResize
|
|
]);
|
|
const scrollRef = (0, _useResizeObserverRef.useResizeObserverRef_unstable)(resizeCallback);
|
|
(0, _reactutilities.useIsomorphicLayoutEffect)(()=>{
|
|
var _container_current, _container_current1;
|
|
if (!container.current) {
|
|
return;
|
|
}
|
|
const containerSize = direction === 'vertical' ? ((_container_current = container.current) === null || _container_current === void 0 ? void 0 : _container_current.getBoundingClientRect().height) * 1.5 : ((_container_current1 = container.current) === null || _container_current1 === void 0 ? void 0 : _container_current1.getBoundingClientRect().width) * 1.5;
|
|
let couldBeSmaller = false;
|
|
let recheckTotal = 0;
|
|
for(let i = currentIndex; i < currentIndex + virtualizerLength; i++){
|
|
const newItemSize = getItemSize(i);
|
|
recheckTotal += newItemSize;
|
|
const newLength = i - currentIndex;
|
|
const bufferItems = Math.max(Math.floor(newLength / 4), 2);
|
|
const totalNewLength = newLength + bufferItems * 2 + 4;
|
|
const compareLengths = totalNewLength < virtualizerLength;
|
|
if (recheckTotal > containerSize && compareLengths) {
|
|
couldBeSmaller = true;
|
|
break;
|
|
}
|
|
}
|
|
// Check if the render has caused us to need a re-calc of virtualizer length
|
|
if (recheckTotal < containerSize || couldBeSmaller) {
|
|
handleScrollResize(container);
|
|
}
|
|
}, [
|
|
getItemSize,
|
|
currentIndex,
|
|
direction,
|
|
virtualizerLength,
|
|
resizeCallback,
|
|
handleScrollResize
|
|
]);
|
|
return {
|
|
virtualizerLength,
|
|
bufferItems: virtualizerBufferItems,
|
|
bufferSize: virtualizerBufferSize,
|
|
scrollRef
|
|
};
|
|
};
|