revite/src/lib/windowSize.ts

25 lines
607 B
TypeScript
Raw Normal View History

2021-06-19 07:34:53 -04:00
import { useEffect, useState } from "preact/hooks";
export function useWindowSize() {
2021-07-05 06:25:20 -04:00
const [windowSize, setWindowSize] = useState({
width: window.innerWidth,
height: window.innerHeight,
});
2021-06-19 07:34:53 -04:00
2021-07-05 06:25:20 -04:00
useEffect(() => {
function handleResize() {
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
}
2021-06-19 07:34:53 -04:00
2021-07-05 06:25:20 -04:00
window.addEventListener("resize", handleResize);
handleResize();
2021-06-19 07:34:53 -04:00
2021-07-05 06:25:20 -04:00
return () => window.removeEventListener("resize", handleResize);
}, []);
2021-06-19 07:34:53 -04:00
2021-07-05 06:25:20 -04:00
return windowSize;
2021-06-19 07:34:53 -04:00
}