dosemate / frontend /src /useMediaQuery.jsx
DoseMate
Make the whole UI responsive (mobile drawer, fluid grids, responsive auth)
852e9a3
Raw History Blame Contribute Delete
604 Bytes
import { useState, useEffect } from "react"
// Returns true when the viewport is at/below the breakpoint (mobile/tablet).
export function useIsMobile(breakpoint = 820) {
const query = `(max-width: ${breakpoint}px)`
const [matches, setMatches] = useState(
() => typeof window !== "undefined" && window.matchMedia(query).matches
)
useEffect(() => {
const mq = window.matchMedia(query)
const onChange = () => setMatches(mq.matches)
onChange()
mq.addEventListener("change", onChange)
return () => mq.removeEventListener("change", onChange)
}, [query])
return matches
}