import * as React from "react"

const MOBILE_BREAKPOINT = 768

export function useIsMobile(defaultValue = false) {
  const [isMobile, setIsMobile] = React.useState<boolean>(defaultValue)

  React.useEffect(() => {
    const update = () => setIsMobile(window.innerWidth < MOBILE_BREAKPOINT)
    const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
    mql.addEventListener("change", update)
    update()
    return () => mql.removeEventListener("change", update)
  }, [])

  return isMobile
}
