跳转到内容
Nacre
组件配色
EN

图标 Icon

stableIcon · IconBadge在画廊运行
Icon
在浏览器里运行真实组件。

线性图标,五档尺寸(12 / 14 / 16 / 18 / 20,默认 16);颜色传 token 名。可用的名字是 ICONS 的键(IconName)。行首的类型徽记用 IconBadge;图标钮用 IconButton,它要求写名字。

状态:stable · 组件:Icon、IconBadge

import { Icon, IconBadge } from 'nacre'

图标配文字时是装饰,不给 accessibilityLabel,读屏只读文字。

import { Text, View } from 'nsh'
import { Icon, TEXT_SIZE } from 'nacre'
export function IconUsage() {
return (
<View class="flex-row items-center gap-1.5">
<Icon name="bell" size={14} color="muted-foreground" />
<Text class={`${TEXT_SIZE.small} text-foreground`}>新消息提醒</Text>
</View>
)
}

通知、转接和客户资质行分别用 32、24、36pt 徽记。按语气配底色与图标色,小号圆底也可传自定义图标。只有徽记单独传意时才给 label。

import { View } from 'nsh'
import { Icon, IconBadge } from 'nacre'
export function IconBadges() {
return (
<View class="flex-row items-center gap-2">
<IconBadge testID="icon-badge-alert" tone="warning" icon="warning" label="告警" />
<IconBadge testID="icon-badge-target" round size="sm" icon={(state) => <Icon name="user" size={state.size} color={state.color} />} />
<IconBadge testID="icon-badge-certificate" size="lg" icon="building" />
</View>
)
}

只用 12 / 14 / 16 / 18 / 20。行内配 small 字用 14,正文配 16。

import { Text, View } from 'nsh'
import { Icon, TEXT_SIZE } from 'nacre'
export function IconSizes() {
return (
<View class="flex-row items-end gap-4">
{[12, 14, 16, 18, 20].map((size) => (
<View class="items-center gap-1.5">
<Icon name="bell" size={size} color="foreground" />
<Text class={`${TEXT_SIZE.caption} text-muted-foreground tabular-nums`}>{String(size)}</Text>
</View>
))}
</View>
)
}

color 传 theme.json 的颜色 token 名(SVG 用 tintColor 着色,text-* 类管不到);状态图标配状态色,并且旁边写清文字。

import { Text, View } from 'nsh'
import { Icon, TEXT_SIZE } from 'nacre'
export function IconColors() {
return (
<View class="gap-2">
<View class="flex-row items-center gap-1.5">
<Icon name="warning" size={16} color="warning" />
<Text class={`${TEXT_SIZE.small} text-foreground`}>排队超过 5 分钟</Text>
</View>
<View class="flex-row items-center gap-1.5">
<Icon name="circle-x" size={16} color="destructive-text" />
<Text class={`${TEXT_SIZE.small} text-foreground`}>发送失败</Text>
</View>
<View class="flex-row items-center gap-1.5">
<Icon name="check" size={16} color="success-text" />
<Text class={`${TEXT_SIZE.small} text-foreground`}>已解决</Text>
</View>
<View class="flex-row items-center gap-1.5">
<Icon name="info" size={16} color="muted-foreground" />
<Text class={`${TEXT_SIZE.small} text-muted-foreground`}>客户已离开会话</Text>
</View>
</View>
)
}

ICONS 里所有可用的名字;悬停看全名。

手机:ICONS 里所有可用的名字,名字写在图标下方。

import { Text, View } from 'nsh'
import { ICONS, Icon, textTooltip, TEXT_SIZE } from 'nacre'
import type { IconName } from 'nacre'
export function IconCatalog() {
const names = Object.keys(ICONS) as IconName[]
return (
<View class="gap-3">
<Text class={`${TEXT_SIZE.small} text-muted-foreground`}>{`$图标全集(${names.length}$)`}</Text>
<View class="flex-row flex-wrap gap-2">
<Each of={names}>
{(name) => (
<View class="w-[104px] h-16 items-center justify-center gap-1.5 rounded-lg bg-plane" tooltip={textTooltip(name())}>
<Icon name={name()} size={18} color="foreground" />
<Text class={`max-w-[96px] truncate ${TEXT_SIZE.caption} text-muted-foreground`}>{name()}</Text>
</View>
)}
</Each>
</View>
</View>
)
}
  • 默认不进读屏树(装饰);图标单独表达含义时传 accessibilityLabel,它才会被读出。
  • 可点的图标不要用 Icon 自己包按钮,用 IconButton(label 必填,自动作为提示)。

该用

  • 尺寸只用 12 / 14 / 16 / 18 / 20
  • 状态图标配状态色 token
  • 颜色写 token 名(muted-foreground、warning…)

不该用

  • 用图标代替文字表达唯一含义
  • 图标染品牌色当装饰
  • 用 class 的 text-* 给图标上色(不生效)
属性 类型 默认 说明
name * IconName —
size number 16
color ColorToken 'foreground' SVG 用 tintColor 着色,text-* 类管不到图片,所以颜色走 token 名
class string —
accessibilityLabel string —

* 必填

属性 类型 默认 说明
icon * IconName | IconRender —
tone IconBadgeTone 'neutral'
size 'sm' | 'md' | 'lg' 'md' sm 是 24pt 底座和 14pt 图标,md / lg 分别是 32 / 36pt 底座与 16pt 图标
round boolean —
label string — 与文字并排时作装饰;独立表达含义时给读屏名
class string —
testID string —

* 必填