2023-06-27 12:28:09 +03:00
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
|
2023-10-02 15:42:23 +03:00
|
|
|
// Max width for mobile device: 640px
|
|
|
|
// Match small min-width media query in tailwind
|
|
|
|
const MOBILE_MAX_WIDTH = 640;
|
|
|
|
|
2023-06-27 12:28:09 +03:00
|
|
|
export const useDevice = (): { isMobile: boolean } => {
|
2023-12-06 19:24:59 +03:00
|
|
|
const [isMobile, setIsMobile] = useState(false);
|
2023-06-27 12:28:09 +03:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
const handleResize = () => {
|
|
|
|
const screenWidth = window.innerWidth;
|
2023-10-02 15:42:23 +03:00
|
|
|
setIsMobile(screenWidth < MOBILE_MAX_WIDTH);
|
2023-06-27 12:28:09 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
// Initial check
|
|
|
|
handleResize();
|
|
|
|
|
|
|
|
// Event listener for screen resize
|
|
|
|
window.addEventListener("resize", handleResize);
|
|
|
|
|
|
|
|
// Clean up event listener on component unmount
|
|
|
|
return () => {
|
|
|
|
window.removeEventListener("resize", handleResize);
|
|
|
|
};
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
return { isMobile };
|
|
|
|
};
|