()
| 17 | } |
| 18 | |
| 19 | export function useMediaQuery() { |
| 20 | const [device, setDevice] = useState<"mobile" | "tablet" | "desktop" | null>( |
| 21 | getDevice(), |
| 22 | ); |
| 23 | const [dimensions, setDimensions] = useState<{ |
| 24 | width: number; |
| 25 | height: number; |
| 26 | } | null>(getDimensions()); |
| 27 | |
| 28 | useEffect(() => { |
| 29 | const checkDevice = () => { |
| 30 | setDevice(getDevice()); |
| 31 | setDimensions(getDimensions()); |
| 32 | }; |
| 33 | |
| 34 | // Initial detection |
| 35 | checkDevice(); |
| 36 | |
| 37 | // Listener for windows resize |
| 38 | window.addEventListener("resize", checkDevice); |
| 39 | |
| 40 | // Cleanup listener |
| 41 | return () => { |
| 42 | window.removeEventListener("resize", checkDevice); |
| 43 | }; |
| 44 | }, []); |
| 45 | |
| 46 | return { |
| 47 | device, |
| 48 | width: dimensions?.width, |
| 49 | height: dimensions?.height, |
| 50 | isMobile: device === "mobile", |
| 51 | isTablet: device === "tablet", |
| 52 | isDesktop: device === "desktop", |
| 53 | }; |
| 54 | } |
no test coverage detected
searching dependent graphs…