JSFiddle - React, Tailwind, and code Playground
VueJS Modal Component
by tonytlwu
HTML
<!-- template for the modal component -->
<script type="x/vue-template" id="modal-template">
<div class="modal-mask" v-show="show" transition="modal">
<div class="modal-wrapper">
<div class="modal-container">
<div class="modal-header">
<slot name="header">
default header
</slot>
</div>
<div class="modal-body">
<slot name="body">
default body
</slot>
</div>
<div class="modal-footer">
<button v-if="showFooter" class="btn btn-success modal-default-button"
@click="show = false">
OK
</button>
</div>
</div>
</div>
</div>
</script>
<!-- app -->
<div id="app">
<button class="btn btn-primary" id="show-modal" @click="showAlert">Show Modal</button>
<!-- use the modal component, pass in the prop -->
<modal :show.sync="showModal" :showFooter.sync="modal.showFooter">
<h3 slot="header" v-html="modal.header"></h3>
<p slot="body" v-html="modal.body"></p>
</modal>
</div>
CSS
</style>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<style>
body {
background: transparent;
}
.modal-mask {
position: fixed;
z-index: 9998;
top: 0;
left: 0;
width: 100%;
height: 100%;
background-color: rgba(0, 0, 0, .5);
display: table;
transition: opacity .3s ease;
}
.modal-wrapper {
display: table-cell;
vertical-align: middle;
}
.modal-container {
width: 68%;
margin: 0px auto;
padding: 20px 30px;
background-color: #fff;
border-radius: 2px;
box-shadow: 0 2px 8px rgba(0, 0, 0, .33);
transition: all .3s ease;
font-family: Helvetica, Arial, sans-serif;
max-width: 600px;
}
.modal-header h3 {
margin-top: 0;
color: #42b983;
}
.modal-body {
margin: 20px 0;
}
.modal-default-button {
float: right;
}
/*
* the following styles are auto-applied to elements with
* v-transition="modal" when their visiblity is toggled
* by Vue.js.
*
* You can easily play with the modal transition by editing
* these styles.
*/
.modal-enter, .modal-leave {
opacity: 0;
}
.modal-enter .modal-container,
.modal-leave .modal-container {
-webkit-transform: scale(1.1);
transform: scale(1.1);
}
JavaScript
// register modal component
Vue.component('modal', {
template: '#modal-template',
props: {
show: {
type: Boolean,
required: true,
twoWay: true
},
showFooter: {
type: Boolean,
required: false,
default: true,
twoWay: true
}
}
})
// start app
new Vue({
el: '#app',
data: {
showModal: false,
modal: {
header: '',
body: ''
}
},
methods: {
showAlert() {
this.$set('modal', {
header: 'Porta Inceptos Fermentum Tristique',
body: 'Vestibulum id ligula porta felis euismod semper. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit. Maecenas faucibus mollis interdum.'
})
this.$set('showModal', true)
}
}
})