|
| 1 | +<script setup lang="ts"> |
| 2 | +import { ref } from 'vue' |
| 3 | +import { onClickOutside, useElementSize } from '@vueuse/core' |
| 4 | +
|
| 5 | +const props = withDefaults(defineProps<{ |
| 6 | + modelValue?: boolean |
| 7 | + top?: HTMLElement | string |
| 8 | + left?: HTMLElement | string |
| 9 | + autoClose?: boolean |
| 10 | + transition?: 'right' | 'bottom' | 'top' |
| 11 | +}>(), { |
| 12 | + transition: 'right', |
| 13 | +}) |
| 14 | +
|
| 15 | +const emit = defineEmits<{ |
| 16 | + (e: 'close'): void |
| 17 | +}>() |
| 18 | +
|
| 19 | +const el = ref<HTMLElement>() |
| 20 | +
|
| 21 | +const { height } = useElementSize(() => props.top as HTMLElement, undefined, { box: 'border-box' }) |
| 22 | +
|
| 23 | +const width = typeof props.left === 'string' && props.left.startsWith('#') |
| 24 | + ? document.querySelector(props.left)?.getBoundingClientRect().width |
| 25 | + : useElementSize(() => props.left as HTMLElement, undefined, { box: 'border-box' }).width |
| 26 | +
|
| 27 | +onClickOutside(el, () => { |
| 28 | + if (props.modelValue && props.autoClose) |
| 29 | + emit('close') |
| 30 | +}, { |
| 31 | + ignore: ['a', 'button', 'summary', '[role="dialog"]'], |
| 32 | +}) |
| 33 | +
|
| 34 | +const transitionType = { |
| 35 | + right: { |
| 36 | + 'enter-from-class': 'transform translate-x-1/1', |
| 37 | + 'leave-to-class': 'transform translate-x-1/1', |
| 38 | + }, |
| 39 | + top: { |
| 40 | + 'enter-from-class': 'transform translate-y--1/1', |
| 41 | + 'leave-to-class': 'transform translate-y--1/1', |
| 42 | + }, |
| 43 | + bottom: { |
| 44 | + 'enter-from-class': 'transform translate-y-1/1', |
| 45 | + 'leave-to-class': 'transform translate-y-1/1', |
| 46 | + }, |
| 47 | +} |
| 48 | +</script> |
| 49 | + |
| 50 | +<template> |
| 51 | + <Transition |
| 52 | + v-bind="transitionType[transition]" |
| 53 | + enter-active-class="duration-200 ease-in" |
| 54 | + enter-to-class="opacity-100" |
| 55 | + leave-active-class="duration-200 ease-out" |
| 56 | + leave-from-class="opacity-100" |
| 57 | + > |
| 58 | + <div |
| 59 | + v-if="modelValue" |
| 60 | + ref="el" |
| 61 | + :border="`${transition === 'right' ? 'l' : transition === 'bottom' ? 't' : 'b'} base`" |
| 62 | + flex="~ col gap-1" |
| 63 | + :class="{ 'right-0': transition === 'right' || transition === 'bottom' }" |
| 64 | + absolute bottom-0 z-10 z-20 of-auto text-sm glass-effect |
| 65 | + :style="{ |
| 66 | + top: transition === 'bottom' ? 'auto' : `${height}px`, |
| 67 | + left: transition === 'right' && !width ? 'auto' : `${width}px`, |
| 68 | + }" |
| 69 | + v-bind="$attrs" |
| 70 | + > |
| 71 | + <NIconButton absolute right-2 top-2 z-20 text-xl icon="carbon-close" @click="$emit('close')" /> |
| 72 | + <div relative h-full w-full of-auto> |
| 73 | + <slot /> |
| 74 | + </div> |
| 75 | + </div> |
| 76 | + </Transition> |
| 77 | +</template> |
0 commit comments