forked from www/www-new
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
38 lines
757 B
38 lines
757 B
2 years ago
|
import { useEffect, useState } from "react";
|
||
|
|
||
|
interface WindowDimension {
|
||
|
width: number;
|
||
|
height: number;
|
||
|
}
|
||
|
|
||
|
function getWindowDimension() {
|
||
|
const { innerWidth: width, innerHeight: height } = window;
|
||
|
return {
|
||
|
width,
|
||
|
height,
|
||
|
};
|
||
|
}
|
||
|
|
||
|
export function useWindowDimension(): WindowDimension {
|
||
|
const [windowSize, setWindowDimension] = useState<WindowDimension>({
|
||
|
width: 0,
|
||
|
height: 0,
|
||
|
});
|
||
|
|
||
|
useEffect(() => {
|
||
|
const handleResize = () => {
|
||
|
setWindowDimension(getWindowDimension());
|
||
|
};
|
||
|
|
||
|
// Set size at the first client-side load
|
||
|
handleResize();
|
||
|
window.addEventListener("resize", handleResize);
|
||
|
|
||
|
return () => {
|
||
|
window.removeEventListener("resize", handleResize);
|
||
|
};
|
||
|
}, []);
|
||
|
|
||
|
return windowSize;
|
||
|
}
|