An icon + text button for toolbars (the composer toolbar, call controls): it can collapse to an icon, has a toggle state, and shows a shortcut hint after the text. Icon-only buttons outside a toolbar are IconButtons; standalone submit actions are Buttons.
Phone: Icon-and-text buttons in a toolbar (composer toolbar, call controls): can be icon-only and toggle; phones show no shortcut.
Status: experimental · Components: ToolbarButton
Import
Section titled “Import”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="Quick replies" shortcut="⌘/" /> <ToolbarButton icon="translate" label="Auto-translate" pressed={translating()} onPress={() => translating.update((v) => !v)} /> <ToolbarButton icon="sparkles" label="Writing assistant" /> </View> )}Examples
Section titled “Examples”Icon only
Section titled “Icon only”compact shows only the icon; label becomes the screen reader name and tooltip. When the toolbar gets narrow, switch the whole row to compact together. In compact mode the shortcut only appears in the tooltip.
Phone: compact shows only the icon and label becomes the screen reader name; the whole toolbar switches to compact when it narrows.
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="Quote" pressed={quoted()} onPress={() => quoted.update((v) => !v)} /> <ToolbarButton compact icon="pencil" label="Edit" /> <ToolbarButton compact icon="quote" label="Quick replies" shortcut="⌘/" /> <ToolbarButton compact icon="eraser" label="Clear formatting" /> </View> )}States
Section titled “States”Toggles (auto-translate, bold) use pressed, read as pressed; triggers that open a popover also pass expanded (read as expanded, and they don’t scale when pressed). tooltip can differ from label (to explain why it’s unavailable); pass null for no tooltip.
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="More formatting" pressed={open()} expanded={open()} onPress={() => open.update((v) => !v)} /> <ToolbarButton icon="sparkles" label="Writing assistant" disabled /> <ToolbarButton icon="star" label="Invite rating" tooltip="The customer has rated" /> </View> <If when={open()}> <Text class={`${TEXT_SIZE.small} text-muted-foreground`}>The panel it opens is showing</Text> </If> </View> )}Custom icon
Section titled “Custom icon”Besides a library icon name, icon can be a render function: the button hands it the size and color to use, for the app’s own icons or an overlay on the icon (a reminder ripple). state is made of getters; read them in JSX attributes so they follow the toggle state.
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="Invite rating" 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> )}Text glyphs
Section titled “Text glyphs”children holds a text glyph such as an emoji, centered in place of the icon; use it with compact, with label as the screen reader name.
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() ? `$Inserted ${picked()}` : 'Pick an emoji'}</Text> </View> )}Inverse surface
Section titled “Inverse surface”surface=“inverse” goes on a solid primary fill: at rest it has a faint fill, and pressed flips to a white fill with primary text; the focus ring uses primary-foreground. class is for layout only (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="Mute" pressed={muted()} onPress={() => muted.update((v) => !v)} /> <ToolbarButton surface="inverse" class="flex-1 min-w-0" icon="pause" label="Hold" pressed={held()} onPress={() => held.update((v) => !v)} /> <ToolbarButton surface="inverse" class="flex-1 min-w-0" icon="phone" label="Transfer" /> </View> )}Key state toggles
Section titled “Key state toggles”variant=“control” is for states that must read at a glance (mute and hold during a call): at rest it has a background fill and an outline, and pressed flips to a solid primary fill with white text; the toolbar look (no fill at rest, a faint fill when pressed) is hard to tell apart in dark mode. On a solid primary fill (surface=“inverse”) both looks are the same.
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="Mute" pressed={muted()} onPress={() => muted.update((v) => !v)} /> <ToolbarButton variant="control" class="flex-1 min-w-0" icon="pause" label="Hold" pressed={held()} onPress={() => held.update((v) => !v)} /> </View> )}Dial keys
Section titled “Dial keys”size=“keypad” is a 40pt dial key: ghost, with no fill until pressed (like the macOS Phone keypad), with the digit in children (title size, tabular numbers); use it with compact, with label as the screen reader name. class sets the width (flex-1 to share a row), with 8pt between keys. The digit is already the text, so pass tooltip null.
Phone: size=“keypad” is a dial key: no fill at rest and a fill on press, digits as children (tabular), label as the screen reader name; width comes from class (flex-1 to share a row).
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> )}Keyboard
Section titled “Keyboard”| Key | Action |
|---|---|
| Tab | Focus and show the focus ring |
| Space / Return | Press |
Accessibility
Section titled “Accessibility”- The role is button and the screen reader name is always label, also when it shows only an icon or an emoji.
- With pressed, the pressed state is exposed; with expanded, it reads expanded / collapsed.
- Disabled buttons can’t be pressed and are 40% transparent.
Do / Don’t
Section titled “Do / Don’t”Do
- Use one size for a toolbar, and switch the row to compact together
- Use pressed for toggles, and add expanded to popover triggers
- Put shortcuts in shortcut, not in label
Don’t
- Use it as a submit button (use Button)
- Change colors in class (use surface)
- Put explanations in label (put them in tooltip)
Property descriptions follow the Chinese source comments.
ToolbarButton
Section titled “ToolbarButton”| Prop | Type | Default | Description |
|---|---|---|---|
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 |
— |
* required