mirror of
https://github.com/spacedriveapp/spacedrive
synced 2024-07-14 08:54:04 +00:00
* video intro bg * test hsl * test video bg * run tests * comment * mob intro * git glitch * git * webm type
40 lines
810 B
TypeScript
40 lines
810 B
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
/*
|
|
A simple hook that returns the window size
|
|
*/
|
|
|
|
type hookReturn = {
|
|
width: number | null;
|
|
height: number | null;
|
|
};
|
|
|
|
export const useWindowSize = (): hookReturn => {
|
|
const [windowSize, setWindowSize] = useState<hookReturn>({
|
|
width: null,
|
|
height: null
|
|
});
|
|
|
|
useEffect(() => {
|
|
if (typeof window === 'undefined') {
|
|
return;
|
|
}
|
|
const handleResize = () => {
|
|
setWindowSize({
|
|
width: window.innerWidth,
|
|
height: window.innerHeight
|
|
});
|
|
};
|
|
|
|
window.addEventListener('load', handleResize);
|
|
window.addEventListener('resize', handleResize);
|
|
|
|
return () => {
|
|
window.removeEventListener('resize', handleResize);
|
|
window.removeEventListener('load', handleResize);
|
|
};
|
|
}, [windowSize.width, windowSize.height]);
|
|
|
|
return windowSize;
|
|
};
|