new hook to handle size using mui hook usemediaquery
This commit is contained in:
committed by
Benoit TELLIER
parent
9c572aea45
commit
c1d3cf5f3d
@@ -1,38 +1,13 @@
|
|||||||
import { useState, useEffect } from "react";
|
import { useMediaQuery, useTheme } from '@linagora/twake-mui'
|
||||||
|
|
||||||
export const DEVICES_SCREEN_LIMITS = {
|
|
||||||
tablet: 925,
|
|
||||||
mobile: 450,
|
|
||||||
} as const;
|
|
||||||
|
|
||||||
function useWindowWidth(): number {
|
|
||||||
const [width, setWidth] = useState(() => window.innerWidth);
|
|
||||||
|
|
||||||
useEffect(() => {
|
|
||||||
let rafId: number | null = null;
|
|
||||||
|
|
||||||
const onResize = () => {
|
|
||||||
if (rafId !== null) cancelAnimationFrame(rafId);
|
|
||||||
rafId = requestAnimationFrame(() => setWidth(window.innerWidth));
|
|
||||||
};
|
|
||||||
|
|
||||||
window.addEventListener("resize", onResize);
|
|
||||||
return () => {
|
|
||||||
window.removeEventListener("resize", onResize);
|
|
||||||
if (rafId !== null) cancelAnimationFrame(rafId);
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
return width;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function useScreenSizeDetection() {
|
export function useScreenSizeDetection() {
|
||||||
const width = useWindowWidth();
|
const theme = useTheme()
|
||||||
|
|
||||||
|
const isDesktop = useMediaQuery(theme.breakpoints.up('lg'))
|
||||||
|
const isMobile = useMediaQuery(theme.breakpoints.down('sm'))
|
||||||
|
|
||||||
return {
|
return {
|
||||||
isTooSmall: width <= DEVICES_SCREEN_LIMITS.mobile,
|
isTooSmall: isMobile,
|
||||||
isTablet:
|
isTablet: !isMobile && !isDesktop
|
||||||
width <= DEVICES_SCREEN_LIMITS.tablet &&
|
}
|
||||||
width > DEVICES_SCREEN_LIMITS.mobile,
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user