跳到内容

徽章

一款用于卡片、表格和导航项上的状态、计数和标签的小型标签——采用了新粗野主义风格的边框和阴影处理。

import { Badge } from "@/components/ui/badge"

export function BadgeDemo() {

徽章是一个紧凑的行内标签——底层只是一个普通 <span>,没有任何基元,按新粗野主义的配方设计:粗边框、硬阴影、加粗字体。render 属性(借助 Base UI 的 useRender)能让任意元素——链接、按钮——套上徽章的样式。

适合以下场景:

  • 状态——表格行和卡片上的「启用中」「草稿」「失败」,出错时用 destructive
  • 计数——导航标签旁的未读消息数、购物车件数、通知总数。
  • 标签与元信息——博客卡片上的分类、套餐等级、文档里的版本标记。

安装

pnpm dlx shadcn@latest add https://neobrutalism.com/r/base/badge.json
npx shadcn@latest add https://neobrutalism.com/r/base/badge.json
yarn dlx shadcn@latest add https://neobrutalism.com/r/base/badge.json
bunx --bun shadcn@latest add https://neobrutalism.com/r/base/badge.json

将以下代码复制并粘贴到您的项目中。

components/ui/badge.tsx
import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { Slot } from "radix-ui"

import { cn } from "@/lib/utils"

const badgeVariants = cva(
  "group/badge inline-flex h-5 w-fit shrink-0 items-center justify-center gap-1 overflow-hidden rounded border-2 px-2 py-0.5 text-xs font-head font-medium whitespace-nowrap shadow-sm transition-all focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-primary has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-1.5 aria-invalid:border-destructive [&>svg]:pointer-events-none [&>svg]:size-3!",
  {
    variants: {
      variant: {
        default: "bg-primary text-primary-foreground [a]:hover:bg-primary/80",
        secondary:
          "bg-secondary text-secondary-foreground [a]:hover:bg-secondary/80",
        destructive:
          "bg-destructive text-destructive-foreground [a]:hover:bg-destructive/90",
        outline:
          "bg-transparent text-foreground [a]:hover:bg-muted [a]:hover:text-muted-foreground",
        ghost:
          "border-transparent bg-transparent shadow-none hover:bg-muted hover:text-muted-foreground dark:hover:bg-muted/50",
        link: "border-transparent bg-transparent shadow-none text-primary underline-offset-4 hover:underline",
      },
    },
    defaultVariants: {
      variant: "default",
    },
  }
)

function Badge({
  className,
  variant = "default",
  asChild = false,
  ...props
}: React.ComponentProps<"span"> &
  VariantProps<typeof badgeVariants> & { asChild?: boolean }) {
  const Comp = asChild ? Slot.Root : "span"

  return (
    <Comp
      data-slot="badge"
      data-variant={variant}
      className={cn(badgeVariants({ variant }), className)}
      {...props}
    />
  )
}

export { Badge, badgeVariants }

更新导入路径以匹配您的项目设置。

用法

import { Badge } from "@/components/ui/badge"
<Badge variant="default | outline | secondary | destructive">Badge</Badge>

示例

变体

使用 variant 属性来改变徽章的变体。

import { Badge } from "@/components/ui/badge"

export function BadgeVariants() {

带图标

您可以在徽章内渲染图标。使用 data-icon="inline-start" 将图标渲染在左侧,使用 data-icon="inline-end" 将图标渲染在右侧。

import { BadgeCheck, BookmarkIcon } from "lucide-react"

import { Badge } from "@/components/ui/badge"

带 Spinner

您可以在徽章内渲染一个 Spinner。请记得为该 Spinner 添加 data-icon="inline-start"data-icon="inline-end" 属性。

import { Badge } from "@/components/ui/badge"
import { Spinner } from "@/components/ui/spinner"

链接

使用 render 属性将链接渲染为徽章。

import { ArrowUpRightIcon } from "lucide-react"

import { Badge } from "@/components/ui/badge"

自定义颜色

您可以通过向 Badge 组件添加 bg-chart-2 text-primary-foreground 等自定义类来自定义徽章的颜色。

import { Badge } from "@/components/ui/badge"

export function BadgeCustomColors() {

RTL

要在 Neobrutalism 中启用 RTL 支持,请参阅 RTL 配置指南

"use client"

import * as React from "react"

无障碍

徽章渲染的是一个普通 <span>——屏幕阅读器只能拿到里面的文字,别的什么都没有,所以千万不要让颜色单独承载含义:variant="destructive" 的播报和 default 一模一样,含义必须写进文字里,比如「失败」。光秃秃的数字需要补上语境——加一个 aria-label 或视觉隐藏的文本,让「3」被读成「3 条未读通知」。通过 render 属性渲染成链接时,它就是一个普通的锚点元素,链接的常规语义照旧适用。

API 参考

Badge

Badge 组件显示一个徽章,或一个外观类似徽章的组件。

PropTypeDefault
variant"default" | "secondary" | "destructive" | "outline" | "ghost" | "link""default"
classNamestring-