Button 按钮
按钮用于开始一个即时操作。
何时使用
标记了一个(或封装一组)操作命令,响应用户点击行为,触发相应的业务逻辑。
在 Ghuo Design 中我们提供了五种按钮。
- 主按钮:用于主行动点,一个操作区域只能有一个主按钮。
- 默认按钮:用于没有主次之分的一组行动点。
- 虚线按钮:常用于添加操作。
- 文本按钮:用于最次级的行动点。
- 链接按钮:一般用于链接,即导航至某位置。
以及三种状态属性与上面配合使用。
- 危险:删除/移动/修改权限等危险操作,一般需要二次确认。
- 幽灵:用于背景色比较复杂的地方,常用在首页/产品页等展示场景。
- 禁用:行动点不可用的时候,一般需要文案解释。
代码演示
按钮类型
按钮有五种类型:主按钮、默认按钮、虚线按钮、文本按钮和链接按钮。主按钮在同一个操作区域最多出现一次。
vue
<template>
<el-space wrap>
<el-button type="primary">主按钮</el-button>
<el-button>默认按钮</el-button>
<el-button type="info" plain>虚线按钮</el-button>
<el-button type="text">文本按钮</el-button>
<el-button type="primary" link>链接按钮</el-button>
</el-space>
</template>
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
图标按钮
当需要在 Button 内嵌入 Icon 时,可以设置 icon 属性,或者直接在 Button 内使用 Icon 组件。
vue
<template>
<el-space wrap>
<el-button type="primary">
<el-icon><Search /></el-icon>
搜索
</el-button>
<el-button type="primary">
上传
<el-icon><Upload /></el-icon>
</el-button>
<el-button type="primary" circle>
<el-icon><Search /></el-icon>
</el-button>
<el-button type="primary" circle>
<el-icon><Edit /></el-icon>
</el-button>
<el-button type="primary" circle>
<el-icon><Delete /></el-icon>
</el-button>
</el-space>
</template>
<script setup>
import { Search, Upload, Edit, Delete } from '@element-plus/icons-vue'
</script>
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
按钮尺寸
按钮有大、中、小三种尺寸。通过设置 size 为 large small 分别把按钮设为大、小尺寸。若不设置 size,则尺寸为中。
vue
<template>
<el-space wrap align="center">
<el-button type="primary" size="large">大号按钮</el-button>
<el-button type="primary">默认按钮</el-button>
<el-button type="primary" size="small">小号按钮</el-button>
</el-space>
<el-space wrap align="center">
<el-button type="primary" size="large" round>大号按钮</el-button>
<el-button type="primary" round>默认按钮</el-button>
<el-button type="primary" size="small" round>小号按钮</el-button>
</el-space>
<el-space wrap align="center">
<el-button type="primary" size="large" circle>
<el-icon><Search /></el-icon>
</el-button>
<el-button type="primary" circle>
<el-icon><Search /></el-icon>
</el-button>
<el-button type="primary" size="small" circle>
<el-icon><Search /></el-icon>
</el-button>
</el-space>
</template>
<script setup>
import { Search } from '@element-plus/icons-vue'
</script>
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
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
禁用状态
添加 disabled 属性即可让按钮处于不可用状态,同时按钮样式也会改变。
vue
<template>
<el-space wrap>
<el-button type="primary" disabled>主按钮</el-button>
<el-button disabled>默认按钮</el-button>
<el-button type="info" plain disabled>虚线按钮</el-button>
<el-button type="text" disabled>文本按钮</el-button>
</el-space>
</template>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
加载状态
添加 loading 属性即可让按钮处于加载状态,最后两个按钮演示点击后进入加载状态。
vue
<template>
<el-space wrap>
<el-button type="primary" loading>加载中</el-button>
<el-button type="primary" loading>
<template #loading>
<div class="custom-loading">
<el-icon><Loading /></el-icon>
</div>
</template>
加载中
</el-button>
<el-button type="primary" @click="handleClick1">
点击加载
</el-button>
<el-button type="primary" :loading="loading2" @click="handleClick2">
点击加载
</el-button>
</el-space>
</template>
<script setup>
import { ref } from 'vue'
import { Loading } from '@element-plus/icons-vue'
const loading2 = ref(false)
const handleClick1 = () => {
// 处理点击事件
}
const handleClick2 = () => {
loading2.value = true
setTimeout(() => {
loading2.value = false
}, 2000)
}
</script>
<style scoped>
.custom-loading .el-icon {
animation: rotating 2s linear infinite;
}
@keyframes rotating {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}
</style>
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
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
危险按钮
删除/移动/修改权限等危险操作,一般需要二次确认。
vue
<template>
<el-space wrap>
<el-button type="danger">危险按钮</el-button>
<el-button type="danger" plain>危险按钮</el-button>
<el-button type="danger" text>危险按钮</el-button>
<el-button type="danger" link>危险按钮</el-button>
</el-space>
</template>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
幽灵按钮
幽灵按钮将按钮的内容反色,背景变为透明,常用在有色背景上。
vue
<template>
<div style="background: #1890ff; padding: 24px;">
<el-space wrap>
<el-button type="primary" plain>主按钮</el-button>
<el-button plain>默认按钮</el-button>
<el-button type="info" plain>信息按钮</el-button>
<el-button type="success" plain>成功按钮</el-button>
<el-button type="warning" plain>警告按钮</el-button>
<el-button type="danger" plain>危险按钮</el-button>
</el-space>
</div>
</template>
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
按钮组合
可以将多个 Button 放入 ButtonGroup 的容器中。通过设置 size 为 large small 分别把按钮组设为大、小尺寸。若不设置 size,则尺寸为中。
<el-button-group>
<el-button type="primary">
<el-icon><Edit /></el-icon>
</el-button>
<el-button type="primary">
<el-icon><Share /></el-icon>
</el-button>
<el-button type="primary">
<el-icon><Delete /></el-icon>
</el-button>
</el-button-group>
<el-button-group>
<el-button>L</el-button>
<el-button>M</el-button>
<el-button>R</el-button>
</el-button-group>
vue
<template>
<el-space direction="vertical">
<el-button-group>
<el-button type="primary">
<el-icon><ArrowLeft /></el-icon>
上一页
</el-button>
<el-button type="primary">
下一页
<el-icon><ArrowRight /></el-icon>
</el-button>
</el-button-group>
<el-button-group>
<el-button type="primary">
<el-icon><Edit /></el-icon>
</el-button>
<el-button type="primary">
<el-icon><Share /></el-icon>
</el-button>
<el-button type="primary">
<el-icon><Delete /></el-icon>
</el-button>
</el-button-group>
<el-button-group>
<el-button>L</el-button>
<el-button>M</el-button>
<el-button>R</el-button>
</el-button-group>
</el-space>
</template>
<script setup>
import { ArrowLeft, ArrowRight, Edit, Share, Delete } from '@element-plus/icons-vue'
</script>
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
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
块级按钮
block 属性将使按钮适合其父宽度。
vue
<template>
<el-space direction="vertical" style="width: 100%;">
<el-button type="primary" style="width: 100%;">主按钮</el-button>
<el-button style="width: 100%;">默认按钮</el-button>
<el-button type="info" plain style="width: 100%;">虚线按钮</el-button>
<el-button type="text" style="width: 100%;">文本按钮</el-button>
</el-space>
</template>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
API
Button Props
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
size | 尺寸 | string | large / default / small | — |
type | 类型 | string | primary / success / warning / danger / info / text | — |
plain | 是否朴素按钮 | boolean | — | false |
text | 是否文字按钮 | boolean | — | false |
bg | 是否显示文字按钮背景颜色 | boolean | — | false |
link | 是否链接按钮 | boolean | — | false |
round | 是否圆角按钮 | boolean | — | false |
circle | 是否圆形按钮 | boolean | — | false |
loading | 是否加载中状态 | boolean | — | false |
loading-icon | 自定义加载中状态图标组件 | string / Component | — | Loading |
disabled | 按钮是否为禁用状态 | boolean | — | false |
icon | 图标组件 | string / Component | — | — |
autofocus | 是否默认聚焦 | boolean | — | false |
native-type | 原生 type 属性 | string | button / submit / reset | button |
auto-insert-space | 自动在两个中文字符之间插入空格 | boolean | — | — |
color | 自定义按钮颜色, 并自动计算 hover 和 active 触发后的颜色 | string | — | — |
dark | dark 模式, 意味着自动设置 color 为 dark 模式的颜色 | boolean | — | false |
Button Events
事件名称 | 说明 | 回调参数 |
---|---|---|
click | 点击时触发 | (event: MouseEvent) |
Button Slots
插槽名 | 说明 | 子标签 |
---|---|---|
— | 自定义默认内容 | — |
loading | 自定义加载中组件 | — |
icon | 自定义图标组件 | — |
ButtonGroup Props
参数 | 说明 | 类型 | 可选值 | 默认值 |
---|---|---|---|---|
size | 用于控制该按钮组内按钮的大小 | string | large / default / small | — |
type | 用于控制该按钮组内按钮的类型 | string | primary / success / warning / danger / info / text | — |
ButtonGroup Slots
插槽名 | 说明 | 子标签 |
---|---|---|
default | 自定义按钮组内容 | Button |
设计指南
何时使用
- 标记了一个(或封装一组)操作命令,响应用户点击行为,触发相应的业务逻辑。
设计建议
- 按钮的层次不宜过多,尽量控制在三个层次以内。
- 主按钮在同一个操作区域最多出现一次。
- 按钮的排列顺序应该体现操作的重要程度。
- 危险操作建议使用危险按钮,并在必要时提供二次确认。
- 在表单场景中,提交按钮应该放在取消按钮的右侧。
无障碍
- 按钮应该有明确的文字说明或 aria-label 属性。
- 支持键盘导航,可以通过 Tab 键聚焦,通过 Enter 或 Space 键触发。
- 禁用状态的按钮不应该接收焦点。
- 加载状态的按钮应该有适当的 aria-live 属性。