18 lines
568 B
TypeScript
18 lines
568 B
TypeScript
import { useEffect, useState } from 'react';
|
|
|
|
export const useIsMobileViewport = (breakpoint = 640) => {
|
|
const [isMobileViewport, setIsMobileViewport] = useState(false);
|
|
|
|
useEffect(() => {
|
|
if (typeof window === 'undefined') return;
|
|
|
|
const mediaQuery = window.matchMedia(`(max-width: ${breakpoint - 1}px)`);
|
|
const update = () => setIsMobileViewport(mediaQuery.matches);
|
|
update();
|
|
mediaQuery.addEventListener('change', update);
|
|
return () => mediaQuery.removeEventListener('change', update);
|
|
}, [breakpoint]);
|
|
|
|
return isMobileViewport;
|
|
};
|