Files
shakespeare/src/hooks/useIsMobile.tsx
T
Siddharth Singh 993b5ece9c switch back to width breakpoint for useMobileView
@alexgleason pointed out aspect ratio would break screens that were
physically wide but not quite the shape 11/9 expects
2025-09-19 00:37:11 +05:30

20 lines
592 B
TypeScript

import { useEffect, useState } from "react"
const MOBILE_BREAKPOINT = 1080;
export function useIsMobile(): boolean {
const [isMobile, setIsMobile] = useState(window.innerWidth < MOBILE_BREAKPOINT);
useEffect(() => {
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
const onChange = () => {
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
}
mql.addEventListener("change", onChange);
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
return () => mql.removeEventListener("change", onChange);
}, []);
return !!isMobile;
}