Subscribe to a CSS media query, SSR-safe (defaults to matched).
(query: string)
| 20 | |
| 21 | /** Subscribe to a CSS media query, SSR-safe (defaults to matched). */ |
| 22 | function useMediaQuery(query: string): boolean { |
| 23 | const [matches, setMatches] = useState(() => |
| 24 | typeof window !== 'undefined' ? window.matchMedia(query).matches : true, |
| 25 | ) |
| 26 | useEffect(() => { |
| 27 | const mq = window.matchMedia(query) |
| 28 | const handler = () => setMatches(mq.matches) |
| 29 | setMatches(mq.matches) // re-sync in case the query changed between renders |
| 30 | mq.addEventListener('change', handler) |
| 31 | return () => mq.removeEventListener('change', handler) |
| 32 | }, [query]) |
| 33 | return matches |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Three breakpoints drive the responsive shell: |
no test coverage detected