Files
outline/app/hooks/useMediaQuery.ts
Tom MoorandClaude Opus 4.8 0a90968d97 Adopt React 19 hooks for browser-state and client-side filters (#13844)
* refactor: Adopt React 19 browser-state and filter hooks

Rewrite browser-state hooks with useSyncExternalStore, adopt
useEffectEvent for latest-ref patterns, and defer client-side
filters with useDeferredValue.

* fix: Refresh usePersistedState default when key changes

* fix: Normalize serialized cache key for undefined values

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>

* iterate

---------

Co-authored-by: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
2026-09-23 19:11:29 -04:00

35 lines
1.0 KiB
TypeScript

import { useCallback, useSyncExternalStore } from "react";
import { isBrowser } from "@shared/utils/browser";
const canMatch = (): boolean =>
isBrowser && typeof window.matchMedia === "function";
const getServerSnapshot = (): boolean => false;
/**
* 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 subscribe = useCallback(
(onStoreChange: () => void) => {
if (!canMatch()) {
return () => {};
}
const media = window.matchMedia(query);
media.addEventListener("change", onStoreChange);
return () => media.removeEventListener("change", onStoreChange);
},
[query]
);
const getSnapshot = useCallback(
() => (canMatch() ? window.matchMedia(query).matches : false),
[query]
);
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
}