font-map / src /hooks /useMediaQuery.js
tfrere's picture
tfrere HF Staff
feat: mobile UX overhaul + focus mode polish + visual hover hitbox
4319071
import { useEffect, useState } from 'react';
/**
* Subscribe to a CSS media query and re-render when it flips.
* Cheap: one matchMedia listener for the lifetime of the consumer.
*/
export function useMediaQuery(query) {
const [matches, setMatches] = useState(() =>
typeof window !== 'undefined' && window.matchMedia
? window.matchMedia(query).matches
: false,
);
useEffect(() => {
if (typeof window === 'undefined' || !window.matchMedia) return;
const mql = window.matchMedia(query);
const onChange = (e) => setMatches(e.matches);
setMatches(mql.matches);
mql.addEventListener('change', onChange);
return () => mql.removeEventListener('change', onChange);
}, [query]);
return matches;
}