diff --git a/src/hooks/useResizeObserver.js b/src/hooks/useResizeObserver.js new file mode 100644 index 000000000..267369d62 --- /dev/null +++ b/src/hooks/useResizeObserver.js @@ -0,0 +1,20 @@ +import { useLayoutEffect, useState, useRef } from 'react'; + +const useResizeObserver = () => { + const ref = useRef(null); + const [height, setHeight] = useState(0); + + const resizeObserver = new ResizeObserver((entries) => { + entries.forEach((entry) => { + setHeight(entry.contentBoxSize.blockSize); + }); + }); + + useLayoutEffect(() => { + resizeObserver.observe(ref.current); + }, []); + + return [ref, height]; +}; + +export default useResizeObserver;