封装消息提示框,分为success, info, error级别,并全部替换
This commit is contained in:
1 parent
5df6957e15
commit
b0fda9f081
7 files changed
+306
-75
No files matched your search
@@ -0,0 +1,170 @@
|
||||
<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;
|
||||
}
|
||||
|
||||
const props = withDefaults(defineProps<Props>(), {
|
||||
level: 'info',
|
||||
title: '',
|
||||
message: '',
|
||||
closeOnMask: true,
|
||||
closeOnEsc: true,
|
||||
});
|
||||
|
||||
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 || '');
|
||||
|
||||
const closeDialog = () => {
|
||||
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,
|
||||
(visible) => {
|
||||
if (!visible) {
|
||||
document.removeEventListener('keydown', handleEscKey);
|
||||
return;
|
||||
}
|
||||
document.addEventListener('keydown', handleEscKey);
|
||||
},
|
||||
{ immediate: true }
|
||||
);
|
||||
|
||||
onBeforeUnmount(() => {
|
||||
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">
|
||||
<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">
|
||||
<h3 class="text-[16px] leading-6 font-semibold text-slate-800">
|
||||
{{ resolvedTitle }}
|
||||
</h3>
|
||||
<p class="mt-3 text-[14px] leading-8 text-slate-700 whitespace-pre-line">
|
||||
{{ 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;
|
||||
}
|
||||
|
||||
@media (max-width: 768px) {
|
||||
.level-dialog-panel {
|
||||
max-width: 94vw;
|
||||
padding: 20px 18px;
|
||||
}
|
||||
|
||||
.level-dialog-panel p {
|
||||
line-height: 1.75;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user