跳到主要内容

Button 按钮

按钮用于触发操作。通过 variant 表达操作层级,支持不同尺寸、图标、加载和禁用状态。

代码演示

Button · 变体
Button · 尺寸
状态
已触发 0

用法

import { Button } from '@unif/react-native-design';

<Button label="确认" variant="primary" onPress={handleConfirm} />
<Button label="取消" variant="secondary" onPress={handleCancel} />
<Button label="查看详情" variant="outline" onPress={openDetails} />
<Button label="重新发起" variant="text" size="sm" onPress={restart} />
<Button label="提交" variant="primary" block size="lg" onPress={submit} />

API

参数类型默认值说明
labelstring按钮文字,同时作为业务名称;去除首尾空白后不能为空;空白时不响应点击并提示开发诊断
onPress() => void必填点击回调;disabled / loading 时组件移除有效 handler
size('sm' | 'md' | 'lg')?'md'高度 28 / 36 / 44
variant('primary' | 'secondary' | 'ghost' | 'neutral' | 'outline' | 'danger' | 'text')?'primary'视觉变体
blockboolean?false仅撑满父容器交叉轴(alignSelf: stretch);主轴尺寸由父容器或 style 决定
disabledboolean?false禁用(opacity 0.5 + 不响应 onPress)
loadingboolean?false加载态:用 ActivityIndicator 替代 label,自动 disabled
leftIconIconName?左侧图标,与文本同色;尺寸由 Button size 固定,不随 fontScale 放大
rightIconIconName?右侧图标,与文本同色;尺寸由 Button size 固定,不随 fontScale 放大
styleStyleProp<ViewStyle>?额外样式覆盖(merge 到末尾)
testIDstring?E2E / 测试定位
accessibilityHintstring?SR 行为说明 hint,仅在「行为不显然」时加
accessibilityStateOmit<AccessibilityState, 'disabled' | 'busy'>?caller 可补充 selected / expanded 等状态;disabled / busy 由组件接管,类型上禁止覆盖

无障碍(a11y)

来源:src/components/ui/Button/ButtonBase.tsxButton.tsxtypes.ts

  • 默认 accessibilityRole'button'(在 ButtonBase 中硬编码,accessibilityRole = 'button')。
  • a11y props:accessibilityLabel 自动取 trim 后非空的必填 label,无需另传;accessibilityHint 可选,仅在「行为不显然」时补充(如「切换主题」)。空白 label 不会留下 unnamed button:组件移除 handler/action 语义,并在 effect 中诊断。
  • 状态语义:disabled loading 任一为真时都会上报 accessibilityState.disabled: true 并移除有效 handler;loading 还上报 busy: true(此时 label 被 ActivityIndicator 替换)。调用方传入的状态不能覆盖这两个字段。
// label 即 a11y label;hint 仅在行为不显然时补
<Button label="确认" variant="primary" onPress={handleConfirm} />
<Button label="切换" accessibilityHint="切换深浅主题" onPress={toggleTheme} />

主题键(Tokens)

读取来源:src/components/ui/Button/styles.tsButtonBase.tsx

Token来源variant / 说明
c.primaryuseColors()primary 变体背景色;ghost / text 变体文字色
c.onPrimaryuseColors()primary 变体文字/图标色
c.surfaceContainerHighuseColors()secondary 变体背景色
c.foregrounduseColors()secondary / neutral / outline 变体文字色
c.surfaceuseColors()outline 变体背景色
c.outlineuseColors()outline 变体边框色
c.erroruseColors()danger 变体背景色
c.onErroruseColors()danger 变体文字色
radius.lg / radius.md / radius.sm静态 tokensize='lg'/'md'/'sm' 对应圆角
control.lg / control.md / control.sm静态 token(src/theme按尺寸对应高度(44 / 36 / 28)
space['6'] / space['4']静态 tokenmd / sm 水平内边距(lgr(18))
space['2'] / space['1']静态 token内容 gap(lg/md'2',sm'1')
fw.semi静态 token按钮文字字重(600
type.body / type.sm / type.xxs静态 tokensize='lg'/'md'/'sm' 对应字号

FAQ

字号变大时,按钮尺寸也会变大吗?

ThemeProvider fontScale 只把 Button label 字号缩放一次。Button 高度、水平 padding、gap、圆角和左右 Icon 尺寸仍由原始 size token 决定,不随字号档位改变。例如 fontScale={1.5} 会放大文字,但 md 仍保持 control.md 的原有几何,Icon 仍使用未缩放的 type.sm + 2