mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-24 04:51:51 +00:00
21 lines
683 B
TypeScript
21 lines
683 B
TypeScript
"use client";
|
|
|
|
import { useSyncExternalStore } from "react";
|
|
|
|
const canMatchMedia = () =>
|
|
typeof window !== "undefined" && typeof window.matchMedia === "function";
|
|
|
|
// SSR-safe media query subscription (server snapshot is always false).
|
|
export function useMediaQuery(query: string): boolean {
|
|
return useSyncExternalStore(
|
|
(onChange) => {
|
|
if (!canMatchMedia()) return () => {};
|
|
const mediaQueryList = window.matchMedia(query);
|
|
mediaQueryList.addEventListener("change", onChange);
|
|
return () => mediaQueryList.removeEventListener("change", onChange);
|
|
},
|
|
() => (canMatchMedia() ? window.matchMedia(query).matches : false),
|
|
() => false,
|
|
);
|
|
}
|