N-FIN-33: create useMediaQuery hook

This commit is contained in:
Markus Thielker 2024-03-17 00:42:01 +01:00
parent a6c9074490
commit 206ad0c528
No known key found for this signature in database

View file

@ -0,0 +1,16 @@
import { useEffect, useMemo, useState } from 'react';
export function useMediaQuery(mq: string) {
const mql = useMemo(() => matchMedia(mq), [mq]);
const [matches, setMatch] = useState(mql.matches);
useEffect(() => {
const listener = (e: any) => setMatch(e.matches);
mql.addEventListener('change', listener);
return () => mql.removeEventListener('change', listener);
}, [mq, mql]);
return matches;
}