TDesign Button 按钮组件(Uniapp)完整指南:引入方式、属性 API、事件与样式定制

原创2026-10-15 15:15:101,731 阅读
文章标签:UI组件设计系统小程序前端移动开发跨平台

TDesign Button 按钮组件(Uniapp)完整指南:引入方式、属性 API、事件与样式定制

TDesign 的 Button 按钮组件用于开启一个闭环的操作任务,例如“删除”对象、“购买”商品等,是表单提交、权限授权、客服会话等场景中最常用的基础组件之一。本文以 Button 按钮官方文档 为主体,结合本仓库 uniapp-components 版源码 与 示例代码,系统讲解 TDesign Button 在 Uniapp 项目中的引入方式、组件类型、状态与样式、完整 Props 与 Events API、插槽与外部样式类,以及基于 CSS Variables 的主题定制方法。读完本文,你将能够独立完成按钮组件的选型、配置、事件处理与视觉定制。

引入方式

推荐使用 easycom 模式引入组件,配置后无需手动 import 即可直接在模板中使用 <t-button />。详细配置方式可参考 快速开始(本仓库中各组件文档均指向该快速开始指引)。

如需手动引入,可按如下方式导入:

import TButton from '@tdesign/uniapp/button/button.vue';

引入后在页面或组件的 components 中注册即可使用。示例代码(base/index.vue)展示了标准的引入与使用流程:

<script>
import TButton from '@tdesign/uniapp/button/button.vue';

export default {
  components: {
    TButton,
  },
  // ...
};
</script>

组件类型

按钮组件提供了基础按钮、图标按钮、幽灵按钮、组合按钮、通栏按钮五种常见形态,覆盖从常规操作到强调型操作的大部分业务场景。

基础按钮

基础按钮是使用频率最高的形态,通过 theme 区分视觉强调程度,通过 variant 切换填充/描边/虚框/文字等表现形式。参考 基础按钮示例:

<t-button theme="primary" size="large"> 填充按钮 </t-button>
<t-button theme="light" size="large"> 填充按钮 </t-button>
<t-button size="large"> 填充按钮 </t-button>

<!-- 同一主题下的不同 variant -->
<t-button theme="primary" size="large" variant="outline"> 描边按钮 </t-button>
<t-button theme="primary" size="large" variant="dashed"> 虚框按钮 </t-button>
<t-button theme="primary" size="large" variant="text"> 文字按钮 </t-button>

其中 variant 可选值包括 base(基础填充)、outline(线框)、dashed(虚线)、text(文字)四种,默认值为 base。

图标按钮

图标按钮在按钮内渲染 t-icon 图标,图标名称通过 icon 属性传入。参考 图标按钮示例:

<t-button theme="primary" icon="app" content="填充按钮" size="large" />
<t-button theme="primary" icon="search" shape="square" size="large" aria-label="搜索" />

icon 属性支持两种取值:

  • 字符串:直接表示图标名称,如 icon="search";
  • 对象:透传至 t-icon 组件,可携带 name、size、color、prefix、style 等配置。

从 组件源码 看,icon 值会通过 calcIcon(value, '') 被解析为内部 innerIcon 对象,并据此渲染 <t-icon> 子组件;calcIcon 的实现在 common/utils.js:当传入字符串时直接作为图标名,传入对象时原样透传,二者都不满足时返回 null 不渲染图标。

图标尺寸会跟随按钮尺寸自动适配:extra-small 与 small 为 36rpx,medium 为 40rpx,large 为 48rpx(见 button.vue 中的映射)。图标按钮在 shape="circle" 等场景下还常配合 aria-label 提供无障碍描述。

幽灵按钮

幽灵按钮为镂空(透明背景)样式,常用于深色背景或 Banner 场景。参考 幽灵按钮示例,该示例背景为深色(#181818):

<t-button theme="primary" ghost size="large"> 幽灵按钮 </t-button>
<t-button theme="danger" ghost size="large"> 幽灵按钮 </t-button>
<t-button ghost size="large"> 幽灵按钮 </t-button>

ghost 默认值为 false,开启后背景透明、边框与文字使用对应主题色。在样式实现(button.less)中,幽灵按钮的优先级高于 theme,固定透明背景,并分别为 default、primary、danger 提供了独立的边框色与 hover 色变量(如 --td-button-ghost-primary-color、--td-button-ghost-danger-color)。

组合按钮

组合按钮通过多个 block 按钮横向拼接实现,常用于“上一步/下一步”等步骤型操作。参考 组合按钮示例:

<t-button theme="light" block size="large" :custom-style="'flex: 1; margin-right: 16px;'">
  填充按钮
</t-button>
<t-button theme="primary" block size="large" :custom-style="'flex: 1;'">
  填充按钮
</t-button>

要点:

  • 每个子按钮都设置 block 使宽度撑满父容器,再通过 flex: 1 均分宽度;
  • 使用 margin-right 制造按钮间距;
  • 示例中还通过 canUseVirtualHost() 判断环境是否支持虚拟宿主,以决定样式应写在 custom-style 还是原生 style 上。

通栏按钮

通栏按钮即宽度为 100% 的块级按钮,仅需设置 block 属性。参考 通栏按钮示例:

<t-button theme="primary" size="large" block> 填充按钮 </t-button>

样式层面对应 button.less 中的 &--block { display: flex; width: 100%; } 规则。

组件状态

按钮禁用态

通过 disabled 属性开启禁用态,禁用后按钮不可点击、视觉变为灰色并应用禁用配色。参考 禁用态示例:

<t-button theme="primary" size="large" disabled> 填充按钮 </t-button>
<t-button theme="primary" size="large" variant="outline" disabled> 描边按钮 </t-button>
<t-button theme="primary" size="large" variant="text" disabled> 文字按钮 </t-button>

关于 disabled 需要了解以下几点:

  • 默认值为 undefined,与表单组件配合时存在优先级规则:Button.disabled > Form.disabled(见 props.ts 的注释);
  • 禁用或加载状态下,open-type、form-type 都会被置空,hover-class 点击态也不会生效(见 button.vue);
  • handleTap 中也会显式拦截:if (this.disabled || this.loading) return;(见 button.vue),确保禁用态不会派发 click 事件;
  • 每个主题(default/primary/light/danger)及每种 variant 都通过 CSS 变量定义了独立的禁用配色,例如 --td-button-primary-disabled-bg、--td-button-danger-disabled-color 等。

加载状态

loading 属性可将按钮切换为加载状态,此时按钮内部渲染 t-loading 组件,且同样拦截点击事件。参考 图标按钮示例 中的用法:

<t-button theme="primary" size="large" loading> 加载中 </t-button>

loading 相关行为:

  • 加载状态下 open-type、form-type 同样会被置空;
  • loading-props 属性可透传 Loading 组件全部属性,其 TS 类型为 LoadingProps(见 type.ts),可配置 delay、duration、indicator、size、theme、text 等(见 button.vue);
  • 加载指示器默认尺寸为 40rpx、主题为 circular;
  • 从 Skyline 渲染示例(_example/skyline/button.vue)可以看到,在 Skyline 渲染下可自定义 <t-loading theme="spinner"> 展示“加载中...”文案,作为 loading 内置加载态的替代方案。

组件样式

按钮尺寸

size 控制按钮尺寸,可选 extra-small / small / medium / large,默认 medium。各尺寸对应的高度与字号如下表(数据来自 button.less):

尺寸 高度 水平内边距 图标尺寸 字号
extra-small 56rpx 16rpx 36rpx 中等(@font-mark-medium)
small 64rpx 24rpx 36rpx 中等(@font-mark-medium)
medium 80rpx 32rpx 40rpx 大(@font-mark-large)
large 96rpx 40rpx 48rpx 大(@font-mark-large)

参考 尺寸示例:

<t-button theme="primary" size="large"> 按钮48 </t-button>
<t-button theme="primary"> 按钮40 </t-button>
<t-button theme="primary" size="small"> 按钮32 </t-button>
<t-button theme="primary" size="extra-small"> 按钮28 </t-button>

示例中“按钮48/按钮40/按钮32/按钮28”即对应各尺寸的高度(rpx)。

按钮形状

shape 控制按钮形状,可选 rectangle(长方形,默认)/ square(正方形)/ round(圆角长方形)/ circle(圆形)。参考 形状示例:

<t-button theme="primary" size="large"> 填充按钮 </t-button>
<t-button theme="primary" size="large" icon="search" shape="square" aria-label="搜索" />
<t-button theme="primary" size="large" shape="round"> 填充按钮 </t-button>
<t-button theme="primary" size="large" icon="search" shape="circle" aria-label="搜索" />

各形状的样式实现(button.less):

  • round:圆角半径取当前尺寸高度的一半(calc(@button-*-height / 2)),且会同步调整 ::after 伪元素圆角;
  • square:padding: 0,宽高相等,宽度取当前尺寸高度值;
  • circle:border-radius: 50%,宽高相等,适合纯图标按钮,此时务必配合 aria-label 提升无障碍可访问性。

按钮主题

theme 控制按钮主题色,可选 default / primary / danger / light,默认 default,其中 primary 为品牌色、danger 为危险色。参考 主题示例,每个主题均可与 variant 自由组合:

<!-- default -->
<t-button size="large"> 填充按钮 </t-button>
<t-button size="large" variant="outline"> 描边按钮 </t-button>
<t-button size="large" variant="text"> 文字按钮 </t-button>

<!-- primary -->
<t-button size="large" theme="primary"> 填充按钮 </t-button>
<t-button size="large" theme="primary" variant="outline"> 描边按钮 </t-button>
<t-button size="large" theme="primary" variant="text"> 文字按钮 </t-button>

<!-- danger -->
<t-button size="large" theme="danger"> 填充按钮 </t-button>
<t-button size="large" theme="danger" variant="outline"> 描边按钮 </t-button>
<t-button size="large" theme="danger" variant="text"> 文字按钮 </t-button>

<!-- light -->
<t-button size="large" theme="light"> 填充按钮 </t-button>
<t-button size="large" theme="light" variant="outline"> 描边按钮 </t-button>
<t-button size="large" theme="light" variant="text"> 文字按钮 </t-button>

类名生成逻辑见 button.vue:setClass 会根据 variant、theme、shape、size 以及 block / disabled / ghost 布尔值拼装出形如 t-button--primary t-button--base t-button--rectangle t-button--size-large 的类名;而 button.less 中的 button-theme mixin 为每个主题统一生成默认色、激活态(hover)色与禁用态配色规则。

API 详解

Button Props

组件全部属性及其说明如下(默认值与类型均以 props.ts 与 type.ts 为准):

名称 类型 默认值 说明 必传
custom-style Object - 自定义样式 N
activity-type Number - 卡片 id。open-type 设置为 liveActivity 时使用,可设置 notify_type 参数;用户点击按钮后可通过 createliveactivity 事件回调获取 code N
app-parameter String '' 打开 APP 时向 APP 传递的参数,open-type=launchApp 时有效 N
block Boolean false 是否为块级元素(宽度 100%) N
content String - 按钮内容 N
custom-dataset String / Number / Boolean / Object / Array {} 自定义 dataset,可通过 event.currentTarget.dataset.custom 获取 N
disabled Boolean undefined 禁用状态。优先级:Button.disabled > Form.disabled N
entrance-path String '' 从消息小程序入口打开小程序的路径,默认为聊天工具启动路径 N
ghost Boolean false 是否为幽灵按钮(镂空按钮) N
hover-class String '' 指定按钮按下去的样式类,按钮不为加载或禁用状态时有效;当 hover-class="none" 时没有点击态效果 N
hover-start-time Number 20 按住后多久出现点击态,单位毫秒 N
hover-stay-time Number 70 手指松开后点击态保留时间,单位毫秒 N
hover-stop-propagation Boolean false 是否阻止本节点的祖先节点出现点击态 N
icon String / Object - 图标名称;字符串表示图标名称,Object 表示透传至 icon 组件 N
lang String - 指定返回用户信息的语言:en 英文 / zh_CN 简体中文 / zh_TW 繁体中文 N
loading Boolean false 是否显示为加载状态 N
loading-props Object {} 透传 Loading 组件全部属性,TS 类型为 LoadingProps N
need-show-entrance Boolean true 转发的文本消息是否要带小程序入口 N
open-type String - 微信开放能力,可选 contact / share / getPhoneNumber / getUserInfo / launchApp / openSetting / feedback / chooseAvatar / agreePrivacyAuthorization N
phone-number-no-quota-toast Boolean true 原生按钮属性,手机号快速验证或实时验证额度用尽时是否展示“暂时无法使用”提示,open-type="getPhoneNumber" 或 getRealtimePhoneNumber 时有效 N
send-message-img String 截图 会话内消息卡片图片,open-type="contact" 时有效 N
send-message-path String 当前分享路径 会话内消息卡片点击跳转小程序路径,open-type="contact" 时有效 N
send-message-title String 当前标题 会话内消息卡片标题,open-type="contact" 时有效 N
session-from String '' 会话来源,open-type="contact" 时有效 N
shape String rectangle 按钮形状,可选 rectangle / square / round / circle N
show-message-card Boolean false 是否显示会话内消息卡片,open-type="contact" 时有效 N
size String medium 组件尺寸,可选 extra-small / small / medium / large N
t-id String '' 按钮标签 id N
theme String default 组件风格,可选 default / primary / danger / light N
type String - 同小程序的 formType,可选 submit / reset N
variant String base 按钮形式,可选 base / outline / dashed / text N

值得注意的实现细节:

  • 多项枚举属性(lang、open-type、shape、size、theme、type、variant)在 props.ts 中都带有 validator 校验函数,传入非法值时会被拦截;
  • icon、size、theme、variant、block、disabled、loading、shape、ghost 均被 watch 监听并触发 setClass 重建类名(见 button.vue),因此动态切换这些属性会实时反映到样式上;
  • 按钮根元素会通过 :data-custom 注入 customDataset,事件回调中可经由 event.currentTarget.dataset.custom 取出。

open-type 微信开放能力详解

open-type 对接微信原生按钮的开放能力,各取值含义如下(详见 props.ts):

  • contact:打开客服会话;用户在会话中点击消息卡片返回小程序时,可从 bindcontact 回调获取信息(鸿蒙 OS 暂不支持);
  • liveActivity:通过前端获取新的一次性订阅消息下发机制使用的 code,需要配合 activity-type 使用;
  • share:触发用户转发;
  • getPhoneNumber:获取用户手机号,从 bindgetphonenumber 回调获取用户信息(小程序插件中不能使用);
  • getUserInfo:获取用户信息,从 bindgetuserinfo 回调获取(小程序插件中不能使用);
  • launchApp:打开 APP,可通过 app-parameter 属性传参;
  • openSetting:打开授权设置页;
  • feedback:打开“意见反馈”页面;
  • chooseAvatar:获取用户头像,从 bindchooseavatar 回调获取头像信息;
  • agreePrivacyAuthorization:用户同意隐私协议按钮,点击一次后所有隐私接口可正常调用,可通过 bindagreeprivacyauthorization 监听同意事件。

这些开放能力事件由 button.vue 在原生 <button> 上一一绑定并向上转发。

Button Events

组件事件列表如下:

名称 参数 描述
agreeprivacyauthorization - 用户同意隐私协议事件回调,open-type=agreePrivacyAuthorization 时有效(如使用 onNeedPrivacyAuthorization 接口,需在触发后再调用 resolve({ event: "agree", buttonId }))
chooseavatar - 获取用户头像回调,open-type=chooseAvatar 时有效,e.detail.avatarUrl 为头像临时文件链接
click (e: MouseEvent) 点击时触发
contact - 客服消息回调,open-type="contact" 时有效
createliveactivity - 新的一次性订阅消息下发机制回调,open-type=liveActivity 时有效
error - 使用开放能力发生错误时的回调,open-type=launchApp 时有效
getphonenumber - 手机号快速验证回调,open-type=getPhoneNumber 时有效。提示:触发回调后应立即隐藏按钮或置为 disabled,避免用户重复授权产生额外费用
getrealtimephonenumber - 手机号实时验证回调,open-type=getRealtimePhoneNumber 时有效,处理方式同上
getuserinfo - 用户点击后返回获取到的用户信息,回调 detail 数据与 wx.getUserInfo 返回一致,open-type="getUserInfo" 时有效
launchapp - 打开 APP 成功的回调,open-type=launchApp 时有效
opensetting - 打开授权设置页后的回调,open-type=openSetting 时有效

需要说明的是,getrealtimephonenumber 与 phoneoneclicklogin 等事件在 button.vue 中同样被监听并转发,属于对原生按钮能力的完整透传。

Button Slots

名称 描述
默认插槽(-) 按钮内容,作用同 content 插槽
content 自定义 content 显示内容
suffix 右侧内容,可用于定义右侧图标

在 button.vue 中,内容区按 <slot name="content"> → content 文本 → 默认插槽 <slot /> 的顺序渲染,且当同时存在图标/加载与文本时,会自动追加 --content--has-icon 类以控制间距。

Button External Classes

外部样式类用于在页面中覆写组件内部节点样式:

类名 描述
t-class 根节点样式类
t-class-icon 图标样式类
t-class-loading 加载样式类

外部类通过 button.vue 中的 externalClasses 声明为 t-class、t-class-icon、t-class-loading 三项。组合按钮示例与形状示例中均用到了 t-class 来追加自定义类;图标与加载指示器分别接收 t-class-icon、t-class-loading。

CSS Variables

组件提供了完整的 CSS 变量体系用于自定义样式,所有变量均可被外部覆盖。以下按主题、形态与尺寸分组展示(默认值取自 button.less 与 README.md):

通用变量

名称 默认值
--td-button-border-radius @radius-default
--td-button-border-width 4rpx
--td-button-icon-border-radius 8rpx
--td-button-icon-spacer @spacer

default 主题

名称 默认值
--td-button-default-color @text-color-primary
--td-button-default-bg-color @bg-color-component
--td-button-default-border-color @bg-color-component
--td-button-default-disabled-bg @bg-color-component-disabled
--td-button-default-disabled-border-color @bg-color-component-disabled
--td-button-default-disabled-color @text-color-disabled
--td-button-default-active-bg-color @bg-color-component-active
--td-button-default-active-border-color @bg-color-component-active
--td-button-default-outline-color @text-color-primary
--td-button-default-outline-border-color @component-border
--td-button-default-outline-disabled-color @component-border
--td-button-default-text-active-bg-color @bg-color-container-active

primary 主题

名称 默认值
--td-button-primary-color @text-color-anti
--td-button-primary-bg-color @brand-color
--td-button-primary-border-color @brand-color
--td-button-primary-disabled-bg @brand-color-disabled
--td-button-primary-disabled-border-color @brand-color-disabled
--td-button-primary-disabled-color @text-color-anti
--td-button-primary-active-bg-color @brand-color-active
--td-button-primary-active-border-color @brand-color-active
--td-button-primary-outline-color @brand-color
--td-button-primary-outline-border-color @button-primary-outline-color
--td-button-primary-outline-disabled-color @brand-color-disabled
--td-button-primary-text-color @brand-color
--td-button-primary-text-disabled-color @brand-color-disabled
--td-button-primary-dashed-color @brand-color
--td-button-primary-dashed-border-color @button-primary-dashed-color
--td-button-primary-dashed-disabled-color @brand-color-disabled

light 主题

名称 默认值
--td-button-light-color @brand-color
--td-button-light-bg-color @brand-color-light
--td-button-light-border-color @brand-color-light
--td-button-light-disabled-bg @brand-color-light
--td-button-light-disabled-border-color @brand-color-light
--td-button-light-disabled-color @brand-color-disabled
--td-button-light-active-bg-color @brand-color-light-active
--td-button-light-active-border-color @brand-color-light-active
--td-button-light-outline-color @brand-color
--td-button-light-outline-bg-color @brand-color-light
--td-button-light-outline-border-color @button-light-outline-color
--td-button-light-outline-disabled-color @brand-color-disabled
--td-button-light-text-color @brand-color
--td-button-light-text-active-bg-color @bg-color-container-active

danger 主题

名称 默认值
--td-button-danger-color @text-color-anti
--td-button-danger-bg-color @error-color
--td-button-danger-border-color @error-color
--td-button-danger-disabled-bg @error-color-3
--td-button-danger-disabled-border-color @error-color-3
--td-button-danger-disabled-color @font-white-1
--td-button-danger-active-bg-color @error-color-active
--td-button-danger-active-border-color @error-color-active
--td-button-danger-outline-color @error-color
--td-button-danger-outline-border-color @button-danger-outline-color
--td-button-danger-outline-disabled-color @error-color-3
--td-button-danger-text-color @error-color
--td-button-danger-text-disabled-color @button-danger-disabled-color
--td-button-danger-dashed-color @error-color
--td-button-danger-dashed-border-color @button-danger-dashed-color
--td-button-danger-dashed-disabled-color @button-danger-disabled-color

ghost 相关变量

名称 默认值
--td-button-ghost-color @text-color-anti
--td-button-ghost-border-color @button-ghost-color
--td-button-ghost-disabled-color @font-white-4
--td-button-ghost-hover-color @font-white-2
--td-button-ghost-primary-color @brand-color
--td-button-ghost-primary-border-color @brand-color
--td-button-ghost-primary-hover-color @brand-color-active
--td-button-ghost-danger-color @error-color
--td-button-ghost-danger-border-color @error-color
--td-button-ghost-danger-hover-color @error-color-active

尺寸变量

名称 默认值
--td-button-extra-small-font @font-mark-medium
--td-button-extra-small-height 56rpx
--td-button-extra-small-padding-horizontal 16rpx
--td-button-extra-small-icon-size 36rpx
--td-button-small-font @font-mark-medium
--td-button-small-height 64rpx
--td-button-small-padding-horizontal 24rpx
--td-button-small-icon-size 36rpx
--td-button-medium-font @font-mark-large
--td-button-medium-height 80rpx
--td-button-medium-padding-horizontal 32rpx
--td-button-medium-icon-size 40rpx
--td-button-large-font @font-mark-large
--td-button-large-height 96rpx
--td-button-large-padding-horizontal 40rpx
--td-button-large-icon-size 48rpx

这些变量与 button.less 顶部的 var(--td-button-*, fallback) 声明一一对应,因此你可以在任意位置直接覆写同名 CSS 变量来改变按钮外观,例如全局统一按钮高度或品牌色:

:root {
  --td-button-primary-bg-color: #0052d9;
  --td-button-large-height: 104rpx;
}

组件实现原理速览

从 button.vue 可以提炼出组件的核心实现思路,方便排查问题与二次开发:

  1. 透传原生能力:组件根节点是原生 <button>,通过 :form-type、:open-type、:hover-*、:lang、客服卡片系列(send-message-*、session-from、show-message-card)、app-parameter、entrance-path 等属性将配置原样透传给小程序原生按钮,最大化复用平台能力。
  2. 条件透传:当 disabled 或 loading 为真时,form-type、open-type 会被置为空字符串,防止禁用/加载状态误触发生开放能力调用。
  3. 内部子组件:图标态使用 t-icon,加载态使用 t-loading,二者均接收独立的样式类(t-class-icon、t-class-loading)以便外部覆写。
  4. 样式变量驱动:全部颜色、尺寸均通过 CSS 变量提供默认值(见 button.less),支持在页面级或全局覆盖,无需修改组件源码。
  5. 多端适配:源码中针对 QQ 小程序(MP-QQ)补充了 outline 与 ghost 的实线边框规则,针对 H5 与微信小程序(MP-WEIXIN)补充了图标/加载与内容之间的间距规则(见 button.vue),保证不同端渲染一致。

结语

TDesign Button 按钮组件在保持原生 <button> 全部能力(客服、转发、手机号、头像、隐私授权等开放能力)的同时,通过 variant × theme × shape × size 的组合体系覆盖了从基础操作到强调型操作的绝大多数业务场景,并提供插槽、外部样式类与全套 CSS 变量三种定制途径。配合 组件源码、属性定义、类型定义 与 示例集合,开发者可以快速将其应用到实际的 Uniapp / 小程序项目中,并灵活适配品牌视觉体系。

登录后查看全文
tdesign-miniprogram