silphius/app/react/hooks/use-rect.js
2024-07-20 07:07:22 -05:00

17 lines
415 B
JavaScript

import {useLayoutEffect, useState} from 'react'
import useResizeObserver from '@react-hook/resize-observer'
export default function useRect(target) {
const [rect, setRect] = useState();
useLayoutEffect(() => {
setRect(target.current.getBoundingClientRect())
}, [target]);
useResizeObserver(
target,
(entry) => {
setRect(entry.target.getBoundingClientRect());
},
);
return rect;
}