工具栏里的图标 + 文字按钮(输入区工具栏、通话控制):可只留图标、有切换态、文字后带快捷键提示。只有图标且不在工具栏里时用 IconButton;独立的提交动作用 Button。
手机:工具栏里的图标 + 文字按钮(输入区工具栏、通话控制):可只留图标、有切换态;手机上不显示快捷键。
状态:experimental · 组件:ToolbarButton
引入
定位到「引入」import { ToolbarButton } from 'nacre'用法
定位到「用法」import { View, signal } from 'nsh'import { ToolbarButton } from 'nacre'
export function ToolbarButtonUsage() { const translating = signal(false) return ( <View class="flex-row flex-wrap items-center gap-0.5"> <ToolbarButton icon="quote" label="快捷语" shortcut="⌘/" /> <ToolbarButton icon="translate" label="自动翻译" pressed={translating()} onPress={() => translating.update((v) => !v)} /> <ToolbarButton icon="sparkles" label="写作助手" /> </View> )}示例
定位到「示例」只留图标
定位到「只留图标」compact 只显示图标,label 退为读屏名与悬停提示;工具栏变窄时整排一起切 compact。shortcut 在只图标时只出现在悬停提示里。
手机:compact 只显示图标,label 退为读屏名;工具栏变窄时整排一起切 compact。
import { View, signal } from 'nsh'import { ToolbarButton } from 'nacre'
export function ToolbarButtonCompact() { const quoted = signal(true) return ( <View class="flex-row flex-wrap items-center gap-0.5"> <ToolbarButton compact icon="quote" label="引用" pressed={quoted()} onPress={() => quoted.update((v) => !v)} /> <ToolbarButton compact icon="pencil" label="编辑" /> <ToolbarButton compact icon="quote" label="快捷语" shortcut="⌘/" /> <ToolbarButton compact icon="eraser" label="清除格式" /> </View> )}状态
定位到「状态」切换类(自动翻译、加粗)用 pressed,读屏报「已按下」;弹浮层的触发器同时传 expanded(报展开态,按下不缩放)。tooltip 可以和 label 不同(说明当前为什么不可用),传 null 不出提示。
import { Text, View, signal } from 'nsh'import { ToolbarButton, TEXT_SIZE } from 'nacre'
export function ToolbarButtonStates() { const open = signal(false) return ( <View class="gap-2"> <View class="flex-row flex-wrap items-center gap-0.5"> <ToolbarButton compact icon="more" label="更多格式" pressed={open()} expanded={open()} onPress={() => open.update((v) => !v)} /> <ToolbarButton icon="sparkles" label="写作助手" disabled /> <ToolbarButton icon="star" label="邀请评价" tooltip="客户已完成评价" /> </View> <If when={open()}> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>弹出的面板开着</Text> </If> </View> )}自带图标
定位到「自带图标」icon 除了库图标名,还可以是渲染函数:按钮把该用的 size 与 color 交给它,用来放应用自己的图标或图标上的叠层(提醒涟漪)。state 是 getter,在 JSX 属性里读才会跟着切换态变。
import { View } from 'nsh'import { Icon, ToolbarButton } from 'nacre'
export function ToolbarButtonCustomIcon() { return ( <View class="flex-row flex-wrap items-center gap-0.5"> <ToolbarButton label="邀请评价" icon={(state) => ( <View class="items-center justify-center"> <View class="absolute w-4 h-4 rounded-full border border-foreground opacity-40" /> <Icon name="star" size={state.size} color={state.color} /> </View> )} /> </View> )}文字图形
定位到「文字图形」children 放表情这类文字图形,代替图标居中;配 compact,label 写读屏名。
import { Text, View, signal } from 'nsh'import { ToolbarButton, TEXT_SIZE } from 'nacre'
export function ToolbarButtonEmoji() { const picked = signal('') return ( <View class="gap-2"> <View class="flex-row"> {['😊', '🙏', '👍', '❤️', '🎉', '👋'].map((emoji) => ( <ToolbarButton compact label={emoji} onPress={() => picked.set(emoji)}> {emoji} </ToolbarButton> ))} </View> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{picked() ? `$已插入 ${picked()}` : '点一个表情'}</Text> </View> )}反色表面
定位到「反色表面」surface=“inverse” 放在主色实底上:静止带一层淡底,按下态翻成白底主色字;焦点环改用 primary-foreground。class 只放布局(flex-1)。
import { View, signal } from 'nsh'import { ToolbarButton } from 'nacre'
export function ToolbarButtonInverse() { const muted = signal(false) const held = signal(true) return ( <View class="w-[280px] max-w-full p-2 rounded-lg bg-primary flex-row gap-1"> <ToolbarButton surface="inverse" class="flex-1 min-w-0" icon="volume" label="静音" pressed={muted()} onPress={() => muted.update((v) => !v)} /> <ToolbarButton surface="inverse" class="flex-1 min-w-0" icon="pause" label="保持" pressed={held()} onPress={() => held.update((v) => !v)} /> <ToolbarButton surface="inverse" class="flex-1 min-w-0" icon="phone" label="转接" /> </View> )}关键状态切换
定位到「关键状态切换」variant=“control” 给必须一眼看清开关的状态(通话中的静音、保持):静止就有背景色底 + 描边,按下翻成主色实底、白字;工具栏那种静止无底、按下极淡底的外观在深色下分不清开关。主色实底上(surface=“inverse”)两种外观相同。
import { View, signal } from 'nsh'import { ToolbarButton } from 'nacre'
export function ToolbarButtonControl() { const muted = signal(true) const held = signal(false) return ( <View class="w-[280px] max-w-full p-2 rounded-lg border border-border bg-secondary flex-row gap-1"> <ToolbarButton variant="control" class="flex-1 min-w-0" icon="volume" label="静音" pressed={muted()} onPress={() => muted.update((v) => !v)} /> <ToolbarButton variant="control" class="flex-1 min-w-0" icon="pause" label="保持" pressed={held()} onPress={() => held.update((v) => !v)} /> </View> )}拨号键
定位到「拨号键」size=“keypad” 是 40 高的拨号键:ghost,静止无底、按下才有底色(macOS 电话键盘),children 放数字(title 字号、等宽数字),配 compact,label 写读屏名;宽度由 class 给(flex-1 均分一行),键距 8。数字本身就是文字,tooltip 传 null。
手机:size=“keypad” 是拨号键:静止无底、按下才有底色,children 放数字(等宽),label 写读屏名;宽度由 class 给(flex-1 均分一行)。
import { Text, View, signal } from 'nsh'import { ToolbarButton, TEXT_SIZE } from 'nacre'
export function ToolbarButtonKeypad() { const typed = signal('') return ( <View class="w-[240px] max-w-full gap-2"> <Text class={`h-6 ${TEXT_SIZE.title} tabular-nums text-foreground`}>{typed()}</Text> <Each of={[['1', '2', '3'], ['4', '5', '6'], ['7', '8', '9'], ['*', '0', '#']]}> {(row) => ( <View class="flex-row gap-2"> <Each of={row()}> {(key) => ( <ToolbarButton size="keypad" compact class="flex-1" label={key()} tooltip={null} onPress={() => typed.update((v) => (v + key()).slice(-16))}> {key()} </ToolbarButton> )} </Each> </View> )} </Each> </View> )}键盘
定位到「键盘」| 键 | 行为 |
|---|---|
| Tab | 聚焦,显示焦点环 |
| Space / Return | 按下 |
无障碍
定位到「无障碍」- role 是 button,读屏名始终是 label,只图标、放表情时也一样。
- 传了 pressed 读屏报按下态;传了 expanded 报「已展开 / 已收起」。
- 禁用时不可点、40% 透明。
该用 / 不该用
定位到「该用 / 不该用」该用
- 一条工具栏的按钮用同一档尺寸,一起切 compact
- 切换类用 pressed,弹浮层的触发器加 expanded
- 快捷键写进 shortcut,不拼进 label
不该用
- 当提交按钮用(用 Button)
- 在 class 里改颜色(用 surface)
- 把说明写进 label(写进 tooltip)
API
定位到「API」ToolbarButton
定位到「ToolbarButton」| 属性 | 类型 | 默认 | 说明 |
|---|---|---|---|
label * |
string |
— | 读屏名,同时是文字与悬停提示 |
icon |
IconName | ToolbarIconRender |
— | 图标:库图标名,或自带图标的渲染函数 |
children |
JSX.Element |
— | 文字图形(表情),代替图标居中显示;读屏名仍是 label |
compact |
boolean |
— | 只显示图标:文字退为读屏名与悬停提示(工具栏变窄、格式钮) |
pressed |
boolean |
— | 切换态(自动翻译开着、加粗生效):底色常亮、前景加深,读屏报「已按下」 |
expanded |
boolean |
— | 弹浮层的触发器报给读屏的展开态;给了就不做按下缩放(ADR 0020) |
shortcut |
string |
— | 快捷键(⌘/),文字后淡一档显示,悬停提示里也带;只图标时只在提示里 |
tooltip |
TooltipContent | null |
— | 悬停提示,默认同 label;null 不显示(按钮上方已有气泡) |
disabled |
boolean |
— | |
size |
'xs' | 'sm' | 'md' | 'keypad' |
'sm' |
xs 24 / sm 28(默认)/ md 32;keypad 桌面 40、触控 44 高的拨号键:悬停时显底、children 放数字(title 字号、等宽数字), 宽度不定,由 class 给(flex-1 均分一行) |
surface |
'default' | 'inverse' |
— | default 白卡与浮层上(静止无底);inverse 主色实底上,静止带淡底 |
variant |
'toolbar' | 'control' |
— | toolbar(默认)静止无底、按下极淡底,给不常看的工具;control 是关键状态的切换(通话中的静音、保持), 静止就有可见底(背景色底 + 描边),按下翻成主色实底,一眼分得清开关。inverse 表面上两者相同(淡底 / 白底) |
class |
string |
— | 根节点的布局类(flex-1、外边距),不改颜色 |
viewRef |
(node: ViewRef) => void |
— | 根节点(Tab 焦点邻居、交还焦点) |
onFocus |
() => void |
— | |
onBlur |
() => void |
— | |
onPress |
(event: PressEvent) => void |
— | |
testID |
string |
— |
* 必填