Last active
February 5, 2021 08:52
-
-
Save aliakakis/7407a2b9cd0327c73aec682f96c28ac5 to your computer and use it in GitHub Desktop.
Vue based
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
}); | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- | |
<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