import * as React from "react" const MOBILE_BREAKPOINT = 768 export function useIsMobile() { const [isMobile, setIsMobile] = React.useState(undefined) React.useEffect(() => { const onChange = () => { setIsMobile(window.innerWidth < MOBILE_BREAKPOINT) } if (typeof window.matchMedia !== "function") { onChange() window.addEventListener("resize", onChange) return () => window.removeEventListener("resize", onChange) } const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`) mql.addEventListener("change", onChange) onChange() return () => mql.removeEventListener("change", onChange) }, []) return !!isMobile }