Portal/rsconcept/frontend/src/hooks/useWindowSize.ts

35 lines
787 B
TypeScript
Raw Normal View History

2024-06-07 20:17:03 +03:00
'use client';
import { useEffect, useState } from 'react';
import { PARAMETER } from '@/utils/constants';
function useWindowSize() {
const isClient = typeof window === 'object';
function getSize() {
return {
width: isClient ? window.innerWidth : undefined,
height: isClient ? window.innerHeight : undefined,
isSmall: isClient && window.innerWidth < PARAMETER.smallScreen
};
}
const [windowSize, setWindowSize] = useState(getSize);
useEffect(() => {
if (!isClient) {
return;
}
function handleResize() {
setWindowSize(getSize());
}
window.addEventListener('resize', handleResize);
return () => window.removeEventListener('resize', handleResize);
}, []);
return windowSize;
}
export default useWindowSize;