useContainerWidth.ts•538 B
import { useEffect, useRef, useState } from "react";
export function useContainerWidth<T extends HTMLElement>() {
const ref = useRef<T>(null);
const [width, setWidth] = useState(0);
useEffect(() => {
if (!ref.current) return;
const handleResize = () => {
if (ref.current) setWidth(ref.current.offsetWidth);
};
handleResize();
const observer = new window.ResizeObserver(handleResize);
observer.observe(ref.current);
return () => observer.disconnect();
}, []);
return [ref, width] as const;
}