ai: hooks: Refactor isMobile logic.
Browse files- hooks/use-mobile.ts +58 -9
hooks/use-mobile.ts
CHANGED
|
@@ -1,21 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import * as React from "react";
|
| 2 |
|
| 3 |
const MOBILE_BREAKPOINT = 768;
|
| 4 |
|
| 5 |
-
export function useIsMobile() {
|
| 6 |
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(
|
| 7 |
undefined,
|
| 8 |
);
|
| 9 |
|
| 10 |
React.useEffect(() => {
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
};
|
| 15 |
-
|
| 16 |
-
setIsMobile(window.innerWidth < MOBILE_BREAKPOINT);
|
| 17 |
-
return () => mql.removeEventListener("change", onChange);
|
| 18 |
-
}, []);
|
| 19 |
|
| 20 |
-
return
|
| 21 |
}
|
|
|
|
| 1 |
+
//
|
| 2 |
+
// SPDX-FileCopyrightText: Hadad <hadad@linuxmail.org>
|
| 3 |
+
// SPDX-License-Identifier: MIT
|
| 4 |
+
//
|
| 5 |
+
|
| 6 |
import * as React from "react";
|
| 7 |
|
| 8 |
const MOBILE_BREAKPOINT = 768;
|
| 9 |
|
| 10 |
+
export function useIsMobile(breakpoint: number = MOBILE_BREAKPOINT) {
|
| 11 |
const [isMobile, setIsMobile] = React.useState<boolean | undefined>(
|
| 12 |
undefined,
|
| 13 |
);
|
| 14 |
|
| 15 |
React.useEffect(() => {
|
| 16 |
+
if (typeof window === "undefined") {
|
| 17 |
+
return;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
const mediaQuery = `(max-width: ${breakpoint - 1}px)`;
|
| 21 |
+
|
| 22 |
+
const getIsMobile = () => window.innerWidth < breakpoint;
|
| 23 |
+
|
| 24 |
+
const updateIsMobile = () => {
|
| 25 |
+
setIsMobile(getIsMobile());
|
| 26 |
+
};
|
| 27 |
+
|
| 28 |
+
let mql: MediaQueryList | null = null;
|
| 29 |
+
let rafId: number | null = null;
|
| 30 |
+
|
| 31 |
+
mql = window.matchMedia(mediaQuery);
|
| 32 |
+
|
| 33 |
+
const debouncedUpdate = () => {
|
| 34 |
+
if (rafId !== null) {
|
| 35 |
+
cancelAnimationFrame(rafId);
|
| 36 |
+
}
|
| 37 |
+
rafId = requestAnimationFrame(updateIsMobile);
|
| 38 |
+
};
|
| 39 |
+
|
| 40 |
+
if (mql.addEventListener) {
|
| 41 |
+
mql.addEventListener("change", debouncedUpdate);
|
| 42 |
+
} else if (mql.addListener) {
|
| 43 |
+
mql.addListener(debouncedUpdate);
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
window.addEventListener("resize", debouncedUpdate);
|
| 47 |
+
window.addEventListener("orientationchange", debouncedUpdate);
|
| 48 |
+
|
| 49 |
+
updateIsMobile();
|
| 50 |
+
|
| 51 |
+
return () => {
|
| 52 |
+
if (rafId !== null) {
|
| 53 |
+
cancelAnimationFrame(rafId);
|
| 54 |
+
}
|
| 55 |
+
|
| 56 |
+
if (mql) {
|
| 57 |
+
if (mql.removeEventListener) {
|
| 58 |
+
mql.removeEventListener("change", debouncedUpdate);
|
| 59 |
+
} else if (mql.removeListener) {
|
| 60 |
+
mql.removeListener(debouncedUpdate);
|
| 61 |
+
}
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
window.removeEventListener("resize", debouncedUpdate);
|
| 65 |
+
window.removeEventListener("orientationchange", debouncedUpdate);
|
| 66 |
};
|
| 67 |
+
}, [breakpoint]);
|
|
|
|
|
|
|
|
|
|
| 68 |
|
| 69 |
+
return typeof isMobile === "boolean" ? isMobile : false;
|
| 70 |
}
|