Browse Source

misc: better comments on useMediaQuery

Signed-off-by: Muthu Kumar <muthukumar@thefeathers.in>
pull/1/head
Muthu Kumar 3 years ago
parent
commit
0e7555dd01
Signed by: mkrhere GPG Key ID: 3FD688398897097E
  1. 12
      src/util/useMediaQuery.ts

12
src/util/useMediaQuery.ts

@ -13,16 +13,17 @@ import { useEffect, useState } from "react";
*
*/
const useMediaQuery = (mediaQuery: string) => {
const [isVerified, setIsVerified] = useState(!!window.matchMedia(mediaQuery).matches);
const [isMatch, setIsMatch] = useState(!!window.matchMedia(mediaQuery).matches);
useEffect(() => {
const mediaQueryList = window.matchMedia(mediaQuery);
const documentChangeHandler = () => setIsVerified(!!mediaQueryList.matches);
const documentChangeHandler = () => setIsMatch(!!mediaQueryList.matches);
try {
mediaQueryList.addEventListener("change", documentChangeHandler);
} catch (e) {
//Safari isn't supporting mediaQueryList.addEventListener
// Safari isn't supporting mediaQueryList.addEventListener,
// fallback to deprecated addListener
console.error(e);
mediaQueryList.addListener(documentChangeHandler);
}
@ -33,14 +34,15 @@ const useMediaQuery = (mediaQuery: string) => {
try {
mediaQueryList.removeEventListener("change", documentChangeHandler);
} catch (e) {
//Safari isn't supporting mediaQueryList.removeEventListener
// Safari isn't supporting mediaQueryList.removeEventListener,
// fallback to deprecated removeListener
console.error(e);
mediaQueryList.removeListener(documentChangeHandler);
}
};
}, [mediaQuery]);
return isVerified;
return isMatch;
};
export default useMediaQuery;

Loading…
Cancel
Save