mirror of
https://github.com/outline/outline.git
synced 2026-01-04 18:21:23 -06:00
* Add JSDoc to hooks in app/hooks directory * lint --------- Co-authored-by: codegen-sh[bot] <131295404+codegen-sh[bot]@users.noreply.github.com> Co-authored-by: Tom Moor <tom.moor@gmail.com>
30 lines
772 B
TypeScript
30 lines
772 B
TypeScript
import { useState, useEffect } from "react";
|
|
|
|
/**
|
|
* Hook to check if a media query matches the current viewport.
|
|
*
|
|
* @param query The CSS media query to check against
|
|
* @returns boolean indicating whether the media query matches
|
|
*/
|
|
export default function useMediaQuery(query: string): boolean {
|
|
const [matches, setMatches] = useState<boolean>(false);
|
|
|
|
useEffect(() => {
|
|
if (window.matchMedia) {
|
|
const media = window.matchMedia(query);
|
|
if (media.matches !== matches) {
|
|
setMatches(media.matches);
|
|
}
|
|
const listener = () => {
|
|
setMatches(media.matches);
|
|
};
|
|
media.addListener(listener);
|
|
return () => media.removeListener(listener);
|
|
}
|
|
|
|
return undefined;
|
|
}, [matches, query]);
|
|
|
|
return matches;
|
|
}
|