pingcrm/resources/js/Shared/Dropdown.vue

63 lines
1.4 KiB
Vue
Raw Normal View History

2019-03-18 08:53:00 -03:00
<template>
2019-12-18 17:12:22 -03:00
<button type="button" @click="show = true">
2019-03-18 08:53:00 -03:00
<slot />
<portal v-if="show" to="dropdown">
<div>
2019-12-18 17:12:22 -03:00
<div style="position: fixed; top: 0; right: 0; left: 0; bottom: 0; z-index: 99998; background: black; opacity: .2" @click="show = false" />
<div ref="dropdown" style="position: absolute; z-index: 99999;" @click.stop="show = autoClose ? false : true">
2019-03-18 08:53:00 -03:00
<slot name="dropdown" />
</div>
</div>
</portal>
</button>
</template>
<script>
import Popper from 'popper.js'
export default {
props: {
placement: {
type: String,
default: 'bottom-end',
},
boundary: {
type: String,
default: 'scrollParent',
},
2019-12-18 17:12:22 -03:00
autoClose: {
type: Boolean,
default: true,
},
2019-03-18 08:53:00 -03:00
},
data() {
return {
show: false,
}
},
watch: {
show(show) {
if (show) {
this.$nextTick(() => {
this.popper = new Popper(this.$el, this.$refs.dropdown, {
placement: this.placement,
modifiers: {
preventOverflow: { boundariesElement: this.boundary },
},
})
})
} else if (this.popper) {
setTimeout(() => this.popper.destroy(), 100)
}
},
},
mounted() {
document.addEventListener('keydown', (e) => {
if (e.keyCode === 27) {
2019-12-18 17:12:22 -03:00
this.show = false
2019-03-18 08:53:00 -03:00
}
})
},
}
</script>