Alert.vue 8.74 KB
Newer Older
litzh's avatar
litzh committed
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
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
<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()

// 处理操作按钮点击
const handleActionClick = () => {
    if (alert.value.action && alert.value.action.onClick) {
        // 先执行action的回调
        alert.value.action.onClick()
        // 立即关闭alert
        alert.value.show = false
    }
}

// 处理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)
    }
}

// 响应式变量控制Alert位置
const alertPosition = ref({ top: '1rem' })

// 防抖函数
let scrollTimeout = null
let scrollContainer = null

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

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

        const scrollY = mainScrollable.scrollTop
        const viewportHeight = window.innerHeight

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

onMounted(() => {
    // 查找实际的滚动容器
    scrollContainer = document.querySelector('.main-scrollbar')

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

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

    // 初始化时也调用一次,确保位置正确
    handleScroll()
})

onUnmounted(() => {
    if (scrollContainer) {
        scrollContainer.removeEventListener('scroll', handleScroll)
    }
    window.removeEventListener('scroll', handleScroll)
    if (scrollTimeout) {
        clearTimeout(scrollTimeout)
    }
})
</script>
<template>
            <!-- Apple 风格极简提示消息 -->
            <div v-cloak>
                <transition
                    enter-active-class="alert-enter-active"
                    leave-active-class="alert-leave-active"
                    enter-from-class="alert-enter-from"
                    leave-to-class="alert-leave-to"
                    @after-leave="handleAfterLeave">
                    <div v-if="alert.show"
                        :key="alert._timestamp || alert.message"
                        class="fixed right-6 z-[9999] w-auto min-w-[260px] sm:min-w-[300px] max-w-[calc(100vw-2.5rem)] sm:max-w-md px-4 sm:px-5 transition-all duration-500 ease-out"
                        :style="{ top: alertPosition.top }">
                        <div class="alert-container text-[#1d1d1f] bg-white/95 dark:text-white dark:bg-[#0d0d12]/90 dark:shadow-[0_12px_32px_rgba(0,0,0,0.6),0_4px_12px_rgba(0,0,0,0.4),0_0_0_1px_rgba(255,255,255,0.06)]">
                            <div class="alert-content">
                                <!-- 图标 -->
                                <div class="alert-icon-wrapper">
                                    <i :class="getAlertIcon(alert.type)" class="alert-icon"></i>
                                </div>
                                <!-- 消息文本 -->
                                <div class="alert-message">
                                    <span>{{ alert.message }}</span>
                                </div>
                                <!-- 操作按钮和关闭按钮(右侧,紧挨着) -->
                                <div class="alert-actions">
                                    <!-- 操作链接 - Apple 风格 -->
                                    <button v-if="alert.action" @click="handleActionClick" class="alert-action-link">
                                        {{ alert.action.label }}
                                    </button>
                                    <!-- 关闭按钮 -->
                                    <button @click="alert.show = false" class="alert-close-btn" aria-label="Close">
                                        <i class="fas fa-times"></i>
                                    </button>
                                </div>
                            </div>
                        </div>
                    </div>
                </transition>
            </div>
</template>

<style scoped>
/* Apple 风格 Alert 容器 */
.alert-container {
    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;
}

/* 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: var(--brand-primary);
}

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

/* 消息文本 */
.alert-message {
    flex: 1;
    font-size: 14px;
    font-weight: 500;
    line-height: 1.5;
    letter-spacing: -0.01em;
    min-width: 0; /* 允许文本收缩 */
}

/* 操作按钮和关闭按钮容器 */
.alert-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-shrink: 0;
}

/* 关闭按钮 */
.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 {
    display: inline-flex;
    align-items: center;
    padding: 0;
    border: none;
    background: transparent;
    color: var(--brand-primary);
    font-size: 14px;
    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;
    height: 24px; /* 与关闭按钮高度一致 */
}

.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: #ffffff;
}

:global(.dark) .alert-action-link:hover {
    color: #ffffff;
}

/* 进入动画 */
.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;
        height: 22px; /* 移动端稍微小一点 */
    }

    .alert-actions {
        gap: 6px; /* 移动端间距更小 */
    }
}
</style>