hilingriviw/src/utils/useIsMobile.ts
2026-04-18 11:52:45 +03:00

13 lines
484 B
TypeScript

import { useState, useEffect } from 'preact/hooks';
export function useIsMobile(breakpoint = 768) {
const [isMobile, setIsMobile] = useState(window.innerWidth < breakpoint);
useEffect(() => {
const mq = window.matchMedia(`(max-width: ${breakpoint - 1}px)`);
const handler = (e: MediaQueryListEvent) => setIsMobile(e.matches);
mq.addEventListener('change', handler);
return () => mq.removeEventListener('change', handler);
}, [breakpoint]);
return isMobile;
}