think/packages/client/src/hooks/use-window-size.tsx

28 lines
606 B
TypeScript
Raw Normal View History

2022-03-12 03:27:56 +00:00
import { useState, useEffect } from 'react';
2022-02-20 11:51:55 +00:00
interface Size {
width: number | undefined;
height: number | undefined;
}
export function useWindowSize(): Size {
const [windowSize, setWindowSize] = useState<Size>({
width: undefined,
height: undefined,
});
useEffect(() => {
function handleResize() {
setWindowSize({
width: window.innerWidth,
height: window.innerHeight,
});
}
2022-03-12 03:27:56 +00:00
window.addEventListener('resize', handleResize);
2022-02-20 11:51:55 +00:00
handleResize();
2022-03-12 03:27:56 +00:00
return () => window.removeEventListener('resize', handleResize);
2022-02-20 11:51:55 +00:00
}, []);
return windowSize;
}