pstream is dead; long live pstream taciturnaxolotl.github.io/pstream-ng/
at main 38 lines 909 B view raw
1import { useEffect, useRef, useState } from "react"; 2 3export function useIsMobile(horizontal?: boolean) { 4 const [isMobile, setIsMobile] = useState(false); 5 const isMobileCurrent = useRef<boolean | null>(false); 6 7 useEffect(() => { 8 function onResize() { 9 const value = horizontal 10 ? window.innerHeight < 600 11 : window.innerWidth < 1024; 12 const isChanged = isMobileCurrent.current !== value; 13 if (!isChanged) return; 14 15 isMobileCurrent.current = value; 16 setIsMobile(value); 17 } 18 19 onResize(); 20 window.addEventListener("resize", onResize); 21 22 return () => { 23 window.removeEventListener("resize", onResize); 24 }; 25 }, [horizontal]); 26 27 return { 28 isMobile, 29 }; 30} 31 32export function useIsPWA() { 33 return window.matchMedia("(display-mode: standalone)").matches; 34} 35 36export function useIsIOS() { 37 return /iPad|iPhone|iPod/.test(navigator.userAgent); 38}