跳转到内容
Nacre
组件配色
EN

徽标 Badge

stableBadge在画廊运行
Badge
在浏览器里运行真实组件。

未读与待处理的计数:0 不显示,超过 99 显示 99+;dot 支持 8pt 提醒点和 6pt 录音或通话状态点。只要格式化后的文字(窗口标题、Dock 角标)用 formatBadge(count)。

状态:stable · 组件:Badge

import { Badge } from 'nacre'
import { Text, View, signal } from 'nsh'
import { Badge, Button, TEXT_SIZE } from 'nacre'
export function BadgeUsage() {
const unread = signal(3)
return (
<View class="flex-row flex-wrap items-center gap-2">
<Text class={`${TEXT_SIZE.body} text-foreground`}>会话</Text>
<Badge count={unread()} label={`${unread()}$ 条未读消息`} />
<Button size="xs" onPress={() => unread.update((n) => n + 1)}>
来一条新消息
</Button>
<Button size="xs" variant="ghost" onPress={() => unread.set(0)}>
全部已读
</Button>
</View>
)
}

一律 brand-strong 实底、18 高;0 整个不渲染,超过 99 显示 99+。挂载时已有数不播入场,之后从 0 变成有数才淡入,归零时缩小淡出。

import { View } from 'nsh'
import { Badge } from 'nacre'
export function BadgeCounts() {
return (
<View class="flex-row items-center gap-3">
<Badge count={3} />
<Badge count={28} />
<Badge count={128} />
<Badge count={0} />
</View>
)
}

ring 缩成 14 高、10pt 字,叠在收起侧栏的图标右上角,不伸出 48 宽的栏。

import { View } from 'nsh'
import { Badge, Icon } from 'nacre'
export function BadgeRing() {
return (
<View class="flex-row items-center gap-4">
<View class="relative w-8 h-8 items-center justify-center">
<Icon name="chat" size={18} color="muted-foreground" />
<View class="absolute top-0 right-0">
<Badge ring count={5} label="5 条未读消息" />
</View>
</View>
<View class="relative w-8 h-8 items-center justify-center">
<Icon name="bell" size={18} color="muted-foreground" />
<View class="absolute top-0 right-0">
<Badge ring count={128} label="128 条未读通知" />
</View>
</View>
</View>
)
}

label 给读屏念完整的一句(「3 条待处理留言」);不给就只念数字。

import { Text, View } from 'nsh'
import { Badge, TEXT_SIZE } from 'nacre'
export function BadgeLabel() {
return (
<View class="w-[220px] max-w-full gap-2">
<View class="flex-row items-center justify-between">
<Text class={`${TEXT_SIZE.small} text-foreground`}>留言</Text>
<Badge count={3} label="3 条待处理留言" />
</View>
<View class="flex-row items-center justify-between">
<Text class={`${TEXT_SIZE.small} text-foreground`}>客诉工单</Text>
<Badge count={12} label="12 条待处理客诉" />
</View>
</View>
)
}

dot:8pt 实心点,只说「这里有事」不说几件。tone brand(默认)是有新内容没看(通知未读),destructive 是有问题要处理(质检提醒)。recording 和 live 是 6pt 的录音和通话状态点。显隐由调用方控制;独立传意时给 label。坐席在线与小休用 PresenceDot。

import { Text, View } from 'nsh'
import { Badge, TEXT_SIZE } from 'nacre'
export function BadgeDot() {
return (
<View class="w-[240px] max-w-full gap-2">
<View class="flex-row items-center gap-1.5">
<Badge dot tone="recording" testID="badge-dot-recording" />
<Text class={`${TEXT_SIZE.small} text-destructive-text`}>录音中</Text>
</View>
<View class="flex-row items-center gap-2">
<Text class={`flex-1 ${TEXT_SIZE.small} text-foreground`}>系统维护通知</Text>
<Badge dot testID="badge-dot-unread" label="未读" />
</View>
<View class="flex-row items-center gap-2">
<Text class={`flex-1 ${TEXT_SIZE.small} text-foreground`}>林嘉怡</Text>
<Badge dot tone="destructive" testID="badge-dot-qc" label="质检提醒" />
</View>
</View>
)
}
  • 默认读数字;在侧栏、标签页里用 label 写成完整说法(「12 条未读消息」)。
  • 计数为 0 时不在读屏树里。
  • 圆点单独传意时读 label(「未读」「录音中」);旁边有同义文字时省略 label,读屏只读一次。

该用

  • 0 不显示,超过 99 显示 99+
  • 未读与待处理用同一种徽标
  • 读屏用 label 写全
  • 行里只需要「有没有」时用 dot

不该用

  • 放非数字内容(用 Tag)
  • 只想提示「有新结果」时放数字(用 IconButton 的 dot)
  • 用 dot 表示坐席在线、忙碌(用 PresenceDot)
  • 自己写 w-2 h-2 rounded-full 的圆点
属性 类型 默认 说明
count number 0 计数;0 不显示。dot 时不用
dot boolean — 圆点模式,显隐由调用方控制;8pt 计数提醒或 6pt 进行中状态
tone BadgeDotTone 'brand' 圆点颜色,默认 brand
label string — 读屏念的完整说法(「3 条待处理留言」),不给就只念数字;dot 单独传意时必给,旁边已有同义文字时省略,避免读屏重复
ring boolean — 叠在侧栏图标右上角时用(侧栏收起):缩成 14 高、10pt 字(「99+」左右留白收到 2,不伸出 48 宽的栏)。dot 时不用
testID string —

* 必填