Last active
February 25, 2019 16:18
-
-
Save Akryum/94e25c072ed6526a11c7e117777776dc to your computer and use it in GitHub Desktop.
vue-responsive
This file contains hidden or 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
import Responsive from './utils/responsive' | |
Vue.use(Responsive, { | |
computed: { | |
mobile () { | |
return this.width <= 768 | |
}, | |
tablet () { | |
return this.width <= 900 | |
}, | |
desktop () { | |
return !this.tablet | |
}, | |
}, | |
}) |
This file contains hidden or 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
<template> | |
<div> | |
<div v-if="$responsive.mobile">Mobile</div> | |
<div v-else-if="$responsive.tablet">Tablet</div> | |
<div v-else>Desktop</div> | |
</div> | |
</template> |
This file contains hidden or 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 let responsive | |
export default { | |
install (Vue, options) { | |
const finalOptions = Object.assign({}, { | |
computed: {}, | |
}, options) | |
responsive = new Vue({ | |
data () { | |
return { | |
width: window.innerWidth, | |
height: window.innerHeight, | |
} | |
}, | |
computed: finalOptions.computed, | |
}) | |
Object.defineProperty(Vue.prototype, '$responsive', { | |
get: () => responsive, | |
}) | |
window.addEventListener('resize', () => { | |
responsive.width = window.innerWidth | |
responsive.height = window.innerHeight | |
}) | |
}, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment