feat: rework popup
This commit is contained in:
parent
02deb0bedd
commit
0b58973d87
@ -1,20 +1,20 @@
|
|||||||
<template>
|
<template>
|
||||||
<slot name="trigger" :isOpen="open" :toggle="toggle"></slot>
|
<slot name="trigger" :isOpen="open" :toggle="toggle"></slot>
|
||||||
<div class="popup" :class="{'is-open': open, 'has-overflow': props.hasOverflow && open}" ref="popup">
|
<div
|
||||||
|
class="popup"
|
||||||
|
:class="{
|
||||||
|
'is-open': open,
|
||||||
|
'has-overflow': props.hasOverflow && open
|
||||||
|
}"
|
||||||
|
ref="popup"
|
||||||
|
>
|
||||||
<slot name="content" :isOpen="open"/>
|
<slot name="content" :isOpen="open"/>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import {closeWhenClickedOutside} from '@/helpers/closeWhenClickedOutside'
|
import {ref} from 'vue'
|
||||||
import {onBeforeUnmount, onMounted, ref} from 'vue'
|
import {onClickOutside} from '@vueuse/core'
|
||||||
|
|
||||||
const open = ref(false)
|
|
||||||
const popup = ref(null)
|
|
||||||
|
|
||||||
const toggle = () => {
|
|
||||||
open.value = !open.value
|
|
||||||
}
|
|
||||||
|
|
||||||
const props = defineProps({
|
const props = defineProps({
|
||||||
hasOverflow: {
|
hasOverflow: {
|
||||||
@ -23,24 +23,22 @@ const props = defineProps({
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
function hidePopup(e) {
|
const open = ref(false)
|
||||||
|
const popup = ref<HTMLElement | null>(null)
|
||||||
|
|
||||||
|
function close() {
|
||||||
|
open.value = false
|
||||||
|
}
|
||||||
|
|
||||||
|
function toggle() {
|
||||||
|
open.value = !open.value
|
||||||
|
}
|
||||||
|
|
||||||
|
onClickOutside(popup, () => {
|
||||||
if (!open.value) {
|
if (!open.value) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
close()
|
||||||
// we actually want to use popup.$el, not its value.
|
|
||||||
// eslint-disable-next-line vue/no-ref-as-operand
|
|
||||||
closeWhenClickedOutside(e, popup.value, () => {
|
|
||||||
open.value = false
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
onMounted(() => {
|
|
||||||
document.addEventListener('click', hidePopup)
|
|
||||||
})
|
|
||||||
|
|
||||||
onBeforeUnmount(() => {
|
|
||||||
document.removeEventListener('click', hidePopup)
|
|
||||||
})
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user