mirror of
https://github.com/movie-web/movie-web.git
synced 2024-06-02 17:38:43 +02:00
38 lines
925 B
TypeScript
38 lines
925 B
TypeScript
import { useEffect, useRef, useState } from "react";
|
|
|
|
export function useIsMobile(horizontal?: boolean) {
|
|
const [isMobile, setIsMobile] = useState(false);
|
|
const isMobileCurrent = useRef<boolean | null>(false);
|
|
|
|
useEffect(() => {
|
|
function onResize() {
|
|
const value = horizontal
|
|
? window.innerHeight < 600
|
|
: window.innerWidth < 1024;
|
|
const isChanged = isMobileCurrent.current !== value;
|
|
if (!isChanged) return;
|
|
|
|
isMobileCurrent.current = value;
|
|
setIsMobile(value);
|
|
}
|
|
|
|
onResize();
|
|
window.addEventListener("resize", onResize);
|
|
|
|
return () => {
|
|
window.removeEventListener("resize", onResize);
|
|
};
|
|
}, [horizontal]);
|
|
|
|
const userAgent = window.navigator.userAgent;
|
|
const hasMobileUserAgent =
|
|
/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(
|
|
userAgent,
|
|
);
|
|
|
|
return {
|
|
isMobile,
|
|
hasMobileUserAgent,
|
|
};
|
|
}
|