All checks were successful
Automated Container Build / build-and-push (push) Successful in 3m25s
21 lines
638 B
TypeScript
21 lines
638 B
TypeScript
import { useState, useEffect } from 'react';
|
|
|
|
const MOBILE_BREAKPOINT = 768;
|
|
|
|
export function useIsMobile(breakpoint = 768) {
|
|
const [isMobile, setIsMobile] = useState<boolean>(false);
|
|
const [hasMounted, setHasMounted] = useState(false);
|
|
|
|
useEffect(() => {
|
|
setHasMounted(true);
|
|
const mql = window.matchMedia(`(max-width: ${breakpoint}px)`);
|
|
setIsMobile(mql.matches);
|
|
|
|
const handler = (e: MediaQueryListEvent) => setIsMobile(e.matches);
|
|
mql.addEventListener('change', handler);
|
|
return () => mql.removeEventListener('change', handler);
|
|
}, [breakpoint]);
|
|
|
|
if (!hasMounted) return false;
|
|
return isMobile;
|
|
}
|