2020-08-21 17:49:13 +02:00
|
|
|
// Widths are taken from "ui/scss/init/vars.scss"
|
2022-02-04 19:37:46 +01:00
|
|
|
import React, { useRef } from 'react';
|
2020-08-21 17:49:13 +02:00
|
|
|
|
2022-01-31 13:34:44 +01:00
|
|
|
export function useWindowSize(fn) {
|
2020-08-21 17:49:13 +02:00
|
|
|
const isWindowClient = typeof window === 'object';
|
2022-02-04 17:25:00 +01:00
|
|
|
const initialState = fn ? fn(window.innerWidth) : window.innerWidth;
|
|
|
|
const [windowSize, setWindowSize] = React.useState(isWindowClient ? initialState : undefined);
|
2022-02-04 19:37:46 +01:00
|
|
|
const prev = useRef();
|
2020-08-21 17:49:13 +02:00
|
|
|
|
|
|
|
React.useEffect(() => {
|
|
|
|
function setSize() {
|
2022-01-31 13:34:44 +01:00
|
|
|
if (fn) {
|
|
|
|
const curr = fn(window.innerWidth);
|
2022-02-04 19:37:46 +01:00
|
|
|
if (prev !== curr) {
|
|
|
|
setWindowSize(curr);
|
|
|
|
prev.current = curr;
|
|
|
|
}
|
2022-01-31 13:34:44 +01:00
|
|
|
} else setWindowSize(window.innerWidth);
|
2020-08-21 17:49:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (isWindowClient) {
|
|
|
|
window.addEventListener('resize', setSize);
|
|
|
|
|
|
|
|
return () => window.removeEventListener('resize', setSize);
|
|
|
|
}
|
2022-02-04 19:37:46 +01:00
|
|
|
}, [fn, isWindowClient, setWindowSize, windowSize]);
|
2020-08-21 17:49:13 +02:00
|
|
|
|
|
|
|
return windowSize;
|
|
|
|
}
|
2020-08-10 22:47:39 +02:00
|
|
|
|
|
|
|
export function useIsMobile() {
|
2022-01-31 13:34:44 +01:00
|
|
|
return useWindowSize((windowSize) => windowSize < 901);
|
2020-08-10 22:47:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function useIsMediumScreen() {
|
2022-01-31 13:34:44 +01:00
|
|
|
return useWindowSize((windowSize) => windowSize < 1151);
|
2020-08-21 17:49:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
export function useIsLargeScreen() {
|
2022-01-31 13:34:44 +01:00
|
|
|
return useWindowSize((windowSize) => windowSize > 1600);
|
2020-08-10 22:47:39 +02:00
|
|
|
}
|