跳转到内容
Nacre
组件配色
EN

图标钮 IconButton

stableIconButton在画廊运行
IconButton
在浏览器里运行真实组件。

只有图标的按钮,用在工具栏、行内与输入区。label 必填,同时是读屏名和悬停提示;有文字的动作用 Button。

状态:stable · 组件:IconButton

import { IconButton } from 'nacre'
import { Text, View, signal } from 'nsh'
import { IconButton, TEXT_SIZE } from 'nacre'
export function IconButtonUsage() {
const copied = signal(false)
return (
<View class="flex-row items-center gap-2">
<IconButton icon="copy" label="复制" onPress={() => copied.set(true)} />
<Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{copied() ? '已复制到剪贴板' : '点图标复制消息'}</Text>
</View>
)
}

ghost 无底(默认),用在工具栏与行内;primary 主色实底只给发送这类主动作;secondary 浅灰实底;round 改成圆形(输入区的 +、发送)。浮在图片上的切换钮用 float。

import { View } from 'nsh'
import { IconButton } from 'nacre'
export function IconButtonVariants() {
return (
<View class="flex-row flex-wrap items-center gap-2">
<IconButton icon="copy" label="复制" />
<IconButton icon="more" label="更多" />
<IconButton icon="send" label="发送" variant="primary" />
<IconButton icon="plus" label="添加" variant="secondary" round />
</View>
)
}

切换类用 pressed 表示开着,控制面板开合的同时传 expanded;disabled 与 loading 都不可点,loading 把图标换成转圈;dot 是右上角的提示点(有新结果待看),不带数字。

import { View, signal } from 'nsh'
import { IconButton } from 'nacre'
export function IconButtonStates() {
const open = signal(false)
return (
<View class="flex-row flex-wrap items-center gap-2">
<IconButton icon="sidebar-right" label="收起右侧面板" pressed={open()} expanded={open()} onPress={() => open.update((v) => !v)} />
<IconButton icon="download" label="下载" disabled />
<IconButton icon="refresh" label="刷新" loading />
<IconButton icon="export" label="导出记录" dot />
</View>
)
}

xs 24 / sm 28(默认)/ md 32 / lg 36,宽 = 高,跟结构 token control-h-* 走;嵌在一行小字里的清除、复制用 2xs 16(命中区补到 24)。

import { View } from 'nsh'
import { IconButton } from 'nacre'
export function IconButtonSizes() {
return (
<View class="flex-row flex-wrap items-center gap-2">
<IconButton size="xs" icon="download" label="下载" />
<IconButton size="sm" icon="download" label="下载" />
<IconButton size="md" icon="download" label="下载" />
<IconButton size="lg" icon="download" label="下载" />
</View>
)
}

surface 说明它放在哪一层(只对 ghost 生效):inverse 放在主色实底上(提醒气泡),图标与焦点环取 primary-foreground;media 叠在任意图片上(缩略图的移除钮),圆形半透明深底压住各种图,2xs 时图标 10。标题栏上用 chrome。

import { Text, View } from 'nsh'
import { IconButton, TEXT_SIZE } from 'nacre'
export function IconButtonSurfaces() {
return (
<View class="flex-row flex-wrap items-center gap-4">
<View class="flex-row items-center gap-1 h-7 pl-2.5 pr-1 rounded-md bg-primary">
<Text class={`${TEXT_SIZE.small} text-primary-foreground`}>记得邀请客户评价</Text>
<IconButton surface="inverse" size="xs" icon="close" label="关闭" />
</View>
<View class="w-16 h-16 rounded-lg bg-secondary overflow-hidden">
<View class="absolute inset-0 bg-primary/40" />
<IconButton surface="media" size="2xs" icon="close" label="移除图片" class="absolute top-1 right-1" />
</View>
</View>
)
}

icon 也可以是渲染函数:按钮把该用的 size 与 color(跟尺寸档、表面与按下态)交给它,放应用自己的图标集(拨号盘、退格)。

import { Text, View, signal, token } from 'nsh'
import { IconButton, TEXT_SIZE } from 'nacre'
import type { ColorToken } from 'nacre'
export function IconButtonCustomIcon() {
const open = signal(false)
const glyph = (state: { size: number; color: ColorToken }) => (
<View class="gap-px" style={{ width: state.size, height: state.size }}>
<Each of={[0, 1, 2]}>
{() => (
<View class="flex-1 flex-row gap-px">
<Each of={[0, 1, 2]}>{() => <View class="flex-1 rounded-full" style={{ backgroundColor: token(state.color) }} />}</Each>
</View>
)}
</Each>
</View>
)
return (
<View class="flex-row items-center gap-4">
<IconButton icon={glyph} label="拨号盘" pressed={open()} expanded={open()} onPress={() => open.update((v) => !v)} />
<View class="flex-row items-center gap-1 h-7 pl-2.5 pr-1 rounded-md bg-primary">
<Text class={`${TEXT_SIZE.small} text-primary-foreground`}>拨号盘</Text>
<IconButton surface="inverse" size="xs" icon={glyph} label="拨号盘" />
</View>
</View>
)
}

shortcut 排在悬停提示的文案右侧、淡一档;不要把快捷键拼进 label。它打开的菜单开着时传 tooltip={null} 收起提示。

手机:手机上不显示快捷键;label 是读屏名。

import { View } from 'nsh'
import { IconButton } from 'nacre'
export function IconButtonShortcut() {
return (
<View class="flex-row items-center gap-2">
<IconButton icon="search" label="搜索会话" shortcut="⌘K" />
<IconButton icon="sidebar-left" label="收起侧栏" shortcut="⌃⌘S" />
</View>
)
}
键 行为
Tab 聚焦,显示焦点环(focusable={false} 时不进 Tab 序列)
Space / Return 按下
  • role 是 button,读屏名是 label;悬停提示默认也是 label。
  • pressed 写进读屏的按下态;传了 expanded 时同时报「已展开 / 已收起」。
  • disabled 与 loading 时不可点。

该用

  • label 写动作(「收起右侧面板」「复制」)
  • 切换类用 pressed 表示开着
  • 同一条工具栏用同一档尺寸
  • 在主色底、图片上换 surface,不在 class 里改颜色

不该用

  • 用图标钮做主要操作(用 Button)
  • 把快捷键写进 label
  • 用 dot 表示数量(用 Badge)
属性 类型 默认 说明
icon * IconName | IconRender — 库图标名,或自带图标的渲染函数(应用自己的图标集,拿到尺寸与颜色)
label * string — 无障碍名,同时作为悬停提示
tooltip TooltipContent | null — 悬停提示,默认同 label(需要带快捷键时单独给);null 不显示(它打开的菜单开着时,FINDINGS 49)
shortcut string | null — 快捷键(⌃⌘S),在悬停提示里排在文案右侧、淡一档;不要拼进 label / tooltip
pressed boolean — 按下 / 打开态:底色常亮、图标变 foreground;切换时图标 0.8 → 1 弹一下(MOTION.toggle,确认生效)
pressedIcon IconName — 开态换的图标(收藏 star → star-filled、置顶):开关类图标钮开态用实心,不只靠换色(component-standard §15 D9)
expanded boolean — 控制展开的按钮(侧栏开合、右栏开合)报给读屏的展开态
disabled boolean —
loading boolean — 进行中:图标换成转圈,不可再点(刷新)
iconColor ColorToken —
onPress (event: PressEvent) => void — 带按下事件(读修饰键:⌥ 点击)
testID string —
dot boolean — 右上角 6pt 的 brand 提示点(有新结果待看,如导出完成);不带数字,数字用 Badge
size '2xs' | 'xs' | 'sm' | 'md' | 'lg' 'sm' 2xs / xs / sm(默认)/ md / lg:桌面 16 / 24 / 28 / 32 / 36,手机 20 / 32 / 44 / 48 / 52。手机小图标按钮向外补足 44pt 点击区
focusable boolean — 不进 Tab 顺序(工具栏里由方向键走的项、悬停才出现的钮)
viewRef (node: ViewRef) => void —
onFocus () => void —
onBlur () => void —
variant 'ghost' | 'primary' | 'secondary' | 'float' 'ghost' ghost 无底(默认);primary 主色实底(发送);secondary 浅灰实底;float 浮在内容上(图片两侧切换),不透明底 + 浮层投影
round boolean — 圆形(输入区的 +、Copilot 发送),默认圆角方形
class string — 根节点的布局类(外边距、self),不改颜色
surface 'card' | 'chrome' | 'inverse' | 'media' — 放在哪一层(只对 ghost 生效):card 白卡与浮层(hover-tint / selected-tint,默认);chrome 窗口材质上(标题栏,chrome-*); inverse 主色实底上(提醒气泡),图标与焦点环取 primary-foreground; media 叠在任意图片上(缩略图的移除钮),圆形半透明深底压住各种图,深色模式反过来

* 必填