fourcal-pms/frontend/src/components/common/BaseDialog.vue

117 lines
3.5 KiB
Vue

<template>
<Teleport to="body">
<Transition name="dialog">
<div v-if="modelValue" class="fixed inset-0 z-[900] flex items-start justify-center overflow-y-auto p-4 sm:p-6">
<div class="fixed inset-0 bg-gray-900/45 backdrop-blur-[2px]" @click="tryClose"></div>
<div
:class="['relative my-8 w-full rounded-2xl bg-white shadow-xl animate-scale-in dark:bg-dark-800', widthClass]"
role="dialog"
aria-modal="true"
>
<div class="flex items-start justify-between gap-4 border-b border-gray-100 px-5 py-3.5 dark:border-dark-700">
<div class="min-w-0">
<h3 class="text-sm font-semibold text-gray-900 dark:text-gray-100">{{ title }}</h3>
<p v-if="description" class="mt-0.5 text-xs text-gray-500 dark:text-dark-400">{{ description }}</p>
</div>
<button class="btn btn-ghost btn-icon -mr-1 -mt-1 shrink-0" @click="tryClose">
<Icon name="x" size="sm" />
</button>
</div>
<div :class="[bodyScroll ? 'max-h-[65vh] overflow-y-auto px-5 py-4' : 'px-5 py-4']">
<slot />
</div>
<div class="flex items-center justify-end gap-2 border-t border-gray-100 px-5 py-3 dark:border-dark-700">
<slot name="footer">
<button class="btn btn-secondary btn-sm" @click="close">{{ cancelText }}</button>
<button
class="btn btn-sm"
:class="confirmTone === 'danger' ? 'btn-danger' : 'btn-primary'"
:disabled="confirming || confirmDisabled"
@click="emit('confirm')"
>
<LoadingSpinner v-if="confirming" size="sm" />
{{ confirmText }}
</button>
</slot>
</div>
</div>
</div>
</Transition>
</Teleport>
</template>
<script setup lang="ts">
import { onBeforeUnmount, watch } from 'vue'
import Icon from '@/components/icons/Icon.vue'
import LoadingSpinner from './LoadingSpinner.vue'
const props = withDefaults(
defineProps<{
modelValue: boolean
title: string
description?: string
confirmText?: string
cancelText?: string
confirmTone?: 'primary' | 'danger'
confirming?: boolean
confirmDisabled?: boolean
bodyScroll?: boolean
width?: 'sm' | 'md' | 'lg' | 'xl'
closeOnBackdrop?: boolean
}>(),
{
confirmText: '确定',
cancelText: '取消',
confirmTone: 'primary',
confirming: false,
confirmDisabled: false,
bodyScroll: true,
width: 'md',
closeOnBackdrop: true
}
)
const emit = defineEmits<{ (e: 'update:modelValue', v: boolean): void; (e: 'confirm'): void; (e: 'cancel'): void }>()
const WIDTHS: Record<string, string> = {
sm: 'max-w-md',
md: 'max-w-xl',
lg: 'max-w-3xl',
xl: 'max-w-5xl'
}
const widthClass = WIDTHS[props.width] ?? WIDTHS.md
function close() {
emit('update:modelValue', false)
emit('cancel')
}
function tryClose() {
if (props.closeOnBackdrop && !props.confirming) close()
}
function onKey(event: KeyboardEvent) {
if (event.key === 'Escape' && props.modelValue && !props.confirming) close()
}
watch(
() => props.modelValue,
(open) => {
if (open) document.addEventListener('keydown', onKey)
else document.removeEventListener('keydown', onKey)
}
)
onBeforeUnmount(() => document.removeEventListener('keydown', onKey))
</script>
<style scoped>
.dialog-enter-active,
.dialog-leave-active {
transition: opacity 0.18s ease;
}
.dialog-enter-from,
.dialog-leave-to {
opacity: 0;
}
</style>