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

Loading…
Cancel
Save