Created
August 5, 2023 10:24
-
-
Save samuelkarani/bcf83d4d7d7903f034c7a61f55060d1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export function useBoundingClientRect<T extends HTMLElement>() { | |
const [rect, setRect] = useState<DOMRect | null>(null); | |
const ref = useRef<T>(null); | |
const update = useCallback(() => { | |
if (ref.current) setRect(ref.current.getBoundingClientRect()); | |
else setRect(null); | |
}, []); | |
useEffectOnce(() => { | |
setTimeout(update, 500); | |
}); | |
useEventListener("resize", update); | |
return [ref, rect] as const; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment