#Badge 角标
展示数量、未读提醒或简短状态。
#基础用法
8
basic.tsx
import { Badge, Button } from '@ifhover/leaf-ui';
import { Bell } from 'lucide-react';
export function BadgeBasic() {
return (
<Badge count={8} aria-label="8 条未读通知">
<Button variant="outline" startIcon={<Bell />} aria-label="通知" />
</Badge>
);
}
#数值规则
超过 max 的数值展示为上限加号;使用 showZero 展示零值。
超过数量上限99+
显示零值0
basic.tsx
import { Badge, Button, Flex, Space, Text } from '@ifhover/leaf-ui';
import { Bell } from 'lucide-react';
export function BadgeCounts() {
return (
<Space wrap size={32}>
<Flex direction="column" gap="sm" align="start">
<Text>超过数量上限</Text>
<Badge count={120}>
<Button variant="outline" startIcon={<Bell />} aria-label="通知" />
</Badge>
</Flex>
<Flex direction="column" gap="sm" align="start">
<Text>显示零值</Text>
<Badge count={0} showZero>
<Button variant="outline" startIcon={<Bell />} aria-label="通知" />
</Badge>
</Flex>
</Space>
);
}
#红点提醒
只提示有新内容,不展示具体数量。
basic.tsx
import { Badge, Button } from '@ifhover/leaf-ui';
import { Mail } from 'lucide-react';
export function BadgeDot() {
return (
<Badge dot aria-label="有未读消息">
<Button variant="outline" startIcon={<Mail />} aria-label="消息" />
</Badge>
);
}
#状态标记
用圆点和文字说明当前状态。
运行中待处理已停止更新中未启动
basic.tsx
import { Badge, Space } from '@ifhover/leaf-ui';
export function BadgeStatuses() {
return (
<Space wrap size={24}>
<Badge status="success" text="运行中" />
<Badge status="warning" text="待处理" />
<Badge status="error" text="已停止" />
<Badge status="info" text="更新中" />
<Badge status="default" text="未启动" />
</Space>
);
}
#Badge API
| 属性 | 类型 | 默认值 | 说明 |
|---|---|---|---|
children | ReactNode | — | 设置后将角标附着在该内容右上方 |
count | number | string | — | 角标内容 |
max | number | 99 | 超过该值时展示 max+ |
showZero | boolean | false | 允许展示 0 |
dot | boolean | false | 使用圆点提醒 |
status | 'success' | 'warning' | 'error' | 'info' | 'default' | — | 使用对应颜色的状态圆点 |
text | ReactNode | — | 圆点旁的说明 |
color | string | — | 自定义角标颜色 |
#类型
定义组件属性或组装列表时可导入相关类型。
import type { BadgeProps } from '@ifhover/leaf-ui';#使用说明
独立使用时可展示数量或状态。附着在按钮等内容上时,为圆点提醒提供 aria-label,以说明提醒的含义。