phanpy/src/utils/useWindowSize.js

29 lines
559 B
JavaScript
Raw Normal View History

import { useLayoutEffect, useState } from 'preact/hooks';
export default function useWindowSize() {
const [size, setSize] = useState({
width: null,
height: null,
});
useLayoutEffect(() => {
const handleResize = () => {
setSize({
width: window.innerWidth,
height: window.innerHeight,
});
};
handleResize();
window.addEventListener('resize', handleResize, {
passive: true,
});
return () => {
window.removeEventListener('resize', handleResize);
};
}, []);
return size;
}