()
| 7 | |
| 8 | /** Measures the width/height of an element using ResizeObserver. */ |
| 9 | export const useMeasure = <T extends HTMLElement>(): [ |
| 10 | React.RefObject<T>, |
| 11 | Bounds, |
| 12 | ] => { |
| 13 | const ref = React.useRef<T>(null); |
| 14 | const [bounds, setBounds] = React.useState<Bounds>({ |
| 15 | width: undefined, |
| 16 | height: undefined, |
| 17 | }); |
| 18 | |
| 19 | React.useLayoutEffect(() => { |
| 20 | if (!ref.current) return; |
| 21 | |
| 22 | const observer = new ResizeObserver((entries) => { |
| 23 | const entry = entries[0]; |
| 24 | setBounds({ |
| 25 | width: entry.contentRect.width, |
| 26 | height: entry.contentRect.height, |
| 27 | }); |
| 28 | }); |
| 29 | observer.observe(ref.current); |
| 30 | |
| 31 | return () => observer.disconnect(); |
| 32 | }, []); |
| 33 | |
| 34 | return [ref, bounds]; |
| 35 | }; |
no outgoing calls
no test coverage detected
searching dependent graphs…