mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
23 lines
677 B
TypeScript
23 lines
677 B
TypeScript
import { useEffect, useState } from "react";
|
|
|
|
const MOBILE_BREAKPOINT = 768;
|
|
|
|
/**
|
|
* Returns true when the viewport width is below the mobile breakpoint (768px).
|
|
*/
|
|
export function useMobile(): boolean {
|
|
const [isMobile, setIsMobile] = useState(() =>
|
|
typeof window !== "undefined" ? window.innerWidth < MOBILE_BREAKPOINT : false,
|
|
);
|
|
|
|
useEffect(() => {
|
|
const mq = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
|
|
const handler = (e: MediaQueryListEvent) => setIsMobile(e.matches);
|
|
mq.addEventListener("change", handler);
|
|
setIsMobile(mq.matches);
|
|
return () => mq.removeEventListener("change", handler);
|
|
}, []);
|
|
|
|
return isMobile;
|
|
}
|