pingcrm/resources/js/Shared/Dropdown.vue

64 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 />
2021-12-08 12:15:39 -03:00
<teleport v-if="show" to="#dropdown">
2019-03-18 08:53:00 -03:00
<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" />
2021-12-08 12:15:39 -03:00
<div ref="dropdown" style="position: absolute; z-index: 99999;" @click.stop="show = !autoClose">
2019-03-18 08:53:00 -03:00
<slot name="dropdown" />
</div>
</div>
2021-12-08 12:15:39 -03:00
</teleport>
2019-03-18 08:53:00 -03:00
</button>
</template>
<script>
2021-12-08 12:15:39 -03:00
import { createPopper } from '@popperjs/core'
2019-03-18 08:53:00 -03:00
export default {
props: {
placement: {
type: String,
default: 'bottom-end',
},
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(() => {
2021-12-08 12:15:39 -03:00
this.popper = createPopper(this.$el, this.$refs.dropdown, {
2019-03-18 08:53:00 -03:00
placement: this.placement,
2021-12-08 12:15:39 -03:00
modifiers: [
{
name: 'preventOverflow',
options: {
altBoundary: true,
},
},
],
2019-03-18 08:53:00 -03:00
})
})
} else if (this.popper) {
setTimeout(() => this.popper.destroy(), 100)
}
},
},
mounted() {
2021-02-27 11:00:09 -03:00
document.addEventListener('keydown', e => {
2021-12-08 12:15:39 -03:00
if (e.key === 'Escape') {
2019-12-18 17:12:22 -03:00
this.show = false
2019-03-18 08:53:00 -03:00
}
})
},
}
</script>