(query: string)
| 14 | import {useIsSSR} from 'react-aria/SSRProvider'; |
| 15 | |
| 16 | export 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 | } |
no test coverage detected