@dnanpm/styleguide
Version:
DNA Styleguide repository provides the set of components and theme object used in various DNA projects.
27 lines (24 loc) • 819 B
JavaScript
import { useState, useEffect } from 'react';
const useWindowSize = (collapseSize) => {
const [width, setWidth] = useState();
const [height, setHeight] = useState();
useEffect(() => {
if (typeof window === 'undefined') {
return () => { };
}
const updateSize = () => {
setWidth(window.innerWidth);
setHeight(window.innerHeight);
};
window.addEventListener('resize', updateSize);
// Call handler right away so state gets updated with initial window size
updateSize();
return () => window.removeEventListener('resize', updateSize);
}, []);
return {
width,
height,
isMobile: Boolean(collapseSize && width && width < collapseSize),
};
};
export { useWindowSize as default };