Vue-match-media

Illustrates basic usage of vue-match-media plugin

by Michael Dietz

HTML

<script src="https://unpkg.com/vue-match-media"></script>
<main id="app"></main>

CSS

.mid {
  opacity: 0;
  transition: all 0.5s;
}

.mid.special {
  opacity: 1;
}

.debug {
  color: red;
}

JavaScript

const template = `
<section>
<p>Here's the basic display.</p>
<child></child>
<p class="mid" :class="{special: $mq.tablet}">A minor adjustment.</p>
<div class="debug">{{$mq.all}}</div>
</section>
`

const child = Vue.component('child', Vue.extend({
	template: '<p v-onmedia.desktop="doFunnyBusiness"></p>',
  methods: {
  	doFunnyBusiness: function(alias, matches) {
    	if (matches) this.$el.innerHTML = `Let's pretend we did something fancy here.`
      else this.$el.innerHTML = ''
    }
  }
}))

new Vue({
	el: '#app',
  template,
  conmponents: {
  	child
  },
  mq: {
  	mini: 'only screen and (max-width: 300px)',
  	phone: 'only screen and (max-width: 768px)',
  	tablet: 'only screen and (min-width: 769px) and (max-width: 1024px)',
    desktop: 'only screen and (min-width: 1025px)'
  }
})