36 lines
789 B
TypeScript
36 lines
789 B
TypeScript
|
|
import { useEffect, useState } from 'react';
|
||
|
|
|
||
|
|
// This hook is used to get the window size
|
||
|
|
// It returns an object with the width and height of the window
|
||
|
|
// Works with window resizing as well, not to be confused with isMobile from is-mobile package
|
||
|
|
|
||
|
|
interface WindowSize {
|
||
|
|
width: number;
|
||
|
|
height: number;
|
||
|
|
}
|
||
|
|
|
||
|
|
export function useWindowSize(): WindowSize {
|
||
|
|
const [windowSize, setWindowSize] = useState<WindowSize>({
|
||
|
|
width: 0,
|
||
|
|
height: 0,
|
||
|
|
});
|
||
|
|
|
||
|
|
const handleSize = () => {
|
||
|
|
setWindowSize({
|
||
|
|
width: window.innerWidth,
|
||
|
|
height: window.innerHeight,
|
||
|
|
});
|
||
|
|
};
|
||
|
|
|
||
|
|
useEffect(() => {
|
||
|
|
handleSize();
|
||
|
|
window.addEventListener('resize', handleSize);
|
||
|
|
|
||
|
|
return () => {
|
||
|
|
window.removeEventListener('resize', handleSize);
|
||
|
|
};
|
||
|
|
}, []);
|
||
|
|
|
||
|
|
return windowSize;
|
||
|
|
}
|