18 lines
513 B
TypeScript
18 lines
513 B
TypeScript
import { useSyncExternalStore } from "react"
|
|
|
|
const MOBILE_BREAKPOINT = 768
|
|
|
|
function subscribe(onChange: () => void) {
|
|
const query = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
|
|
query.addEventListener("change", onChange)
|
|
return () => query.removeEventListener("change", onChange)
|
|
}
|
|
|
|
function getSnapshot() {
|
|
return window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`).matches
|
|
}
|
|
|
|
export function useIsMobile() {
|
|
return useSyncExternalStore(subscribe, getSnapshot, () => false)
|
|
}
|