209 lines
5.2 KiB
Vue
209 lines
5.2 KiB
Vue
<script setup lang="ts">
|
|
import { computed, onBeforeUnmount, watch } from 'vue';
|
|
import { Check, Info, AlertTriangle, XCircle, X } from 'lucide-vue-next';
|
|
|
|
type MessageLevel = 'success' | 'info' | 'warning' | 'error';
|
|
|
|
interface Props {
|
|
// 父组件通过 v-model 控制弹框显隐。
|
|
modelValue: boolean;
|
|
// 消息级别决定配色和图标。
|
|
level?: MessageLevel;
|
|
title?: string;
|
|
message?: string;
|
|
closeOnMask?: boolean;
|
|
closeOnEsc?: boolean;
|
|
// 自动关闭时长(毫秒):<= 0 表示不自动关闭。
|
|
autoCloseMs?: number;
|
|
}
|
|
|
|
const props = withDefaults(defineProps<Props>(), {
|
|
level: 'info',
|
|
title: '',
|
|
message: '',
|
|
closeOnMask: true,
|
|
closeOnEsc: true,
|
|
autoCloseMs: 3000,
|
|
});
|
|
|
|
const emit = defineEmits<{
|
|
(e: 'update:modelValue', value: boolean): void;
|
|
(e: 'close'): void;
|
|
}>();
|
|
|
|
const STYLE_MAP: Record<MessageLevel, { panel: string; iconWrap: string; icon: string }> = {
|
|
success: {
|
|
panel: 'bg-[#eef8e8] border-[#b7e2a7]',
|
|
iconWrap: 'border-[#52c41a]',
|
|
icon: 'text-[#52c41a]',
|
|
},
|
|
info: {
|
|
panel: 'bg-[#eaf4ff] border-[#9fcbff]',
|
|
iconWrap: 'border-[#1677ff]',
|
|
icon: 'text-[#1677ff]',
|
|
},
|
|
warning: {
|
|
panel: 'bg-[#fff8e8] border-[#ffd88a]',
|
|
iconWrap: 'border-[#faad14]',
|
|
icon: 'text-[#faad14]',
|
|
},
|
|
error: {
|
|
panel: 'bg-[#fff1f0] border-[#ffb3b0]',
|
|
iconWrap: 'border-[#ff4d4f]',
|
|
icon: 'text-[#ff4d4f]',
|
|
},
|
|
};
|
|
|
|
const ICON_MAP: Record<MessageLevel, any> = {
|
|
success: Check,
|
|
info: Info,
|
|
warning: AlertTriangle,
|
|
error: XCircle,
|
|
};
|
|
|
|
const DEFAULT_TITLE_MAP: Record<MessageLevel, string> = {
|
|
success: 'Success',
|
|
info: 'Info',
|
|
warning: 'Warning',
|
|
error: 'Error',
|
|
};
|
|
|
|
const resolvedStyle = computed(() => STYLE_MAP[props.level]);
|
|
const resolvedIcon = computed(() => ICON_MAP[props.level]);
|
|
const resolvedTitle = computed(() => props.title || DEFAULT_TITLE_MAP[props.level]);
|
|
const resolvedMessage = computed(() => props.message || '');
|
|
let autoCloseTimer: ReturnType<typeof setTimeout> | null = null;
|
|
|
|
const clearAutoCloseTimer = () => {
|
|
if (!autoCloseTimer) return;
|
|
clearTimeout(autoCloseTimer);
|
|
autoCloseTimer = null;
|
|
};
|
|
|
|
const closeDialog = () => {
|
|
clearAutoCloseTimer();
|
|
emit('update:modelValue', false);
|
|
emit('close');
|
|
};
|
|
|
|
const handleMaskClose = () => {
|
|
if (!props.closeOnMask) return;
|
|
closeDialog();
|
|
};
|
|
|
|
const handleEscKey = (event: KeyboardEvent) => {
|
|
if (!props.closeOnEsc) return;
|
|
if (event.key === 'Escape' && props.modelValue) {
|
|
closeDialog();
|
|
}
|
|
};
|
|
|
|
watch(
|
|
() => [props.modelValue, props.level, props.title, props.message, props.autoCloseMs],
|
|
([visible]) => {
|
|
clearAutoCloseTimer();
|
|
|
|
if (!visible) {
|
|
document.removeEventListener('keydown', handleEscKey);
|
|
return;
|
|
}
|
|
document.addEventListener('keydown', handleEscKey);
|
|
|
|
// 关键点:弹框可见时只要内容变化,也会重新计时自动关闭。
|
|
if (props.autoCloseMs > 0) {
|
|
autoCloseTimer = setTimeout(() => {
|
|
closeDialog();
|
|
}, props.autoCloseMs);
|
|
}
|
|
},
|
|
{ immediate: true }
|
|
);
|
|
|
|
onBeforeUnmount(() => {
|
|
clearAutoCloseTimer();
|
|
document.removeEventListener('keydown', handleEscKey);
|
|
});
|
|
</script>
|
|
|
|
<template>
|
|
<Transition name="level-dialog-fade">
|
|
<div
|
|
v-if="modelValue"
|
|
class="fixed inset-0 z-[110] bg-black/40 flex items-center justify-center p-4"
|
|
@click.self="handleMaskClose"
|
|
>
|
|
<div
|
|
class="level-dialog-panel w-full max-w-[430px] rounded-2xl border shadow-[0_14px_28px_rgba(0,0,0,0.14)] px-9 py-8 relative"
|
|
:class="resolvedStyle.panel"
|
|
>
|
|
<button
|
|
type="button"
|
|
class="absolute top-4 right-4 w-8 h-8 inline-flex items-center justify-center rounded-full text-slate-500 hover:text-slate-700 hover:bg-black/5 transition-colors"
|
|
@click="closeDialog"
|
|
aria-label="close dialog"
|
|
>
|
|
<X class="w-4 h-4" />
|
|
</button>
|
|
|
|
<div class="flex items-start gap-4 pr-8 min-h-0">
|
|
<span
|
|
class="w-8 h-8 rounded-full border-2 inline-flex items-center justify-center shrink-0 mt-0.5"
|
|
:class="resolvedStyle.iconWrap"
|
|
>
|
|
<component :is="resolvedIcon" class="w-4 h-4" :class="resolvedStyle.icon" />
|
|
</span>
|
|
|
|
<div class="min-w-0 w-full">
|
|
<h3 class="text-[16px] leading-6 font-semibold text-slate-800">
|
|
{{ resolvedTitle }}
|
|
</h3>
|
|
<p class="level-dialog-message mt-3 text-[14px] leading-8 text-slate-700 whitespace-pre-line break-words">
|
|
{{ resolvedMessage }}
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</Transition>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.level-dialog-fade-enter-active,
|
|
.level-dialog-fade-leave-active {
|
|
transition: opacity 0.2s ease;
|
|
}
|
|
|
|
.level-dialog-fade-enter-from,
|
|
.level-dialog-fade-leave-to {
|
|
opacity: 0;
|
|
}
|
|
|
|
.level-dialog-panel {
|
|
max-height: min(80vh, 640px);
|
|
display: flex;
|
|
flex-direction: column;
|
|
}
|
|
|
|
.level-dialog-message {
|
|
max-height: min(48vh, 320px);
|
|
overflow: auto;
|
|
padding-right: 0.25rem;
|
|
}
|
|
|
|
@media (max-width: 768px) {
|
|
.level-dialog-panel {
|
|
max-width: 94vw;
|
|
max-height: 82vh;
|
|
padding: 20px 18px;
|
|
}
|
|
|
|
.level-dialog-panel p {
|
|
line-height: 1.75;
|
|
}
|
|
|
|
.level-dialog-message {
|
|
max-height: 52vh;
|
|
}
|
|
}
|
|
</style>
|