diff --git a/src/hooks/useNodeDimensions.ts b/src/hooks/useNodeDimensions.ts new file mode 100644 index 00000000..798de902 --- /dev/null +++ b/src/hooks/useNodeDimensions.ts @@ -0,0 +1,31 @@ +import { useCallback } from 'react'; +import shallow from 'zustand/shallow'; + +import { Rect } from '../types'; +import { useStore } from '../store'; + +function useNodeDimensions(id: string): Rect | null { + const nodeDimensions = useStore( + useCallback( + (s) => { + const nodeItem = s.nodeInternals.get(id); + + if (!nodeItem) { + return null; + } + + return { + ...nodeItem.positionAbsolute, + width: nodeItem.width, + height: nodeItem.height, + }; + }, + [id] + ), + shallow + ); + + return nodeDimensions; +} + +export default useNodeDimensions;