Skip to content

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>

图标按钮

当需要在 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>

按钮尺寸

按钮有大、中、小三种尺寸。通过设置 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>

禁用状态

添加 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>

加载状态

添加 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>

危险按钮

删除/移动/修改权限等危险操作,一般需要二次确认。

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>

幽灵按钮

幽灵按钮将按钮的内容反色,背景变为透明,常用在有色背景上。

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>

按钮组合

可以将多个 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>

块级按钮

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>

API

Button Props

参数说明类型可选值默认值
size尺寸stringlarge / default / small
type类型stringprimary / success / warning / danger / info / text
plain是否朴素按钮booleanfalse
text是否文字按钮booleanfalse
bg是否显示文字按钮背景颜色booleanfalse
link是否链接按钮booleanfalse
round是否圆角按钮booleanfalse
circle是否圆形按钮booleanfalse
loading是否加载中状态booleanfalse
loading-icon自定义加载中状态图标组件string / ComponentLoading
disabled按钮是否为禁用状态booleanfalse
icon图标组件string / Component
autofocus是否默认聚焦booleanfalse
native-type原生 type 属性stringbutton / submit / resetbutton
auto-insert-space自动在两个中文字符之间插入空格boolean
color自定义按钮颜色, 并自动计算 hover 和 active 触发后的颜色string
darkdark 模式, 意味着自动设置 color 为 dark 模式的颜色booleanfalse

Button Events

事件名称说明回调参数
click点击时触发(event: MouseEvent)

Button Slots

插槽名说明子标签
自定义默认内容
loading自定义加载中组件
icon自定义图标组件

ButtonGroup Props

参数说明类型可选值默认值
size用于控制该按钮组内按钮的大小stringlarge / default / small
type用于控制该按钮组内按钮的类型stringprimary / success / warning / danger / info / text

ButtonGroup Slots

插槽名说明子标签
default自定义按钮组内容Button

设计指南

何时使用

  • 标记了一个(或封装一组)操作命令,响应用户点击行为,触发相应的业务逻辑。

设计建议

  • 按钮的层次不宜过多,尽量控制在三个层次以内。
  • 主按钮在同一个操作区域最多出现一次。
  • 按钮的排列顺序应该体现操作的重要程度。
  • 危险操作建议使用危险按钮,并在必要时提供二次确认。
  • 在表单场景中,提交按钮应该放在取消按钮的右侧。

无障碍

  • 按钮应该有明确的文字说明或 aria-label 属性。
  • 支持键盘导航,可以通过 Tab 键聚焦,通过 Enter 或 Space 键触发。
  • 禁用状态的按钮不应该接收焦点。
  • 加载状态的按钮应该有适当的 aria-live 属性。

Released under the MIT License.