Alert.vue 8.96 KB
Newer Older
LiangLiu's avatar
LiangLiu committed
1
2
3
4
5
6
7
<script setup>
import { alert, getAlertClass, getAlertIconBgClass, getAlertIcon } from '../utils/other'
import { useI18n } from 'vue-i18n'
import { ref, onMounted, onUnmounted } from 'vue'

const { t, locale } = useI18n()

LiangLiu's avatar
LiangLiu committed
8
9
10
// 处理操作按钮点击
const handleActionClick = () => {
    if (alert.value.action && alert.value.action.onClick) {
LiangLiu's avatar
LiangLiu committed
11
        // 先执行action的回调
LiangLiu's avatar
LiangLiu committed
12
        alert.value.action.onClick()
LiangLiu's avatar
LiangLiu committed
13
        // 立即关闭alert
LiangLiu's avatar
LiangLiu committed
14
15
16
17
        alert.value.show = false
    }
}

LiangLiu's avatar
LiangLiu committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// 处理transition离开完成后的回调
const handleAfterLeave = () => {
    // 只有在alert确实关闭时才重置,避免覆盖正在显示的alert
    if (alert.value && !alert.value.show) {
        // 记录当前alert的时间戳,用于后续检查
        const currentTimestamp = alert.value._timestamp

        // 延迟一小段时间再重置,确保不会影响后续的alert显示
        setTimeout(() => {
            // 只有当alert仍然关闭,且时间戳没有变化(没有新alert创建)时才重置
            if (alert.value && !alert.value.show && alert.value._timestamp === currentTimestamp) {
                alert.value = { show: false, message: '', type: 'info', action: null }
            }
        }, 50)
    }
}

LiangLiu's avatar
LiangLiu committed
35
36
37
38
39
// 响应式变量控制Alert位置
const alertPosition = ref({ top: '1rem' })

// 防抖函数
let scrollTimeout = null
LiangLiu's avatar
LiangLiu committed
40
let scrollContainer = null
LiangLiu's avatar
LiangLiu committed
41
42
43
44
45
46
47
48
49
50

// 监听滚动事件,动态调整Alert位置
const handleScroll = () => {
    // 清除之前的定时器
    if (scrollTimeout) {
        clearTimeout(scrollTimeout)
    }

    // 设置新的定时器,防抖处理
    scrollTimeout = setTimeout(() => {
LiangLiu's avatar
LiangLiu committed
51
52
53
54
55
56
57
58
        // 获取实际的滚动容器
        const mainScrollable = scrollContainer || document.querySelector('.main-scrollbar')
        if (!mainScrollable) {
            alertPosition.value = { top: '1rem' }
            return
        }

        const scrollY = mainScrollable.scrollTop
LiangLiu's avatar
LiangLiu committed
59
60
61
62
63
        const viewportHeight = window.innerHeight

        // 如果用户滚动了超过50px,将Alert显示在视口内
        if (scrollY > 50) {
            // 计算Alert应该显示的位置,确保在视口内可见
LiangLiu's avatar
LiangLiu committed
64
65
            // 距离顶部80px(TopBar高度 + 一些间距)
            alertPosition.value = { top: '80px' }
LiangLiu's avatar
LiangLiu committed
66
67
68
69
70
71
72
73
        } else {
            // 在页面顶部时,显示在固定位置
            alertPosition.value = { top: '1rem' }
        }
    }, 10) // 10ms防抖延迟
}

onMounted(() => {
LiangLiu's avatar
LiangLiu committed
74
75
76
77
78
79
80
81
    // 查找实际的滚动容器
    scrollContainer = document.querySelector('.main-scrollbar')

    if (scrollContainer) {
        scrollContainer.addEventListener('scroll', handleScroll, { passive: true })
    }

    // 也监听 window 的滚动(作为后备)
LiangLiu's avatar
LiangLiu committed
82
    window.addEventListener('scroll', handleScroll, { passive: true })
LiangLiu's avatar
LiangLiu committed
83

LiangLiu's avatar
LiangLiu committed
84
85
86
87
88
    // 初始化时也调用一次,确保位置正确
    handleScroll()
})

onUnmounted(() => {
LiangLiu's avatar
LiangLiu committed
89
90
91
    if (scrollContainer) {
        scrollContainer.removeEventListener('scroll', handleScroll)
    }
LiangLiu's avatar
LiangLiu committed
92
93
94
95
96
97
98
    window.removeEventListener('scroll', handleScroll)
    if (scrollTimeout) {
        clearTimeout(scrollTimeout)
    }
})
</script>
<template>
LiangLiu's avatar
LiangLiu committed
99
            <!-- Apple 风格极简提示消息 -->
LiangLiu's avatar
LiangLiu committed
100
            <div v-cloak>
LiangLiu's avatar
LiangLiu committed
101
102
103
104
                <transition
                    enter-active-class="alert-enter-active"
                    leave-active-class="alert-leave-active"
                    enter-from-class="alert-enter-from"
LiangLiu's avatar
LiangLiu committed
105
106
                    leave-to-class="alert-leave-to"
                    @after-leave="handleAfterLeave">
LiangLiu's avatar
LiangLiu committed
107
                    <div v-if="alert.show"
LiangLiu's avatar
LiangLiu committed
108
                        :key="alert._timestamp || alert.message"
LiangLiu's avatar
LiangLiu committed
109
110
111
112
113
114
115
                        class="fixed left-1/2 transform -translate-x-1/2 z-[9999] w-auto min-w-[280px] sm:min-w-[320px] max-w-[calc(100vw-3rem)] sm:max-w-xl px-6 sm:px-6 transition-all duration-500 ease-out"
                        :style="alertPosition">
                        <div class="alert-container">
                            <div class="alert-content">
                                <!-- 图标 -->
                                <div class="alert-icon-wrapper">
                                    <i :class="getAlertIcon(alert.type)" class="alert-icon"></i>
LiangLiu's avatar
LiangLiu committed
116
                                </div>
LiangLiu's avatar
LiangLiu committed
117
                                <!-- 消息文本 -->
LiangLiu's avatar
LiangLiu committed
118
119
                                <div class="alert-message">
                                    <span>{{ alert.message }}</span>
LiangLiu's avatar
LiangLiu committed
120
121
122
                                </div>
                                <!-- 操作按钮和关闭按钮(右侧,紧挨着) -->
                                <div class="alert-actions">
LiangLiu's avatar
LiangLiu committed
123
124
125
126
                                    <!-- 操作链接 - Apple 风格 -->
                                    <button v-if="alert.action" @click="handleActionClick" class="alert-action-link">
                                        {{ alert.action.label }}
                                    </button>
LiangLiu's avatar
LiangLiu committed
127
128
129
130
                                    <!-- 关闭按钮 -->
                                    <button @click="alert.show = false" class="alert-close-btn" aria-label="Close">
                                        <i class="fas fa-times"></i>
                                    </button>
LiangLiu's avatar
LiangLiu committed
131
                                </div>
LiangLiu's avatar
LiangLiu committed
132
133
134
                            </div>
                        </div>
                    </div>
LiangLiu's avatar
LiangLiu committed
135
                </transition>
LiangLiu's avatar
LiangLiu committed
136
137
            </div>
</template>
LiangLiu's avatar
LiangLiu committed
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

<style scoped>
/* Apple 风格 Alert 容器 */
.alert-container {
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(20px) saturate(180%);
    -webkit-backdrop-filter: blur(20px) saturate(180%);
    border-radius: 16px;
    box-shadow:
        0 4px 6px -1px rgba(0, 0, 0, 0.1),
        0 2px 4px -1px rgba(0, 0, 0, 0.06),
        0 0 0 1px rgba(0, 0, 0, 0.05);
    overflow: hidden;
}

/* 深色模式下的容器样式 */
:global(.dark) .alert-container {
    background: rgba(30, 30, 30, 0.95);
    box-shadow:
        0 4px 6px -1px rgba(0, 0, 0, 0.3),
        0 2px 4px -1px rgba(0, 0, 0, 0.2),
        0 0 0 1px rgba(255, 255, 255, 0.08);
}

/* Alert 内容 */
.alert-content {
    display: flex;
    align-items: center;
    gap: 12px;
    padding: 14px 18px;
}

/* 图标包装器 */
.alert-icon-wrapper {
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

/* 图标样式 */
.alert-icon {
    font-size: 18px;
    color: #1d1d1f;
}

:global(.dark) .alert-icon {
    color: #f5f5f7;
}

/* 消息文本 */
.alert-message {
    flex: 1;
    font-size: 14px;
    font-weight: 500;
    line-height: 1.5;
    color: #1d1d1f;
    letter-spacing: -0.01em;
LiangLiu's avatar
LiangLiu committed
196
    min-width: 0; /* 允许文本收缩 */
LiangLiu's avatar
LiangLiu committed
197
198
199
200
201
202
}

:global(.dark) .alert-message {
    color: #f5f5f7;
}

LiangLiu's avatar
LiangLiu committed
203
204
205
206
207
208
209
210
/* 操作按钮和关闭按钮容器 */
.alert-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

LiangLiu's avatar
LiangLiu committed
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
/* 关闭按钮 */
.alert-close-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    border-radius: 50%;
    border: none;
    background: transparent;
    color: #86868b;
    cursor: pointer;
    flex-shrink: 0;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

.alert-close-btn:hover {
    background: rgba(0, 0, 0, 0.05);
    color: #1d1d1f;
    transform: scale(1.05);
}

.alert-close-btn:active {
    transform: scale(0.95);
}

:global(.dark) .alert-close-btn:hover {
    background: rgba(255, 255, 255, 0.08);
    color: #f5f5f7;
}

.alert-close-btn i {
    font-size: 12px;
}

/* 操作链接 - Apple 风格下划线文本 */
.alert-action-link {
LiangLiu's avatar
LiangLiu committed
248
249
    display: inline-flex;
    align-items: center;
LiangLiu's avatar
LiangLiu committed
250
251
252
253
    padding: 0;
    border: none;
    background: transparent;
    color: var(--brand-primary);
LiangLiu's avatar
LiangLiu committed
254
    font-size: 14px;
LiangLiu's avatar
LiangLiu committed
255
256
257
258
259
260
261
    font-weight: 600;
    text-decoration: underline;
    text-underline-offset: 2px;
    text-decoration-thickness: 1px;
    cursor: pointer;
    transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    white-space: nowrap;
LiangLiu's avatar
LiangLiu committed
262
    height: 24px; /* 与关闭按钮高度一致 */
LiangLiu's avatar
LiangLiu committed
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
}

.alert-action-link:hover {
    color: var(--brand-primary);
    opacity: 0.8;
    text-decoration-thickness: 2px;
}

.alert-action-link:active {
    opacity: 0.6;
}

:global(.dark) .alert-action-link {
    color: var(--brand-primary-light);
}

:global(.dark) .alert-action-link:hover {
    color: var(--brand-primary-light);
}

/* 进入动画 */
.alert-enter-active {
    transition: all 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.alert-leave-active {
    transition: all 0.3s cubic-bezier(0.4, 0, 1, 1);
}

.alert-enter-from {
    opacity: 0;
    transform: translate(-50%, -20px) scale(0.95);
}

.alert-leave-to {
    opacity: 0;
    transform: translate(-50%, -10px) scale(0.98);
}

/* 响应式设计 */
@media (max-width: 640px) {
    .alert-content {
        padding: 12px 16px;
        gap: 8px;
    }

    .alert-message {
        font-size: 13px;
    }

    .alert-icon {
        font-size: 16px;
    }

    .alert-action-link {
        font-size: 13px;
LiangLiu's avatar
LiangLiu committed
319
320
321
322
323
        height: 22px; /* 移动端稍微小一点 */
    }

    .alert-actions {
        gap: 6px; /* 移动端间距更小 */
LiangLiu's avatar
LiangLiu committed
324
325
326
    }
}
</style>