UNPKG

@rc-component/notification

Version:
37 lines 975 B
import * as React from 'react'; /** * Stores measured node sizes by key and exposes a ref callback to update them. */ export default function useSizes() { const [sizeMap, setSizeMap] = React.useState({}); const setNodeSize = React.useCallback((key, node) => { if (!node) { setSizeMap(prevSizeMap => { if (!(key in prevSizeMap)) { return prevSizeMap; } const nextSizeMap = { ...prevSizeMap }; delete nextSizeMap[key]; return nextSizeMap; }); return; } const nextSize = { width: node.offsetWidth, height: node.offsetHeight }; setSizeMap(prevSizeMap => { const prevSize = prevSizeMap[key]; if (prevSize && prevSize.width === nextSize.width && prevSize.height === nextSize.height) { return prevSizeMap; } return { ...prevSizeMap, [key]: nextSize }; }); }, []); return [sizeMap, setNodeSize]; }