Skip to content

Button 按钮

常用的操作按钮,具有像素风格的视觉效果。

名称分类说明类型默认值
type? 风格按钮类型'primary' | 'success' | 'warning' | 'danger' | 'info'
plain? 风格是否为朴素按钮booleanfalse
round? 风格是否为圆角按钮booleanfalse
circle? 风格是否为圆形按钮booleanfalse
dash? 风格是否为虚线边框按钮booleanfalse
ghost? 风格是否为幽灵按钮(无边框/背景)booleanfalse
link? 风格是否为链接按钮(下划线文字样式)booleanfalse
block? 风格是否为块级按钮(撑满父容器宽度)booleanfalse
responsive? 风格是否为响应式按钮(根据视口自适应尺寸)booleanfalse
size? 尺寸按钮尺寸'large' | 'default' | 'small''default'
color? 颜色自定义按钮颜色(十六进制)string
loading? 状态是否为加载状态booleanfalse
disabled? 状态是否禁用booleanfalse
autofocus? 行为是否自动聚焦booleanfalse
native-type? 行为原生 type 属性'button' | 'submit' | 'reset''button'
use-throttle? 行为是否启用节流booleanfalse
throttle-duration? 行为节流时间(毫秒)number500
loading-icon? 内容自定义加载图标string'spinner'
icon? 内容图标名称string
tag? 内容自定义元素标签string'button'
aria-label? 内容按钮的 ARIA 标签,用于无障碍访问string
click? 事件点击按钮时触发(event: MouseEvent) => void
default? 插槽按钮内容
loading? 插槽自定义加载图标
ref? 暴露按钮 HTML 元素Ref<HTMLButtonElement>
size? 暴露按钮尺寸ComputedRef<string>
type? 暴露按钮类型ComputedRef<string>
disabled? 暴露是否禁用ComputedRef<boolean>

基础用法

使用 typeplainroundcircle 属性来定义按钮的样式。

<template>
  <div class="demo-button">
    <div class="row">
      <px-button>Default</px-button>
      <px-button type="primary">Primary</px-button>
      <px-button type="success">Success</px-button>
      <px-button type="info">Info</px-button>
      <px-button type="warning">Warning</px-button>
      <px-button type="danger">Danger</px-button>
    </div>
    <div class="row">
      <px-button plain>Plain</px-button>
      <px-button type="primary" plain>Primary</px-button>
      <px-button type="success" plain>Success</px-button>
      <px-button type="info" plain>Info</px-button>
      <px-button type="warning" plain>Warning</px-button>
      <px-button type="danger" plain>Danger</px-button>
    </div>
    <div class="row">
      <px-button round>Round</px-button>
      <px-button type="primary" round>Primary</px-button>
      <px-button type="success" round>Success</px-button>
      <px-button type="info" round>Info</px-button>
      <px-button type="warning" round>Warning</px-button>
      <px-button type="danger" round>Danger</px-button>
    </div>
    <div class="row">
      <px-button icon="search" circle />
      <px-button type="primary" icon="edit" circle />
      <px-button type="success" icon="check" circle />
      <px-button type="info" icon="message" circle />
      <px-button type="warning" icon="alert" circle />
      <px-button type="danger" icon="trash" circle />
    </div>
  </div>
</template>

<style scoped>
.demo-button {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.row {
  display: grid;
  grid-template-columns: repeat(6, auto);
  justify-content: start;
  align-items: center;
  gap: 10px;
}
</style>

禁用状态

使用 disabled 属性来禁用按钮。

<template>
  <div class="demo-button">
    <div class="row">
      <px-button>Default</px-button>
      <px-button type="primary">Primary</px-button>
      <px-button type="success">Success</px-button>
      <px-button type="info">Info</px-button>
      <px-button type="warning">Warning</px-button>
      <px-button type="danger">Danger</px-button>
    </div>
    <div class="row">
      <px-button disabled>Default</px-button>
      <px-button type="primary" disabled>Primary</px-button>
      <px-button type="success" disabled>Success</px-button>
      <px-button type="info" disabled>Info</px-button>
      <px-button type="warning" disabled>Warning</px-button>
      <px-button type="danger" disabled>Danger</px-button>
    </div>
  </div>
</template>

<style scoped>
.demo-button {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.row {
  display: grid;
  grid-template-columns: repeat(6, auto);
  justify-content: start;
  align-items: center;
  gap: 12px;
}
</style>

不同尺寸

使用 size 属性来设置按钮的大小。

<template>
  <div class="demo-size">
    <div class="row">
      <px-button size="large">Large</px-button>
      <px-button>Default</px-button>
      <px-button size="small">Small</px-button>
      <px-button size="large" round>Large</px-button>
      <px-button round>Default</px-button>
      <px-button size="small" round>Small</px-button>
    </div>
    <div class="row">
      <px-button size="large" icon="search" circle />
      <px-button icon="search" circle />
      <px-button size="small" icon="search" circle />
    </div>
  </div>
</template>

<style scoped>
.demo-size {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}
.row :deep(.px-button + .px-button) {
  margin-left: 0;
}
</style>

图标按钮

使用 icon 属性来为按钮添加图标。

<template>
  <div class="row">
    <px-button type="primary" icon="edit" />
    <px-button type="primary" icon="link" />
    <px-button type="primary" icon="trash" />
    <px-button type="primary" icon="search">Search</px-button>
    <px-button type="primary">
      Upload<px-icon icon="upload" style="margin-left: 8px" />
    </px-button>
  </div>
</template>

<style scoped>
.row {
  display: flex;
  align-items: center;
  gap: 12px;
}
</style>

虚线边框

使用 dash 属性来应用虚线边框样式。像素 clip-path 边框将替换为 CSS 虚线边框,呈现手绘/剪纸风格。

<template>
  <div class="row">
    <px-button dash>Default</px-button>
    <px-button type="primary" dash>Primary</px-button>
    <px-button type="success" dash>Success</px-button>
    <px-button type="info" dash>Info</px-button>
    <px-button type="warning" dash>Warning</px-button>
    <px-button type="danger" dash>Danger</px-button>
  </div>
</template>

<style scoped>
.row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}
.row :deep(.px-button + .px-button) {
  margin-left: 0;
}
</style>

幽灵按钮

使用 ghost 属性来创建无边框、无背景的按钮。仅文字可见,悬停时出现淡色填充。

<template>
  <div class="row">
    <px-button ghost>Default</px-button>
    <px-button type="primary" ghost>Primary</px-button>
    <px-button type="success" ghost>Success</px-button>
    <px-button type="info" ghost>Info</px-button>
    <px-button type="warning" ghost>Warning</px-button>
    <px-button type="danger" ghost>Danger</px-button>
  </div>
</template>

<style scoped>
.row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}
.row :deep(.px-button + .px-button) {
  margin-left: 0;
}
</style>

链接按钮

使用 link 属性来创建下划线文字按钮,无边框、无背景、无阴影。适用于内联操作或超链接风格的交互。

<template>
  <div class="row">
    <px-button link>Default</px-button>
    <px-button type="primary" link>Primary</px-button>
    <px-button type="success" link>Success</px-button>
    <px-button type="info" link>Info</px-button>
    <px-button type="warning" link>Warning</px-button>
    <px-button type="danger" link>Danger</px-button>
  </div>
</template>

<style scoped>
.row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}
.row :deep(.px-button + .px-button) {
  margin-left: 0;
}
</style>

块级按钮

使用 block 属性使按钮撑满父容器的宽度。

<template>
  <div class="column">
    <px-button block>Default Block</px-button>
    <px-button type="primary" block>Primary Block</px-button>
    <px-button type="success" block>Success Block</px-button>
  </div>
</template>

<style scoped>
.column {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 400px;
}
</style>

响应式按钮

使用 responsive 属性使按钮根据视口宽度自动调整尺寸。

断点宽度高度字体大小
基础< 640px24px超小
sm≥ 640px28px
md≥ 768px36px基础
lg≥ 1024px44px
xl≥ 1280px44px大(更宽内边距)
<template>
  <div class="row">
    <px-button responsive>Responsive</px-button>
    <px-button type="primary" responsive>Primary</px-button>
    <px-button type="success" responsive>Success</px-button>
  </div>
  <p class="hint">↔ Resize your browser window to see the button adapt.</p>
</template>

<style scoped>
.row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}
.row :deep(.px-button + .px-button) {
  margin-left: 0;
}
.hint {
  margin-top: 12px;
  font-size: 12px;
  color: var(--px-text-color-secondary);
}
</style>

加载状态

使用 loading 属性来设置按钮的加载状态。

<template>
  <div class="row">
    <px-button type="primary" loading>Loading</px-button>
    <px-button type="primary" loading-icon="loader" loading
      >Loading</px-button
    >
    <px-button type="primary" loading>
      <template #loading>
        <px-icon style="margin-right: 6px" icon="bookmark" spin />
      </template>
      Loading
    </px-button>
  </div>
</template>

<style scoped>
.row {
  display: flex;
  align-items: center;
  gap: 12px;
}
</style>

按钮组

使用 <px-button-group> 组件来嵌套多个按钮。

<template>
  <div class="demo-button">
    <div class="row">
      <px-button-group disabled>
        <px-button type="primary" icon="arrow-left">Previous Page</px-button>
        <px-button type="primary">
          Next Page<px-icon icon="arrow-right" style="margin-left: 8px" />
        </px-button>
      </px-button-group>
    </div>
    <div class="row">
      <px-button-group type="warning" size="small">
        <px-button type="primary" icon="edit" round />
        <px-button type="primary" icon="link" />
        <px-button type="primary" icon="trash" round />
      </px-button-group>
    </div>
  </div>
</template>

<style scoped>
.demo-button {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.row {
  display: flex;
  align-items: center;
  gap: 12px;
}
</style>

自定义颜色

使用 color 属性来设置按钮的自定义颜色。文字颜色、悬停状态、激活状态和禁用状态会根据提供的颜色自动生成。

<template>
  <div class="demo-button">
    <div class="row">
      <px-button color="#626aef">Indigo</px-button>
      <px-button color="#ff6b9d">Pink</px-button>
      <px-button color="#36cfc9">Teal</px-button>
      <px-button color="#fac858">Gold</px-button>
      <px-button color="#ee6666">Coral</px-button>
      <px-button color="#3ba272">Emerald</px-button>
    </div>
    <div class="row">
      <px-button color="#626aef" plain>Indigo</px-button>
      <px-button color="#ff6b9d" plain>Pink</px-button>
      <px-button color="#36cfc9" plain>Teal</px-button>
      <px-button color="#fac858" plain>Gold</px-button>
      <px-button color="#ee6666" plain>Coral</px-button>
      <px-button color="#3ba272" plain>Emerald</px-button>
    </div>
    <div class="row">
      <px-button color="#626aef" disabled>Indigo</px-button>
      <px-button color="#ff6b9d" disabled>Pink</px-button>
      <px-button color="#36cfc9" disabled>Teal</px-button>
      <px-button color="#fac858" disabled>Gold</px-button>
      <px-button color="#ee6666" disabled>Coral</px-button>
      <px-button color="#3ba272" disabled>Emerald</px-button>
    </div>
    <div class="row">
      <px-button color="#626aef" icon="edit" circle />
      <px-button color="#ff6b9d" icon="heart" circle />
      <px-button color="#36cfc9" icon="check" circle />
      <px-button color="#fac858" icon="bookmark" circle />
      <px-button color="#ee6666" icon="trash" circle />
      <px-button color="#3ba272" icon="link" circle />
    </div>
  </div>
</template>

<style scoped>
.demo-button {
  display: flex;
  flex-direction: column;
  gap: 20px;
}
.row {
  display: grid;
  grid-template-columns: repeat(6, auto);
  justify-content: start;
  align-items: center;
  gap: 12px;
}
</style>

登录按钮

结合 iconcolor 属性来创建社交登录按钮。所有 486 个 pixelarticons 图标已内置,githubmail 等图标开箱即用。

<template>
  <div class="login-demo">
    <px-button icon="mail" color="#000000">Login with Email</px-button>
    <px-button icon="github" color="#24292f">Login with GitHub</px-button>
  </div>
</template>

<style scoped>
.login-demo {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 320px;
}
.login-demo :deep(.px-button) {
  width: 100%;
  justify-content: center;
}
.login-demo :deep(.px-button + .px-button) {
  margin-left: 0;
}
</style>

像素 3D 效果

按钮带有自动斜面高光效果——左上方的亮色内嵌和右下方的暗色内嵌——营造出凸起的像素表面,灵感来自经典 RPG UI 和 Windows 95。点击时斜面翻转,产生按下的手感。扁平样式(ghostlinkdash)和禁用按钮没有斜面效果。

自定义标签

使用 tag 属性来自定义按钮的 HTML 标签。

<template>
  <div class="row">
    <px-button>button</px-button>
    <px-button tag="div" role="button" tabindex="0">div</px-button>
    <px-button
      type="primary"
      tag="a"
      href="https://github.com/SaKaNa-Y/sakana-element"
      target="_blank"
      rel="noopener noreferrer"
    >
      a
    </px-button>
  </div>
</template>

<style scoped>
.row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 10px;
}
</style>

节流模式

使用 use-throttlethrottle-duration 属性来启用节流模式。

<script setup>
function handleBtnClick() {}
</script>

<template>
  <div class="row">
    <px-button @click="handleBtnClick">with throttle</px-button>
    <px-button :use-throttle="false" @click="handleBtnClick"
      >without throttle</px-button
    >
  </div>
</template>

<style scoped>
.row {
  display: flex;
  align-items: center;
  gap: 12px;
}
</style>