@u3u/vue-hooks
Version:
⚡️ Awesome Vue Hooks
23 lines (22 loc) • 725 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
var composition_api_1 = require("@vue/composition-api");
function useMedia(query, defaultState) {
if (defaultState === void 0) { defaultState = false; }
var mql;
var matches = composition_api_1.ref(defaultState);
var updateMatches = function () {
if (mql)
matches.value = mql.matches;
};
composition_api_1.onMounted(function () {
mql = window.matchMedia(query);
mql.addListener(updateMatches);
matches.value = mql.matches;
});
composition_api_1.onUnmounted(function () {
mql.removeListener(updateMatches);
});
return matches;
}
exports.default = useMedia;
;