JSFiddle - React, Tailwind, and code Playground
HTML
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<script src="https://unpkg.com/vue"></script>
<div id="app">
<resizing-component></resizing-component>
</div>
CSS
.red-square {
color: white;
background: red;
height: 150px;
}
JavaScript
Vue.component('resizingComponent', {
template: `<div :style="style" class="red-square">{{style}}</div>`,
data () {
return {
windowWidth: window.innerWidth
}
},
computed: {
style () {
// works but doesn't seem right :I
const windowWidth = this.windowWidth
const random = Math.round(Math.random() * (400 - 150) + 150)
return {
width: `${random}px`
}
}
},
methods: {
handleResize () {
alert('resizing');
console.log(_)
this.windowWidth = window.innerWidth
// something like this.$updateComputed('style') would be great
// this.$forceUpdate() doesn't work
}
},
mounted () {
window.addEventListener('resize', _.debounce(this.handleResize, 150));
},
beforeDestroy () {
window.removeEventListener('resize', this.handleResize)
}
})
const app = new Vue({
el: '#app'
})