madiator.com/ui/effects/use-screensize.js
2021-08-16 12:11:25 +02:00

36 lines
884 B
JavaScript

// Widths are taken from "ui/scss/init/vars.scss"
import React from 'react';
function useWindowSize() {
const isWindowClient = typeof window === 'object';
const [windowSize, setWindowSize] = React.useState(isWindowClient ? window.innerWidth : undefined);
React.useEffect(() => {
function setSize() {
setWindowSize(window.innerWidth);
}
if (isWindowClient) {
window.addEventListener('resize', setSize);
return () => window.removeEventListener('resize', setSize);
}
}, [isWindowClient, setWindowSize]);
return windowSize;
}
export function useIsMobile() {
const windowSize = useWindowSize();
return windowSize < 901;
}
export function useIsMediumScreen() {
const windowSize = useWindowSize();
return windowSize < 1151;
}
export function useIsLargeScreen() {
const windowSize = useWindowSize();
return windowSize > 1600;
}