Cleanup - unused files / unused exports / duplicate exports
[ProtonMail-WebClient.git] / packages / components / hooks / useWindowSize.ts
blob88a6a2f605b62d8f21b30cc4715e0821214f5a9c
1 import { useEffect, useState } from 'react';
3 import debounce from '@proton/utils/debounce';
5 const getWindowSize = (): [width: number, height: number] => {
6     return [window.innerWidth, window.innerHeight];
7 };
9 const useWindowSize = () => {
10     const [windowSize, setWindowSize] = useState(() => getWindowSize());
12     useEffect(() => {
13         const reducer = (oldValue: [number, number], newValue: [number, number]) => {
14             if (oldValue[0] === newValue[0] && oldValue[1] === newValue[1]) {
15                 return oldValue;
16             }
17             return newValue;
18         };
20         const onResize = debounce(() => {
21             setWindowSize((old) => reducer(old, getWindowSize()));
22         }, 100);
24         window.addEventListener('resize', onResize);
25         setWindowSize((old) => reducer(old, getWindowSize()));
26         return () => {
27             window.removeEventListener('resize', onResize);
28         };
29     }, []);
31     return windowSize;
34 export default useWindowSize;