Skip to content

Instantly share code, notes, and snippets.

@aliakakis
Last active February 5, 2021 08:52
Show Gist options
  • Save aliakakis/7407a2b9cd0327c73aec682f96c28ac5 to your computer and use it in GitHub Desktop.
Save aliakakis/7407a2b9cd0327c73aec682f96c28ac5 to your computer and use it in GitHub Desktop.
Vue based
export default {
name: "match-media",
props: {
mediaWidth: {
type: Array,
required: true
}
},
data() {
return {
isMatch: false
};
},
mounted() {
this.handleMatchMediaOnResize();
window.addEventListener("resize", this.handleMatchMediaOnResize, false);
},
destroyed() {
window.removeEventListener("resize", this.handleMatchMediaOnResize, false);
},
methods: {
handleMatchMediaOnResize() {
this.mediaWidth.forEach(width => {
this.isMatch = window.matchMedia(width).matches;
});
}
}
};
<!--
<match-media :mediaWidth="['(min-width: 768px)', '(min-width: 1024px)']">
<events-list eventType="live"></events-list>
</match-media>
-->
<template>
<div v-if="isMatch">
<slot></slot>
</div>
</template>
<script src="./match-media.js">
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment