UNPKG

@kubit-ui-web/react-components

Version:

Kubit React Components is a customizable, accessible library of React web components, designed to enhance your application's user experience

52 lines 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useCustomHeightFromChildrens = void 0; const react_1 = require("react"); // helpers const height_1 = require("./helpers/height"); const useCustomHeightFromChildrens = ({ limit, observer = [], customCalcHeight, shouldCalculateHeight = true, }) => { // States const [height, setHeight] = (0, react_1.useState)(''); // References const optionsListCollectionRef = (0, react_1.useRef)([]); const getList = () => { if (!optionsListCollectionRef.current) { // Initialize the Map on first usage. optionsListCollectionRef.current = []; } return optionsListCollectionRef.current; }; const calcHeight = (0, react_1.useCallback)(({ elementList, customCalcHeight, }) => { if (!shouldCalculateHeight || !elementList || elementList.length === 0) { return; } if (customCalcHeight) { setHeight(customCalcHeight(elementList)); } else { setHeight((0, height_1.getHeight)(elementList, limit)); } }, [...observer, customCalcHeight, limit, shouldCalculateHeight]); // When the ref is initiallized, calcHeight will be called const optionsListRefCollection = (0, react_1.useCallback)(node => { const refList = getList(); if (node) { refList[node.index] = node.ref; } else { refList.splice(node.index, 1); } calcHeight({ elementList: refList, customCalcHeight }); }, []); (0, react_1.useEffect)(() => { const refList = getList(); calcHeight({ elementList: refList, customCalcHeight }); }, [calcHeight]); return { height, optionsListRefCollection, optionsListCollectionRef, }; }; exports.useCustomHeightFromChildrens = useCustomHeightFromChildrens; //# sourceMappingURL=useCustomHeightFromChildren.js.map