import { RefObject, useState, useEffect } from 'react' // todo: consider consolidation with use-measure-size export const useElementWidth = (ref: RefObject) => { const [width, setWidth] = useState() useEffect(() => { const handleResize = () => { setWidth(ref.current?.clientWidth) } handleResize() window.addEventListener('resize', handleResize) return () => { window.removeEventListener('resize', handleResize) } }, [ref]) return width }