@gechiui/compose
Version:
GeChiUI higher-order components (HOCs).
38 lines (32 loc) • 916 B
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = useMediaQuery;
var _element = require("@gechiui/element");
/**
* GeChiUI dependencies
*/
/**
* Runs a media query and returns its value when it changes.
*
* @param {string} [query] Media Query.
* @return {boolean} return value of the media query.
*/
function useMediaQuery(query) {
const [match, setMatch] = (0, _element.useState)(() => !!(query && typeof window !== 'undefined' && window.matchMedia(query).matches));
(0, _element.useEffect)(() => {
if (!query) {
return;
}
const updateMatch = () => setMatch(window.matchMedia(query).matches);
updateMatch();
const list = window.matchMedia(query);
list.addListener(updateMatch);
return () => {
list.removeListener(updateMatch);
};
}, [query]);
return !!query && match;
}
//# sourceMappingURL=index.js.map