Files
searcher/hooks/use-mobile.ts
T
2026-06-06 00:49:07 +08:00

25 lines
693 B
TypeScript

import * as React from "react";
const MOBILE_BREAKPOINT = 768;
export function useIsMobile() {
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(() =>
typeof window === "undefined"
? undefined
: window.innerWidth < MOBILE_BREAKPOINT,
);
React.useEffect(() => {
const mql = window.matchMedia(
`(max-width: ${MOBILE_BREAKPOINT - 1}px)`,
);
const onChange = () => {
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
};
mql.addEventListener("change", onChange);
return () => mql.removeEventListener("change", onChange);
}, []);
return !!isMobile;
}