Files
Termix/src/hooks/use-mobile.ts
T

22 lines
588 B
TypeScript
Raw Normal View History

2025-09-12 14:42:00 -05:00
import * as React from "react";
2025-08-07 02:20:27 -05:00
2025-09-12 14:42:00 -05:00
const MOBILE_BREAKPOINT = 768;
2025-08-07 02:20:27 -05:00
export function useIsMobile() {
2026-05-06 15:12:07 -05:00
const [isMobile, setIsMobile] = React.useState<boolean>(
() =>
typeof window !== "undefined" && window.innerWidth < MOBILE_BREAKPOINT,
2025-09-12 14:42:00 -05:00
);
2025-08-07 02:20:27 -05:00
React.useEffect(() => {
2025-09-12 14:42:00 -05:00
const mql = window.matchMedia(`(max-width: ${MOBILE_BREAKPOINT - 1}px)`);
2025-08-07 02:20:27 -05:00
const onChange = () => {
2025-09-12 14:42:00 -05:00
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
};
mql.addEventListener("change", onChange);
return () => mql.removeEventListener("change", onChange);
}, []);
2025-08-07 02:20:27 -05:00
2026-05-06 15:12:07 -05:00
return isMobile;
2025-08-07 02:20:27 -05:00
}