2023-01-24 18:12:37 +01:00
|
|
|
import { useEffect, useRef, useState } from "react";
|
|
|
|
|
2023-02-20 07:25:49 +01:00
|
|
|
export function useIsMobile(horizontal?: boolean) {
|
2023-01-24 18:12:37 +01:00
|
|
|
const [isMobile, setIsMobile] = useState(false);
|
|
|
|
const isMobileCurrent = useRef<boolean | null>(false);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
function onResize() {
|
2023-02-20 07:25:49 +01:00
|
|
|
const value = horizontal
|
|
|
|
? window.innerHeight < 600
|
|
|
|
: window.innerWidth < 1024;
|
2023-01-24 18:12:37 +01:00
|
|
|
const isChanged = isMobileCurrent.current !== value;
|
|
|
|
if (!isChanged) return;
|
|
|
|
|
|
|
|
isMobileCurrent.current = value;
|
|
|
|
setIsMobile(value);
|
|
|
|
}
|
|
|
|
|
|
|
|
onResize();
|
|
|
|
window.addEventListener("resize", onResize);
|
|
|
|
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("resize", onResize);
|
|
|
|
};
|
2023-02-20 07:25:49 +01:00
|
|
|
}, [horizontal]);
|
2023-01-24 18:12:37 +01:00
|
|
|
|
|
|
|
return {
|
|
|
|
isMobile,
|
|
|
|
};
|
|
|
|
}
|