Files
app/frontend/src/hooks/useMobile.ts
T

26 lines
886 B
TypeScript

import * as React from "react"
const MOBILE_BREAKPOINT = 768
export function useIsMobile() {
// Read on the first render, not after it: a node on the flow canvas draws
// its handles for the direction this returns, and React Flow measures them
// once. Starting at `false` drew them for a desktop first, so whether an
// edge met its handle depended on which of the two React measured.
const [isMobile, setIsMobile] = React.useState(
() => window.innerWidth < MOBILE_BREAKPOINT,
)
React.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
}