mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-01-31 20:03:37 +00:00
Fix model selector on mobile
This commit is contained in:
50
apps/ui/src/hooks/use-media-query.ts
Normal file
50
apps/ui/src/hooks/use-media-query.ts
Normal file
@@ -0,0 +1,50 @@
|
||||
import { useState, useEffect } from 'react';
|
||||
|
||||
/**
|
||||
* Hook to detect if a media query matches
|
||||
* @param query - The media query string (e.g., '(max-width: 768px)')
|
||||
* @returns boolean indicating if the media query matches
|
||||
*/
|
||||
export function useMediaQuery(query: string): boolean {
|
||||
const [matches, setMatches] = useState(() => {
|
||||
if (typeof window === 'undefined') return false;
|
||||
return window.matchMedia(query).matches;
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (typeof window === 'undefined') return;
|
||||
|
||||
const mediaQuery = window.matchMedia(query);
|
||||
const handleChange = (e: MediaQueryListEvent) => {
|
||||
setMatches(e.matches);
|
||||
};
|
||||
|
||||
// Set initial value
|
||||
setMatches(mediaQuery.matches);
|
||||
|
||||
// Listen for changes
|
||||
mediaQuery.addEventListener('change', handleChange);
|
||||
|
||||
return () => {
|
||||
mediaQuery.removeEventListener('change', handleChange);
|
||||
};
|
||||
}, [query]);
|
||||
|
||||
return matches;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to detect if the device is mobile (screen width <= 768px)
|
||||
* @returns boolean indicating if the device is mobile
|
||||
*/
|
||||
export function useIsMobile(): boolean {
|
||||
return useMediaQuery('(max-width: 768px)');
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to detect if the device is tablet or smaller (screen width <= 1024px)
|
||||
* @returns boolean indicating if the device is tablet or smaller
|
||||
*/
|
||||
export function useIsTablet(): boolean {
|
||||
return useMediaQuery('(max-width: 1024px)');
|
||||
}
|
||||
Reference in New Issue
Block a user