This commit is contained in:
Alejandro Gomez
2023-02-12 22:26:43 +01:00
committed by Kieran
parent cca7c24770
commit 728472d7bc
2 changed files with 21 additions and 1 deletions

View File

@ -0,0 +1,20 @@
import { useRef, useState, useEffect } from "react";
export default function usePageWidth() {
const ref = useRef<HTMLDivElement | null>(document.querySelector(".page"));
const [width, setWidth] = useState(0);
useEffect(() => {
const updateSize = () => {
if (ref.current) {
setWidth(ref.current.offsetWidth);
}
};
window.addEventListener("resize", updateSize);
updateSize();
return () => window.removeEventListener("resize", updateSize);
}, [ref]);
return width;
}