chrome: some dropdown.vue improvements

This commit is contained in:
Dominik Pschenitschni 2021-10-01 20:20:07 +02:00
parent 117980a8fc
commit 66648be6d5
No known key found for this signature in database
GPG key ID: B257AC0149F43A77

View file

@ -1,14 +1,14 @@
<template>
<div class="dropdown is-right is-active" ref="dropdown">
<div class="dropdown-trigger" @click="open = !open">
<slot name="trigger">
<slot name="trigger" :close="close">
<icon :icon="triggerIcon" class="icon"/>
</slot>
</div>
<transition name="fade">
<div class="dropdown-menu" v-if="open">
<div class="dropdown-content">
<slot></slot>
<slot :close="close"></slot>
</div>
</div>
</transition>
@ -26,10 +26,10 @@ export default {
}
},
mounted() {
document.addEventListener('click', this.hide)
document.addEventListener('click', this.handleClickOutside)
},
beforeUnmount() {
document.removeEventListener('click', this.hide)
document.removeEventListener('click', this.handleClickOutside)
},
props: {
triggerIcon: {
@ -39,13 +39,20 @@ export default {
},
emits: ['close'],
methods: {
hide(e) {
if (this.open) {
closeWhenClickedOutside(e, this.$refs.dropdown, () => {
this.open = false
this.$emit('close', e)
})
close() {
this.open = false
},
toggleOpen() {
this.open = !this.open
},
handleClickOutside(e) {
if (!this.open) {
return
}
closeWhenClickedOutside(e, this.$refs.dropdown, () => {
this.open = false
this.$emit('close', e)
})
},
},
}