File size: 469 Bytes
b91e262 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | import { useEffect, useState } from 'react'
export function useMeasureWidth(
ref: React.RefObject<HTMLDivElement | null>
): number {
const [width, setWidth] = useState<number>(0)
useEffect(() => {
const el = ref.current
if (!el) {
return
}
const observer = new ResizeObserver(([{ contentRect }]) => {
setWidth(contentRect.width)
})
observer.observe(el)
return () => observer.disconnect()
}, [ref])
return width
}
|