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

48 lines 1.73 kB
import { useCallback, useEffect, useRef, useState } from 'react'; // helpers import { getHeight } from './helpers/height'; export const useCustomHeightFromChildrens = ({ limit, observer = [], customCalcHeight, shouldCalculateHeight = true, }) => { // States const [height, setHeight] = useState(''); // References const optionsListCollectionRef = useRef([]); const getList = () => { if (!optionsListCollectionRef.current) { // Initialize the Map on first usage. optionsListCollectionRef.current = []; } return optionsListCollectionRef.current; }; const calcHeight = useCallback(({ elementList, customCalcHeight, }) => { if (!shouldCalculateHeight || !elementList || elementList.length === 0) { return; } if (customCalcHeight) { setHeight(customCalcHeight(elementList)); } else { setHeight(getHeight(elementList, limit)); } }, [...observer, customCalcHeight, limit, shouldCalculateHeight]); // When the ref is initiallized, calcHeight will be called const optionsListRefCollection = useCallback(node => { const refList = getList(); if (node) { refList[node.index] = node.ref; } else { refList.splice(node.index, 1); } calcHeight({ elementList: refList, customCalcHeight }); }, []); useEffect(() => { const refList = getList(); calcHeight({ elementList: refList, customCalcHeight }); }, [calcHeight]); return { height, optionsListRefCollection, optionsListCollectionRef, }; }; //# sourceMappingURL=useCustomHeightFromChildren.js.map