grafana/public/app/core/hooks/useMediaQueryChange.ts
Ashley Harrison 8e9ccfc66e
Navigation: Fix broken layout at 544px (#63793)
* ensure media queries transition properly

* fix unit tests
2023-03-01 15:31:32 +00:00

18 lines
519 B
TypeScript

import { useEffect } from 'react';
export function useMediaQueryChange({
breakpoint,
onChange,
}: {
breakpoint: number;
onChange: (e: MediaQueryListEvent) => void;
}) {
useEffect(() => {
const mediaQuery = window.matchMedia(`(min-width: ${breakpoint}px)`);
const onMediaQueryChange = (e: MediaQueryListEvent) => onChange(e);
mediaQuery.addEventListener('change', onMediaQueryChange);
return () => mediaQuery.removeEventListener('change', onMediaQueryChange);
}, [breakpoint, onChange]);
}