-
-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Button.vue
212 lines (187 loc) · 3.19 KB
/
Button.vue
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
<template>
<button
:class="classes"
:disabled="disabled"
class="btn"
type="button"
@click="handleClick"
>
<span v-if="$slots.icon || loading" class="btn-icon">
<svg
v-if="loading"
class="animate-spin"
fill="none"
viewBox="0 0 24 24"
xmlns="http://www.w3.org/2000/svg"
>
<circle
class="opacity-25"
cx="12"
cy="12"
r="10"
stroke="currentColor"
stroke-width="4"
></circle>
<path
class="opacity-75"
d="M4 12a8 8 0 018-8V0C5.373 0 0 5.373 0 12h4zm2 5.291A7.962 7.962 0 014 12H0c0 3.042 1.135 5.824 3 7.938l3-2.647z"
fill="currentColor"
></path>
</svg>
<slot v-else name="icon" />
</span>
<span class="btn-content">
<slot />
</span>
</button>
</template>
<script lang="ts" setup>
import { computed } from "vue";
import type { RouteLocationRaw } from "vue-router";
import { useRouter } from "vue-router";
import type { Size, Type } from "./interface";
const props = withDefaults(
defineProps<{
type?: Type;
size?: Size;
circle?: boolean;
block?: boolean;
disabled?: boolean;
loading?: boolean;
route?: RouteLocationRaw | undefined;
}>(),
{
type: "default",
size: "md",
circle: false,
block: false,
disabled: false,
loading: false,
route: undefined,
}
);
const router = useRouter();
const emit = defineEmits<{
(event: "click"): void;
}>();
const classes = computed(() => {
return [
`btn-${props.size}`,
`btn-${props.type}`,
{ "btn-circle": props.circle },
{ "btn-block": props.block },
{ "btn-loading": props.loading },
];
});
function handleClick() {
if (props.disabled || props.loading) return;
if (props.route) {
router.push(props.route);
}
emit("click");
}
</script>
<style lang="scss">
.btn {
@apply rounded-base
inline-flex
flex-shrink-0
cursor-pointer
select-none
flex-wrap
items-center
justify-center
transition-all
text-center
text-sm
no-underline
h-9
px-4
outline-0
border-none
appearance-none
align-middle;
&:hover {
@apply opacity-90;
}
&:active {
@apply opacity-100;
}
&:disabled {
@apply opacity-50
cursor-not-allowed;
}
}
.btn-default {
border: 1px solid #d9d9d9;
&:hover {
@apply bg-gray-100;
}
.btn-icon {
@apply text-secondary;
}
}
.btn-primary {
@apply text-white bg-primary #{!important};
}
.btn-secondary {
@apply text-white bg-secondary #{!important};
}
.btn-danger {
background-color: #d71d1d !important;
@apply text-white;
}
.btn-block {
@apply w-full;
}
.btn-icon {
@apply h-5 w-5
text-white
mr-3;
}
.btn-loading {
@apply cursor-not-allowed;
&:hover {
@apply opacity-100;
}
}
.btn-lg {
@apply h-11
px-5
text-lg;
}
.btn-sm {
@apply h-7
px-3
text-xs;
.btn-icon {
@apply h-3
w-3
mr-2;
}
}
.btn-xs {
@apply h-6
px-2
text-xs;
.btn-icon {
@apply h-3
w-3
mr-2;
}
}
.btn-circle {
@apply w-9
p-0
rounded-full;
}
.btn-lg.btn-circle {
@apply w-11;
}
.btn-sm.btn-circle {
@apply w-7;
}
.btn-xs.btn-circle {
@apply w-6;
}
</style>