Skip to content

armgit / ui

Public Imported from GitHub
0
TypeScript
458 B
Raw Blame
import * as React from "react"

const MOBILE_QUERY = "(max-width: 767px)"

function subscribeToMobileQuery(listener: () => void) {
  const query = window.matchMedia(MOBILE_QUERY)
  query.addEventListener("change", listener)
  return () => query.removeEventListener("change", listener)
}

export function useIsMobile() {
  return React.useSyncExternalStore(
    subscribeToMobileQuery,
    () => window.matchMedia(MOBILE_QUERY).matches,
    () => false
  )
}