触发一个动作。主按钮一屏一两个,给当前视图的主要提交;其余用次按钮、描边或幽灵。
状态:stable · 组件:Button
引入
定位到「引入」import { Button } from 'nacre'用法
定位到「用法」import { Text, View, signal } from 'nsh'import { Button, TEXT_SIZE } from 'nacre'
export function ButtonUsage() { const count = signal(0) return ( <View class="flex-row items-center gap-2"> <Button variant="primary" onPress={() => count.update((n) => n + 1)}> 保存 </Button> <Text class={`${TEXT_SIZE.small} text-muted-foreground tabular-nums`}>{count()}</Text> </View> )}示例
定位到「示例」外观
定位到「外观」primary 是唯一的实心彩色按钮,只给关键动作;secondary 是默认;outline 凸起;ghost / subtle 用在工具栏与行内;destructive 浅红底红字;link 只给外部链接。
import { View } from 'nsh'import { Button } from 'nacre'
export function ButtonVariants() { return ( <View class="flex-row flex-wrap items-center gap-2"> <Button variant="primary">主按钮</Button> <Button variant="secondary">次按钮</Button> <Button variant="outline">描边</Button> <Button variant="ghost">幽灵</Button> <Button variant="subtle">次要</Button> <Button variant="destructive">删除</Button> <Button variant="link">链接</Button> </View> )}色调
定位到「色调」tone 只改颜色语义、不改形状:warning 浅黄底深黄字(话后小记这类要尽快处理的动作);danger 配 primary 是红色实底(挂断),配其它变体同 destructive。只有这两种,不开放任意颜色类。
import { View } from 'nsh'import { Button } from 'nacre'
export function ButtonTones() { return ( <View class="flex-row flex-wrap items-center gap-2"> <Button tone="warning" icon="pencil">写小记 00:42</Button> <Button tone="danger" variant="primary" size="md" icon="phone">挂断</Button> <Button tone="danger" icon="refresh">重新连接</Button> </View> )}尺寸
定位到「尺寸」xs / sm(默认)/ md / lg。数字是当前风格和平台下的实际高度,同一行的控件用同一档。
import { View, token } from 'nsh'import { Button } from 'nacre'
export function ButtonSizes() { return ( <View class="flex-row flex-wrap items-center gap-2"> {(['xs', 'sm', 'md', 'lg'] as const).map((size) => ( <Button size={size}>{`${size} ${Number(token(`control-h-${size}`))}`}</Button> ))} </View> )}带图标
定位到「带图标」icon 放文字前,trailingIcon 放文字后(展开类的 chevron)。图标尺寸跟尺寸档。
import { View } from 'nsh'import { Button } from 'nacre'
export function ButtonWithIcon() { return ( <View class="flex-row flex-wrap items-center gap-2"> <Button variant="primary" icon="plus"> 新增分类 </Button> <Button variant="outline" icon="export" trailingIcon="chevron-down"> 导出 </Button> <Button variant="secondary" icon="sparkles"> AI 填写 </Button> </View> )}自带图标
定位到「自带图标」icon 也可以是渲染函数:按钮把该用的 size 与 color(跟尺寸档、变体与色调)交给它,放应用自己的图标集。children 里可以再嵌一段 Text(计时器淡一档)。文字一律等宽数字:计时、倒计时每秒在变,比例数字会让按钮宽度和文字位置跟着跳;被 class 收窄(flex-1 min-w-0)时截断,而不是把一行撑破、把旁边的控件挤出去。align=“start” 让被撑宽的按钮内容靠左,文字长短变化时起点不动。
import { Text, View } from 'nsh'import { Button, Icon, TEXT_SIZE } from 'nacre'
export function ButtonCustomIcon() { return ( <View class="w-[280px] max-w-full gap-2"> <Button variant="primary" size="md" icon={(state) => ( <View class="items-center justify-center"> <Icon name="phone" size={state.size} color={state.color} /> <View class="absolute -top-0.5 -right-0.5 w-1.5 h-1.5 rounded-full bg-primary-foreground" /> </View> )} > 接听 <Text class={`${TEXT_SIZE.small} text-primary-foreground`}>00:12</Text> </Button> <Button tone="warning" align="start" testID="button-align-start" icon={(state) => <Icon name="pencil" size={state.size} color={state.color} />}> 话后处理 01:29 </Button> <Button tone="warning" align="start" class="min-w-0" icon={(state) => <Icon name="pencil" size={state.size} color={state.color} />}> 话后处理 01:29 · 回到这通电话写服务小记 </Button> </View> )}提交中
定位到「提交中」loading 时文字前转 loader,期间不可再点;按钮宽度不变。
import { View, signal } from 'nsh'import { Button } from 'nacre'
export function ButtonLoading() { const busy = signal(false) const save = () => { busy.set(true) setTimeout(() => busy.set(false), 1500) } return ( <View class="flex-row items-center gap-2"> <Button variant="primary" loading={busy()} onPress={save}> 保存 </Button> <Button variant="outline" loading> 同步中 </Button> </View> )}禁用
定位到「禁用」禁用的按钮不响应悬停,提示出不来:为什么不能点要写在旁边(Dialog 的 footerHint)。
手机:禁用的按钮压到 40% 且不响应点按;为什么不能点写在旁边的说明里。
import { View } from 'nsh'import { Button } from 'nacre'
export function ButtonDisabled() { return ( <View class="flex-row flex-wrap items-center gap-2"> <Button variant="primary" disabled> 主按钮 </Button> <Button disabled>次按钮</Button> <Button variant="outline" disabled> 描边 </Button> </View> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| Tab | 聚焦到按钮,显示焦点环 |
| Space / Return | 按下 |
无障碍
定位到「无障碍」- 读屏名是按钮文字;只有图标时用 IconButton(label 必填)。
- loading 读作 busy;展开类按钮传 expanded,读屏会说「已展开 / 已收起」。
- 禁用的按钮不进 Tab 序列。
该用 / 不该用
定位到「该用 / 不该用」该用
- 每个视图 1–2 个主按钮
- 危险操作用 destructive,并二次确认
- 按钮文字写动作(「保存」「转派」)
不该用
- 用 primary 做普通操作
- 用 link 做页面内动作
- 同一行混用不同尺寸档
API
定位到「API」Button
定位到「Button」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
variant |
ButtonVariant |
'secondary' |
按钮外观,默认 secondary,主要操作用 primary。 |
size |
ButtonSize |
'sm' |
尺寸档,默认 sm;inline 用来嵌在一行文字内。 |
tone |
ButtonTone |
— | 语义色调,只改颜色、不改形状;只认语义,不开放任意颜色类 |
disabled |
boolean |
— | 禁用后停止响应按压,并向读屏报告禁用状态。 |
loading |
boolean |
— | 提交中:文字前转 loader,期间不可再点(components.md Button「载」) |
onPress |
(event: PressEvent) => void |
— | 带按下事件(读修饰键:⌥ 点击) |
round |
boolean |
— | 胶囊(全圆角) |
testID |
string |
— | 按钮根节点的测试标识。 |
labelTestID |
string |
— | 文字自己的 testID(测试读按钮上的动态文字) |
accessibilityLabel |
string |
— | 辅助技术读出的动作名称。 |
class |
string |
— | 补充宽度、伸缩等布局类。 |
align |
'center' | 'start' |
— | 内容怎么排:center(默认);start 靠左,给被 class 撑宽(flex-1)、文字是一句话的按钮(话后小记倒计时),居中会让文字随长度左右漂 |
icon |
IconName | IconRender |
— | 文字前的图标:库图标名,或自带图标的渲染函数(应用自己的图标集,拿到尺寸与前景色);提交中换成 loader |
trailingIcon |
IconName |
— | 文字后的图标(展开的 chevron 等) |
iconColor |
ColorToken |
— | 图标颜色,默认跟变体的前景色 |
tooltip |
TooltipContent | null |
— | 悬停提示(文字按钮一般不需要;被禁用时说明原因可以用) |
shortcut |
string | null |
— | 快捷键,排在提示右侧 |
expanded |
boolean |
— | 控制展开的按钮(展开 / 收起)报给读屏的展开态 |
viewRef |
(node: ViewRef) => void |
— | 根节点(交还焦点用) |
pressed |
boolean |
— | 打开 / 按下态:它打开的菜单开着时底色常亮(component-standard.md §3 打开) |
children |
JSX.Element |
— | 按钮中展示的文字或自定义内容。 |
* 必填