|
| 1 | +<template> |
| 2 | + <Teleport to="body"> |
| 3 | + <div v-if="visible" class="fixed top-4 right-4 z-50 max-w-sm"> |
| 4 | + <Transition |
| 5 | + enter-active-class="transition-all duration-300 ease-out transform" |
| 6 | + enter-from-class="translate-x-full opacity-0" |
| 7 | + enter-to-class="translate-x-0 opacity-100" |
| 8 | + leave-active-class="transition-all duration-200 ease-in transform" |
| 9 | + leave-from-class="translate-x-0 opacity-100" |
| 10 | + leave-to-class="translate-x-full opacity-0" |
| 11 | + > |
| 12 | + <div |
| 13 | + v-if="visible" |
| 14 | + :class="[ |
| 15 | + 'px-4 py-3 rounded-lg shadow-lg backdrop-blur-sm border', |
| 16 | + typeClasses[type], |
| 17 | + ]" |
| 18 | + > |
| 19 | + <div class="flex items-center space-x-2"> |
| 20 | + <div class="flex-shrink-0"> |
| 21 | + <component :is="iconComponent" class="w-5 h-5" /> |
| 22 | + </div> |
| 23 | + <div class="flex-1"> |
| 24 | + <p class="text-sm font-medium">{{ message }}</p> |
| 25 | + </div> |
| 26 | + <button |
| 27 | + v-if="closable" |
| 28 | + @click="close" |
| 29 | + class="flex-shrink-0 ml-2 text-current opacity-60 hover:opacity-100 transition-opacity" |
| 30 | + > |
| 31 | + <svg |
| 32 | + class="w-4 h-4" |
| 33 | + fill="none" |
| 34 | + stroke="currentColor" |
| 35 | + viewBox="0 0 24 24" |
| 36 | + > |
| 37 | + <path |
| 38 | + stroke-linecap="round" |
| 39 | + stroke-linejoin="round" |
| 40 | + stroke-width="2" |
| 41 | + d="M6 18L18 6M6 6l12 12" |
| 42 | + ></path> |
| 43 | + </svg> |
| 44 | + </button> |
| 45 | + </div> |
| 46 | + </div> |
| 47 | + </Transition> |
| 48 | + </div> |
| 49 | + </Teleport> |
| 50 | +</template> |
| 51 | + |
| 52 | +<script setup lang="ts"> |
| 53 | +import { ref, computed, onMounted } from "vue"; |
| 54 | +
|
| 55 | +export interface ToastProps { |
| 56 | + message: string; |
| 57 | + type?: "success" | "error" | "warning" | "info"; |
| 58 | + duration?: number; |
| 59 | + closable?: boolean; |
| 60 | +} |
| 61 | +
|
| 62 | +const props = withDefaults(defineProps<ToastProps>(), { |
| 63 | + type: "success", |
| 64 | + duration: 3000, |
| 65 | + closable: true, |
| 66 | +}); |
| 67 | +
|
| 68 | +const emit = defineEmits<{ |
| 69 | + close: []; |
| 70 | +}>(); |
| 71 | +
|
| 72 | +const visible = ref(false); |
| 73 | +let timer: NodeJS.Timeout | null = null; |
| 74 | +
|
| 75 | +const typeClasses = { |
| 76 | + success: |
| 77 | + "bg-green-50/95 dark:bg-green-900/95 text-green-800 dark:text-green-200 border-green-200 dark:border-green-700", |
| 78 | + error: |
| 79 | + "bg-red-50/95 dark:bg-red-900/95 text-red-800 dark:text-red-200 border-red-200 dark:border-red-700", |
| 80 | + warning: |
| 81 | + "bg-yellow-50/95 dark:bg-yellow-900/95 text-yellow-800 dark:text-yellow-200 border-yellow-200 dark:border-yellow-700", |
| 82 | + info: "bg-blue-50/95 dark:bg-blue-900/95 text-blue-800 dark:text-blue-200 border-blue-200 dark:border-blue-700", |
| 83 | +}; |
| 84 | +
|
| 85 | +const iconComponent = computed(() => { |
| 86 | + switch (props.type) { |
| 87 | + case "success": |
| 88 | + return { |
| 89 | + template: `<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 90 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"></path> |
| 91 | + </svg>`, |
| 92 | + }; |
| 93 | + case "error": |
| 94 | + return { |
| 95 | + template: `<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 96 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path> |
| 97 | + </svg>`, |
| 98 | + }; |
| 99 | + case "warning": |
| 100 | + return { |
| 101 | + template: `<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 102 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 9v2m0 4h.01m-6.938 4h13.856c1.54 0 2.502-1.667 1.732-2.5L13.732 4c-.77-.833-1.732-.833-2.5 0L4.268 18.5c-.77.833.192 2.5 1.732 2.5z"></path> |
| 103 | + </svg>`, |
| 104 | + }; |
| 105 | + case "info": |
| 106 | + return { |
| 107 | + template: `<svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"> |
| 108 | + <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 16h-1v-4h-1m1-4h.01M21 12a9 9 0 11-18 0 9 9 0 0118 0z"></path> |
| 109 | + </svg>`, |
| 110 | + }; |
| 111 | + default: |
| 112 | + return null; |
| 113 | + } |
| 114 | +}); |
| 115 | +
|
| 116 | +const show = () => { |
| 117 | + visible.value = true; |
| 118 | +
|
| 119 | + if (props.duration > 0) { |
| 120 | + timer = setTimeout(() => { |
| 121 | + close(); |
| 122 | + }, props.duration); |
| 123 | + } |
| 124 | +}; |
| 125 | +
|
| 126 | +const close = () => { |
| 127 | + visible.value = false; |
| 128 | +
|
| 129 | + if (timer) { |
| 130 | + clearTimeout(timer); |
| 131 | + timer = null; |
| 132 | + } |
| 133 | +
|
| 134 | + setTimeout(() => { |
| 135 | + emit("close"); |
| 136 | + }, 200); |
| 137 | +}; |
| 138 | +
|
| 139 | +onMounted(() => { |
| 140 | + show(); |
| 141 | +}); |
| 142 | +
|
| 143 | +defineExpose({ |
| 144 | + show, |
| 145 | + close, |
| 146 | +}); |
| 147 | +</script> |
0 commit comments