mirror of
https://github.com/outline/outline.git
synced 2026-11-20 21:23:06 +03:00
* 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>
35 lines
1.0 KiB
TypeScript
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);
|
|
}
|