You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
30 lines
724 B
30 lines
724 B
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]); |
|
|
|
return { |
|
isMobile, |
|
}; |
|
}
|
|
|