since3-portal/hooks/use-mobile.ts
2026-09-27 16:06:22 +02:00

21 lines
523 B
TypeScript

import * as React from "react"
const MOBILE_BREAKPOINT = 768
function subscribe(callback: () => void) {
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`)
mql.addEventListener("change", callback)
return () => mql.removeEventListener("change", callback)
}
function getSnapshot() {
return window.innerWidth < MOBILE_BREAKPOINT
}
function getServerSnapshot() {
return false
}
export function useIsMobile() {
return React.useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot)
}