MCPcopy Index your code
hub / github.com/adobe/react-spectrum / useMediaQuery

Function useMediaQuery

packages/@react-spectrum/s2/src/useMediaQuery.ts:16–42  ·  view source on GitHub ↗
(query: string)

Source from the content-addressed store, hash-verified

14import {useIsSSR} from 'react-aria/SSRProvider';
15
16export function useMediaQuery(query: string): boolean {
17 let supportsMatchMedia = typeof window !== 'undefined' && typeof window.matchMedia === 'function';
18 let [matches, setMatches] = useState(() =>
19 supportsMatchMedia ? window.matchMedia(query).matches : false
20 );
21
22 useEffect(() => {
23 if (!supportsMatchMedia) {
24 return;
25 }
26
27 let mq = window.matchMedia(query);
28 let onChange = evt => {
29 setMatches(evt.matches);
30 };
31
32 mq.addListener(onChange);
33 return () => {
34 mq.removeListener(onChange);
35 };
36 }, [supportsMatchMedia, query]);
37
38 // If in SSR, the media query should never match. Once the page hydrates,
39 // this will update and the real value will be returned.
40 let isSSR = useIsSSR();
41 return isSSR ? false : matches;
42}

Callers 5

ToastContainerFunction · 0.90
useIsMobileDeviceFunction · 0.90
useScaleFunction · 0.90
EditableCellInnerFunction · 0.90
useLoadingAnimationFunction · 0.90

Calls 1

useIsSSRFunction · 0.90

Tested by

no test coverage detected