@coinmeca/ui
Version:
This is a [Next.js](https://nextjs.org/) project bootstrapped with [`create-next-app`](https://github.com/vercel/next.js/tree/canary/packages/create-next-app).
32 lines • 1.25 kB
JavaScript
"use client";
import { useState, useEffect } from "react";
export default function useWindowSize() {
const w = typeof window !== "undefined" ? window : global;
const [windowWidth, setWindowWidth] = useState(w?.innerWidth || 1920);
const [windowHeight, setWindowHeight] = useState(w?.innerHeight || 1080);
const [windowSize, setWindowSize] = useState({
width: windowWidth,
height: windowHeight,
});
useEffect(() => {
function windowResize() {
if (windowWidth !== window.innerWidth)
setWindowWidth(window.innerWidth);
if (windowHeight !== window.innerHeight)
setWindowHeight(window.innerHeight);
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
}
window.addEventListener("change", windowResize);
window.addEventListener("resize", windowResize);
windowResize();
return () => {
window.removeEventListener("change", windowResize);
window.removeEventListener("resize", windowResize);
};
}, []);
return { windowSize, windowWidth, windowHeight };
}
//# sourceMappingURL=useWindowSize.js.map