- 手风琴
- 警报
- 警报对话框
- 纵横比
- 头像
- 徽章
- 面包屑
- 按钮
- 按钮组
- 日历
- 卡片
- 轮播
- 图表
- 复选框
- 可折叠
- 组合框
- 命令
- 上下文菜单
- 数据表格
- 日期选择器
- 对话框
- 方向
- 抽屉
- 下拉菜单
- 空
- 字段
- 悬停卡
- 输入
- 输入组
- 输入 OTP
- 条目
- 键盘
- 标签
- 菜单栏
- 原生选择
- 导航菜单
- 分页
- 弹出窗口
- 进度
- 单选按钮组
- 可调整大小
- 滚动区域
- 选择
- 分隔符
- 工作表
- 侧边栏
- 骨架
- 滑块
- Sonner
- 旋转器
- 切换
- 表格
- 标签
- 文本区域
- Toast
- 切换
- 切换组
- 工具提示
- 排版
- 手风琴
- 警报
- 警报对话框
- 纵横比
- 头像
- 徽章
- 面包屑
- 按钮
- 按钮组
- 日历
- 卡片
- 轮播
- 图表
- 复选框
- 可折叠
- 组合框
- 命令
- 上下文菜单
- 数据表格
- 日期选择器
- 对话框
- 方向
- 抽屉
- 下拉菜单
- 空
- 字段
- 悬停卡
- 输入
- 输入组
- 输入 OTP
- 条目
- 键盘
- 标签
- 菜单栏
- 原生选择
- 导航菜单
- 分页
- 弹出窗口
- 进度
- 单选按钮组
- 可调整大小
- 滚动区域
- 选择
- 分隔符
- 工作表
- 侧边栏
- 骨架
- 滑块
- Sonner
- 旋转器
- 切换
- 表格
- 标签
- 文本区域
- Toast
- 切换
- 切换组
- 工具提示
- 排版
import { Badge } from "@/components/ui/badge"
export function BadgeDemo() {安装
🌐 Installation
pnpm dlx shadcn@latest add badge
用法
🌐 Usage
import { Badge } from "@/components/ui/badge"<Badge variant="default | outline | secondary | destructive">Badge</Badge>示例
🌐 Examples
变体
🌐 Variants
使用 variant 属性来更改徽章的变体。
🌐 Use the variant prop to change the variant of the badge.
import { Badge } from "@/components/ui/badge"
export function BadgeVariants() {带有图标
🌐 With Icon
你可以在徽章内渲染图标。使用 data-icon="inline-start" 在左侧渲染图标,使用 data-icon="inline-end" 在右侧渲染图标。
🌐 You can render an icon inside the badge. Use data-icon="inline-start" to render the icon on the left and data-icon="inline-end" to render the icon on the right.
import { Badge } from "@/components/ui/badge"
import { BadgeCheck, BookmarkIcon } from "lucide-react"
带旋转器
🌐 With Spinner
你可以在徽章内渲染一个加载动画。记得给加载动画添加 data-icon="inline-start" 或 data-icon="inline-end" 属性。
🌐 You can render a spinner inside the badge. Remember to add the data-icon="inline-start" or data-icon="inline-end" prop to the spinner.
import { Badge } from "@/components/ui/badge"
import { Spinner } from "@/components/ui/spinner"
链接
🌐 Link
使用 render 属性将链接渲染为徽章。
🌐 Use the render prop to render a link as a badge.
import { Badge } from "@/components/ui/badge"
import { ArrowUpRightIcon } from "lucide-react"
自定义颜色
🌐 Custom Colors
你可以通过向 Badge 组件添加自定义类,如 bg-green-50 dark:bg-green-800,来定制徽章的颜色。
🌐 You can customize the colors of a badge by adding custom classes such as bg-green-50 dark:bg-green-800 to the Badge component.
import { Badge } from "@/components/ui/badge"
export function BadgeCustomColors() {从右到左
🌐 RTL
要在 shadcn/ui 中启用 RTL 支持,请参阅 RTL 配置指南。
🌐 To enable RTL support in shadcn/ui, see the RTL configuration guide.
"use client"
import * as React from "react"API参考
🌐 API Reference
徽章
🌐 Badge
Badge 组件显示一个徽章或看起来像徽章的组件。
🌐 The Badge component displays a badge or a component that looks like a badge.
| 属性 | 类型 | 默认值 |
|---|---|---|
variant | "default" | "secondary" | "destructive" | "outline" | "ghost" | "link" | "default" |
className | string | - |