A count of unread and pending items: 0 shows nothing, and anything over 99 shows 99+; dot supports 8pt attention indicators and 6pt recording and live-call indicators. When you only need the formatted text (window title, Dock badge), use formatBadge(count).
Status: stable · Components: Badge
Import
Section titled “Import”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`}>Chats</Text> <Badge count={unread()} label={`${unread()}$ unread messages`} /> <Button size="xs" onPress={() => unread.update((n) => n + 1)}> New message </Button> <Button size="xs" variant="ghost" onPress={() => unread.set(0)}> Mark all as read </Button> </View> )}Examples
Section titled “Examples”Counts
Section titled “Counts”Always a solid brand-strong fill, 18 high; 0 renders nothing at all, and over 99 shows 99+. A count present at mount plays no entrance; after that it fades in when going from 0 to a count, and shrinks and fades out when it returns to zero.
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> )}On an icon corner
Section titled “On an icon corner”ring shrinks it to 14 high with 10pt text, overlaid on the top-right corner of an icon in the collapsed sidebar without sticking out of the 48-wide rail.
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 unread messages" /> </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 unread notifications" /> </View> </View> </View> )}Screen reader wording
Section titled “Screen reader wording”label gives screen readers a full phrase (“3 pending messages”); without it, only the number is read.
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`}>Messages</Text> <Badge count={3} label="3 pending messages" /> </View> <View class="flex-row items-center justify-between"> <Text class={`${TEXT_SIZE.small} text-foreground`}>Cases</Text> <Badge count={12} label="12 pending cases" /> </View> </View> )}dot: an 8pt solid dot that says “something is here” without saying how many. tone brand (the default) means new content not yet seen (unread notifications); destructive means a problem to handle (QC reminders). The recording and live tones are 6pt dots for recording and active calls. Supply a label for standalone dots; omit it when adjacent text already names the state. Use PresenceDot for agent availability.
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`}>Recording</Text> </View> <View class="flex-row items-center gap-2"> <Text class={`flex-1 ${TEXT_SIZE.small} text-foreground`}>System maintenance notice</Text> <Badge dot testID="badge-dot-unread" label="Unread" /> </View> <View class="flex-row items-center gap-2"> <Text class={`flex-1 ${TEXT_SIZE.small} text-foreground`}>Lin Jiayi</Text> <Badge dot tone="destructive" testID="badge-dot-qc" label="QC reminder" /> </View> </View> )}Accessibility
Section titled “Accessibility”- Reads the number by default; in the sidebar and tabs, use label for the full phrase (“12 unread messages”).
- A count of 0 is not in the screen reader tree.
- A standalone dot reads its label (“Unread”, “Recording”). Leave it unnamed next to equivalent text so it is read once.
Do / Don’t
Section titled “Do / Don’t”Do
- Show nothing for 0, and 99+ above 99
- Use the same badge for unread and pending
- Spell it out for screen readers with label
- Use dot when a row only needs “there is something”
Don’t
- Put non-numeric content in it (use Tag)
- Show a number when you only mean “there’s something new” (use IconButton’s dot)
- Use dot for agent availability (use PresenceDot)
- Hand-write a w-2 h-2 rounded-full dot
Property descriptions follow the Chinese source comments.
| Prop | Type | Default | Description |
|---|---|---|---|
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 |
— |
* required