VButton.vue
3.19 KB
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
<script lang="ts">
// Props 定义
export interface VButtonProps {
disabled?: boolean;
icon?: string;
loading?: boolean;
size?: 'large' | 'medium' | 'small';
type?: 'button' | 'reset' | 'submit';
variant?: 'danger' | 'ghost' | 'primary' | 'secondary';
}
// Emits 定义
export interface VButtonEmits {
click: [event: MouseEvent];
}
</script>
<script setup lang="ts">
const props = withDefaults(defineProps<VButtonProps>(), {
type: 'button',
size: 'medium',
variant: 'primary',
disabled: false,
loading: false,
icon: '',
});
const emit = defineEmits<VButtonEmits>();
// 点击事件处理
const handleClick = (event: Event) => {
if (!props.disabled && !props.loading) {
emit('click', event as MouseEvent);
}
};
</script>
<template>
<button
:type="type"
class="v-button"
:class="[
`v-button--${variant}`,
`v-button--${size}`,
{
'v-button--disabled': disabled,
'v-button--loading': loading,
},
]"
:disabled="disabled || loading"
@click="handleClick"
>
<span v-if="loading" class="v-button__loading">⏳</span>
<span v-if="icon && !loading" class="v-button__icon">{{ icon }}</span>
<span v-if="$slots.default" class="v-button__content">
<slot></slot>
</span>
</button>
</template>
<style scoped>
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.venus-button {
display: inline-flex;
gap: 8px;
align-items: center;
justify-content: center;
padding: 8px 16px;
font-size: 14px;
font-weight: 500;
line-height: 1.5;
cursor: pointer;
user-select: none;
border: 1px solid transparent;
border-radius: 6px;
transition: all 0.2s ease-in-out;
}
.venus-button:focus {
outline: 2px solid #3b82f6;
outline-offset: 2px;
}
/* 尺寸变体 */
.venus-button--small {
padding: 4px 8px;
font-size: 12px;
}
.venus-button--medium {
padding: 8px 16px;
font-size: 14px;
}
.venus-button--large {
padding: 12px 24px;
font-size: 16px;
}
/* 颜色变体 */
.venus-button--primary {
color: white;
background-color: #3b82f6;
border-color: #3b82f6;
}
.venus-button--primary:hover:not(:disabled) {
background-color: #2563eb;
border-color: #2563eb;
}
.venus-button--secondary {
color: white;
background-color: #6b7280;
border-color: #6b7280;
}
.venus-button--secondary:hover:not(:disabled) {
background-color: #4b5563;
border-color: #4b5563;
}
.venus-button--danger {
color: white;
background-color: #ef4444;
border-color: #ef4444;
}
.venus-button--danger:hover:not(:disabled) {
background-color: #dc2626;
border-color: #dc2626;
}
.venus-button--ghost {
color: #3b82f6;
background-color: transparent;
border-color: #3b82f6;
}
.venus-button--ghost:hover:not(:disabled) {
color: white;
background-color: #3b82f6;
}
/* 状态 */
.venus-button--disabled,
.venus-button:disabled {
cursor: not-allowed;
opacity: 0.5;
}
.venus-button--loading {
cursor: not-allowed;
}
.venus-button--icon-only {
padding: 8px;
}
.venus-button--icon-only.venus-button--small {
padding: 4px;
}
.venus-button--icon-only.venus-button--large {
padding: 12px;
}
.venus-button__loading {
animation: spin 1s linear infinite;
}
</style>