( settings: MediaQuerySettings, device?: MediaQueryMatchers, onChange?: (_: boolean) => void )
| 110 | } |
| 111 | |
| 112 | const useMediaQuery = ( |
| 113 | settings: MediaQuerySettings, |
| 114 | device?: MediaQueryMatchers, |
| 115 | onChange?: (_: boolean) => void |
| 116 | ) => { |
| 117 | const deviceSettings = useDevice(device) |
| 118 | const query = useQuery(settings) |
| 119 | if (!query) throw new Error('Invalid or missing MediaQuery!') |
| 120 | const mq = useMatchMedia(query, deviceSettings) |
| 121 | const matches = useMatches(mq as unknown as MediaQueryList) |
| 122 | const isUpdate = useIsUpdate() |
| 123 | |
| 124 | useEffect(() => { |
| 125 | if (isUpdate && onChange) { |
| 126 | onChange(matches) |
| 127 | } |
| 128 | }, [matches]) |
| 129 | |
| 130 | useEffect( |
| 131 | () => () => { |
| 132 | if (mq) { |
| 133 | mq.dispose() |
| 134 | } |
| 135 | }, |
| 136 | [] |
| 137 | ) |
| 138 | |
| 139 | return matches |
| 140 | } |
| 141 | |
| 142 | export default useMediaQuery |
no test coverage detected
searching dependent graphs…