forked from Creditra/Creditra-Frontend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuseMediaQuery.ts
More file actions
28 lines (23 loc) · 917 Bytes
/
Copy pathuseMediaQuery.ts
File metadata and controls
28 lines (23 loc) · 917 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { useEffect, useState } from "react";
/**
* Subscribes to a CSS media query and returns whether it currently matches.
* The initial value is read synchronously on first render (SSR-safe).
*/
export function useMediaQuery(query: string): boolean {
const [matches, setMatches] = useState(() => {
if (typeof window === "undefined") return false;
return window.matchMedia(query).matches;
});
useEffect(() => {
const mediaQuery = window.matchMedia(query);
const handleChange = (event: MediaQueryListEvent) => {
setMatches(event.matches);
};
setMatches(mediaQuery.matches);
mediaQuery.addEventListener("change", handleChange);
return () => mediaQuery.removeEventListener("change", handleChange);
}, [query]);
return matches;
}
/** Tailwind `md` breakpoint — sticky summary is mobile-only (below 768px). */
export const BELOW_MD_MEDIA = "(max-width: 767px)";