Bỏ qua đến nội dung

Huy hiệu

Một kiểu nhãn nhỏ dành cho trạng thái, số lượng và thẻ trên các thẻ, bảng và mục điều hướng — với phong cách viền và bóng theo trường phái tân thô mộc.

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

export function BadgeDemo() {

Badge là một nhãn nội dòng gọn nhẹ — chỉ là một <span> thuần, không có primitive bên dưới, tạo kiểu theo công thức neobrutalist: viền dày, bóng đổ đậm và kiểu chữ đậm. Prop render (qua useRender của Base UI) cho phép bất kỳ phần tử nào — một liên kết, một nút — khoác lên kiểu của badge.

Nên dùng khi:

  • Trạng thái — “Đang hoạt động”, “Bản nháp”, “Thất bại” trên các dòng bảng và card; dùng destructive cho lỗi.
  • Số lượng — tin nhắn chưa đọc, món trong giỏ, tổng số thông báo bên cạnh nhãn điều hướng.
  • Thẻ và metadata — danh mục trên card blog, hạng gói dịch vụ, nhãn phiên bản trong tài liệu.

Cài đặt

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

Sao chép và dán đoạn mã sau vào dự án của bạn.

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 }

Cập nhật các đường dẫn import cho khớp với cấu trúc dự án của bạn.

Cách dùng

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

Ví dụ

Biến thể

Dùng prop variant để thay đổi biến thể của badge.

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

export function BadgeVariants() {

Với biểu tượng

Bạn có thể hiển thị một biểu tượng bên trong badge. Dùng data-icon="inline-start" để hiển thị biểu tượng bên trái và data-icon="inline-end" để hiển thị biểu tượng bên phải.

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

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

Với spinner

Bạn có thể hiển thị một spinner bên trong badge. Nhớ thêm prop data-icon="inline-start" hoặc data-icon="inline-end" vào spinner.

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

Dùng prop render để hiển thị một link dưới dạng badge.

import { ArrowUpRightIcon } from "lucide-react"

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

Màu tùy chỉnh

Bạn có thể tùy chỉnh màu của badge bằng cách thêm các lớp tùy chỉnh như bg-chart-2 text-primary-foreground vào thành phần Badge.

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

export function BadgeCustomColors() {

RTL

Để bật hỗ trợ RTL trong Neobrutalism, xem hướng dẫn cấu hình RTL.

"use client"

import * as React from "react"

Khả năng truy cập

Badge render một <span> thuần — trình đọc màn hình chỉ nhận được phần chữ của nó và không gì khác, nên đừng để riêng màu sắc mang ý nghĩa: variant="destructive" được đọc lên y như default, chính phần chữ phải nói rõ “Thất bại”. Những con số trơ trọi cần ngữ cảnh — hãy thêm aria-label hoặc văn bản ẩn về mặt hiển thị để “3” được đọc thành “3 thông báo chưa đọc”. Khi được render thành liên kết qua prop render, nó là một thẻ neo bình thường và ngữ nghĩa liên kết thông thường được áp dụng.

Tham chiếu API

Badge

Thành phần Badge hiển thị một badge hoặc một thành phần trông giống badge.

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