Skip to content

Commit

Permalink
feat(hooks): add useNodeDimensions
Browse files Browse the repository at this point in the history
  • Loading branch information
moklick committed Dec 7, 2021
1 parent 79a32ae commit 731918d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions 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;

0 comments on commit 731918d

Please sign in to comment.