mirror of
https://github.com/IT4Change/ohmyform-ui.git
synced 2025-12-13 09:45:50 +00:00
30 lines
700 B
TypeScript
30 lines
700 B
TypeScript
import {useEffect, useState} from 'react'
|
|
|
|
export const useWindowSize = () => {
|
|
const isClient = typeof window === 'object';
|
|
|
|
function getSize() {
|
|
return {
|
|
width: isClient ? window.innerWidth : undefined,
|
|
height: isClient ? window.innerHeight : undefined
|
|
};
|
|
}
|
|
|
|
const [windowSize, setWindowSize] = useState(getSize);
|
|
|
|
useEffect(() => {
|
|
if (!isClient) {
|
|
return;
|
|
}
|
|
|
|
function handleResize() {
|
|
setWindowSize(getSize());
|
|
}
|
|
|
|
window.addEventListener('resize', handleResize);
|
|
return () => window.removeEventListener('resize', handleResize);
|
|
}, []); // Empty array ensures that effect is only run on mount and unmount
|
|
|
|
return windowSize;
|
|
}
|