Skip to content

Commit 155bdf2

Browse files
committed
feat: 优化提示组件,支持位置和图标显示选项
1 parent e46df8a commit 155bdf2

File tree

2 files changed

+53
-6
lines changed

2 files changed

+53
-6
lines changed

src/renderer/components/ShortcutToast.vue

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<template>
22
<transition name="shortcut-toast">
3-
<div v-if="visible" class="shortcut-toast">
3+
<div v-if="visible" class="shortcut-toast" :class="`shortcut-toast-${position}`">
44
<div class="shortcut-toast-content">
5-
<div class="shortcut-toast-icon">
5+
<div v-if="showIcon" class="shortcut-toast-icon">
66
<i :class="icon"></i>
77
</div>
88
<div class="shortcut-toast-text">{{ text }}</div>
@@ -14,12 +14,24 @@
1414
<script lang="ts" setup>
1515
import { onBeforeUnmount, ref } from 'vue';
1616
17+
defineProps({
18+
position: {
19+
type: String,
20+
default: 'center',
21+
validator: (val: string) => ['top', 'center', 'bottom'].includes(val)
22+
},
23+
showIcon: {
24+
type: Boolean,
25+
default: true
26+
}
27+
});
28+
1729
const visible = ref(false);
1830
const text = ref('');
1931
const icon = ref('');
2032
let timer: NodeJS.Timeout | null = null;
2133
22-
const show = (message: string, iconName: string) => {
34+
const show = (message: string, iconName = '') => {
2335
if (timer) {
2436
clearTimeout(timer);
2537
}
@@ -54,9 +66,28 @@ defineExpose({
5466

5567
<style lang="scss" scoped>
5668
.shortcut-toast {
57-
@apply fixed left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 z-[9999];
69+
@apply fixed left-1/2 z-[9999];
5870
@apply flex items-center justify-center;
5971
72+
// 位置变体
73+
&-center {
74+
@apply top-1/2 -translate-y-1/2;
75+
.shortcut-toast-content {
76+
@apply p-2;
77+
}
78+
}
79+
80+
&-top {
81+
@apply top-20;
82+
}
83+
84+
&-bottom {
85+
@apply bottom-40;
86+
}
87+
88+
// 水平居中
89+
@apply -translate-x-1/2;
90+
6091
&-content {
6192
@apply flex flex-col items-center gap-2 p-4 rounded-lg;
6293
@apply bg-light-200 bg-opacity-70 dark:bg-dark-200 dark:bg-opacity-90;

src/renderer/utils/shortcutToast.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,16 @@ import ShortcutToast from '@/components/ShortcutToast.vue';
55
let container: HTMLDivElement | null = null;
66
let toastInstance: any = null;
77

8-
export function showShortcutToast(message: string, iconName: string) {
8+
interface ToastOptions {
9+
position?: 'top' | 'center' | 'bottom';
10+
showIcon?: boolean;
11+
}
12+
13+
export function showShortcutToast(
14+
message: string,
15+
iconName = '',
16+
options: ToastOptions = {}
17+
) {
918
// 如果容器不存在,创建一个新的容器
1019
if (!container) {
1120
container = document.createElement('div');
@@ -20,6 +29,8 @@ export function showShortcutToast(message: string, iconName: string) {
2029

2130
// 创建新的 toast 实例
2231
const vnode = createVNode(ShortcutToast, {
32+
position: options.position || 'center',
33+
showIcon: options.showIcon !== undefined ? options.showIcon : true,
2334
onDestroy: () => {
2435
if (container) {
2536
render(null, container);
@@ -35,6 +46,11 @@ export function showShortcutToast(message: string, iconName: string) {
3546

3647
// 显示 toast
3748
if (toastInstance) {
38-
toastInstance.show(message, iconName);
49+
toastInstance.show(message, iconName, { showIcon: options.showIcon });
3950
}
4051
}
52+
53+
// 新增便捷方法 - 底部无图标 toast
54+
export function showBottomToast(message: string) {
55+
showShortcutToast(message, '', { position: 'bottom', showIcon: false });
56+
}

0 commit comments

Comments
 (0)