File size: 4,506 Bytes
40f23a9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
<template>
  <Teleport to="body">
    <Transition name="toast" appear>
      <div 
        v-if="visible && message" 
        class="toast"
        :class="[type, position]"
        @click="handleClick"
      >
        <div class="toast-content">
          <!-- 图标 -->
          <div class="toast-icon" v-if="showIcon">
            <i :class="iconClass"></i>
          </div>
          
          <!-- 消息内容 -->
          <div class="toast-message">{{ message }}</div>
        </div>
      </div>
    </Transition>
  </Teleport>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted } from 'vue'

const props = defineProps({
  // 消息内容
  message: {
    type: String,
    default: ''
  },
  
  // 类型:success, error, warning, info
  type: {
    type: String,
    default: 'info',
    validator: (value) => ['success', 'error', 'warning', 'info'].includes(value)
  },
  
  // 显示时长(毫秒)
  duration: {
    type: Number,
    default: 3000
  },
  
  // 位置:top, center, bottom
  position: {
    type: String,
    default: 'top',
    validator: (value) => ['top', 'center', 'bottom'].includes(value)
  },
  
  // 是否显示图标
  showIcon: {
    type: Boolean,
    default: true
  },
  
  // 是否可点击关闭
  closable: {
    type: Boolean,
    default: true
  }
})

const emit = defineEmits(['close'])

const visible = ref(false)
let timer = null

// 图标映射
const iconClass = computed(() => {
  const iconMap = {
    success: 'fas fa-check-circle',
    error: 'fas fa-times-circle',
    warning: 'fas fa-exclamation-triangle',
    info: 'fas fa-info-circle'
  }
  
  return iconMap[props.type] || iconMap.info
})

// 处理点击事件
const handleClick = () => {
  if (props.closable) {
    close()
  }
}

// 显示Toast
const show = () => {
  visible.value = true
  
  if (props.duration > 0) {
    timer = setTimeout(() => {
      close()
    }, props.duration)
  }
}

// 关闭Toast
const close = () => {
  visible.value = false
  if (timer) {
    clearTimeout(timer)
    timer = null
  }
  emit('close')
}

// 生命周期
onMounted(() => {
  show()
})

onUnmounted(() => {
  if (timer) {
    clearTimeout(timer)
  }
})

// 暴露方法给父组件
defineExpose({
  close
})
</script>

<style scoped>
.toast {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  z-index: 10000;
  max-width: calc(100vw - 32px);
  min-width: 200px;
}

.toast.top {
  top: 20px;
}

.toast.center {
  top: 50%;
  transform: translate(-50%, -50%);
}

.toast.bottom {
  bottom: 20px;
}

.toast-content {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--bg-card);
  backdrop-filter: blur(20px);
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.1);
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.3);
}

.toast-icon {
  flex-shrink: 0;
}

.toast-icon i {
  font-size: 16px;
}

.toast-message {
  font-size: 14px;
  font-weight: 500;
  color: var(--text-primary);
  line-height: 1.4;
}

/* 类型样式 */
.toast.success .toast-icon i {
  color: #48bb78;
}

.toast.success .toast-content {
  border-color: rgba(72, 187, 120, 0.3);
}

.toast.error .toast-icon i {
  color: #e53e3e;
}

.toast.error .toast-content {
  border-color: rgba(229, 62, 62, 0.3);
}

.toast.warning .toast-icon i {
  color: #ed8936;
}

.toast.warning .toast-content {
  border-color: rgba(237, 137, 54, 0.3);
}

.toast.info .toast-icon i {
  color: #3182ce;
}

.toast.info .toast-content {
  border-color: rgba(49, 130, 206, 0.3);
}

/* 动画 */
.toast-enter-active,
.toast-leave-active {
  transition: all 0.3s ease-out;
}

.toast-enter-from {
  opacity: 0;
  transform: translateX(-50%) translateY(-20px);
}

.toast.center.toast-enter-from {
  transform: translate(-50%, -50%) scale(0.9);
}

.toast.bottom.toast-enter-from {
  transform: translateX(-50%) translateY(20px);
}

.toast-leave-to {
  opacity: 0;
  transform: translateX(-50%) translateY(-20px);
}

.toast.center.toast-leave-to {
  transform: translate(-50%, -50%) scale(0.9);
}

.toast.bottom.toast-leave-to {
  transform: translateX(-50%) translateY(20px);
}

/* 响应式 */
@media (max-width: 375px) {
  .toast {
    max-width: calc(100vw - 24px);
    min-width: auto;
  }
  
  .toast-content {
    padding: 10px 14px;
    gap: 10px;
  }
  
  .toast-icon i {
    font-size: 14px;
  }
  
  .toast-message {
    font-size: 13px;
  }
}

/* 触摸反馈 */
@media (hover: none) {
  .toast-content:active {
    transform: scale(0.98);
  }
}
</style>