C

Tag 标签

数据展示

用于标记和分类,小型彩色色块。

何时使用

需要标签功能时使用本组件。

代码示例

基础用法

tsx
import { Tag } from "@/components/ui/tag"

export function Basic() {
return <Tag />
}

变体

tsx
{/* 展示 variant 变体 */}
<div className="flex flex-wrap items-center gap-3">
<Tag variant="default">Default</Tag>
<Tag variant="secondary">Secondary</Tag>
<Tag variant="destructive">Destructive</Tag>
<Tag variant="outline">Outline</Tag>
<Tag variant="success">Success</Tag>
</div>

尺寸

tsx
{/* 尺寸对照 */}
<div className="flex flex-wrap items-center gap-3">
<Tag size="sm">Sm</Tag>
<Tag size="default">Default</Tag>
<Tag size="lg">Lg</Tag>
</div>

样式变体

变体

Variant说明默认
default默认样式
secondary次要样式
destructive危险/删除样式
outline描边样式
success成功状态
warning警告状态
info信息提示

尺寸

Size说明默认
default默认尺寸
sm小号
lg大号

rounded

rounded说明默认
default
full
none

API / Props

Tag

PropTypeDefault说明
classNamestring自定义样式类名
closablebooleanfalseWhether the tag can be closed/removed
onClose() => voidCalled when close button is clicked
iconReact.ReactNodeIcon element
...restReact.ComponentProps<"span">透传 <span> 原生属性

注意事项

  • Client Component: 使用 "use client",依赖 React hooks/状态,需在客户端渲染。
  • CVA 变体: 通过 class-variance-authority 管理样式变体,类型安全。变体对象可导出复用。
  • 原生属性: 继承所有 <span> 原生 HTML 属性。

📘 完整交互式示例与控件属性请查看 Storybook 文档