SurfSense/surfsense_web/hooks/use-mobile.ts

20 lines
557 B
TypeScript
Raw Normal View History

2025-07-27 10:05:37 -07:00
import * as React from "react";
2025-04-07 23:47:06 -07:00
2025-07-27 10:05:37 -07:00
const MOBILE_BREAKPOINT = 768;
2025-04-07 23:47:06 -07:00
export function useIsMobile() {
2025-07-27 10:05:37 -07:00
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(undefined);
2025-04-07 23:47:06 -07:00
2025-07-27 10:05:37 -07:00
React.useEffect(() => {
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
const onChange = () => {
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
};
mql.addEventListener("change", onChange);
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
return () => mql.removeEventListener("change", onChange);
}, []);
2025-04-07 23:47:06 -07:00
2025-07-27 10:05:37 -07:00
return !!isMobile;
2025-04-07 23:47:06 -07:00
}