Ordo UI
组件Data Display

Badge

@mox-hub/ui 组件 · Data Display · canonical

badge@mox-hub/uiData Displaycanonicalsrc/components/badge.tsx

变体轴

variant
defaultsecondarydestructiveoutlineghostsuccesswarninginfo
变体演示badge
默认次级描边危险定稿未定稿进行中

Props API

Prop类型默认值说明
className*string——
variant*"default" | "destructive" | "secondary" | "success" | "warning" | "info" | "outline" | "ghost""default"—
render*ReactElement<unknown, string | JSXElementConstructor<any>> | ComponentRenderFn<HTMLProps, {}>——

源码

packages/ui/src/components/badge.tsx
import { mergeProps } from "@base-ui/react/merge-props"
import { useRender } from "@base-ui/react/use-render"
import { cva, type VariantProps } from "class-variance-authority"
import type { JSX } from "react"

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

const badgeVariants = cva(
  "group/badge inline-flex h-5 w-fit max-w-full shrink-0 items-center justify-center gap-1 overflow-hidden truncate rounded-chip border border-transparent px-2 py-0.5 text-2xs leading-none font-medium whitespace-nowrap transition-[color,background-color,border-color,box-shadow] duration-fast outline-none focus-visible:border-ring focus-visible:ring-2 focus-visible:ring-ring/50 has-data-[icon=inline-end]:pe-1.5 has-data-[icon=inline-start]:ps-1.5 aria-invalid:border-destructive aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 [&>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/10 text-destructive focus-visible:ring-destructive/20 dark:bg-destructive/20 dark:focus-visible:ring-destructive/40 [a]:hover:bg-destructive/20",
        success:
          "bg-accent-green-soft text-accent-green-soft-foreground focus-visible:ring-accent-green-soft-foreground/20 dark:focus-visible:ring-accent-green-soft-foreground/40 [a]:hover:bg-accent-green-soft/80",
        warning:
          "bg-accent-yellow-soft text-accent-yellow-soft-foreground focus-visible:ring-accent-yellow-soft-foreground/20 dark:focus-visible:ring-accent-yellow-soft-foreground/40 [a]:hover:bg-accent-yellow-soft/80",
        info:
          "bg-accent-blue-soft text-accent-blue-soft-foreground focus-visible:ring-accent-blue-soft-foreground/20 dark:focus-visible:ring-accent-blue-soft-foreground/40 [a]:hover:bg-accent-blue-soft/80",
        outline:
          "border-border text-foreground [a]:hover:bg-muted",
        ghost:
          "hover:bg-muted hover:text-muted-foreground dark:hover:bg-muted/50",
      },
    },
    defaultVariants: {
      variant: "default",
    },
  }
)

function Badge({
  className,
  variant = "default",
  render,
  ...props
}: useRender.ComponentProps<"span"> & VariantProps<typeof badgeVariants>): JSX.Element {
  return useRender({
    defaultTagName: "span",
    props: mergeProps<"span">(
      {
        className: cn(badgeVariants({ variant }), className),
      },
      props
    ),
    render,
    state: {
      slot: "badge",
      variant,
    },
  })
}

export { Badge, badgeVariants }

可访问性与 RTL

  • 键盘:交互件保留:focus-visible焦点环;浮层支持 Escape 关闭与焦点归还,菜单 / 页签按 Base UI 约定支持方向键。 筛选菜单、拖拽排序、执行树与模型 listbox 等关键键盘流由 Playwright 断言覆盖,见规范示例 · 交互与可访问性。
  • RTL:间距、对齐与方向性图标使用逻辑属性 (ms-* /text-start /rtl:-scale-x-100);通过 ThemeProvider 或I18nProvider 的direction="rtl"驱动,`lint:rtl` 会拦截新增物理类。
  • 文案:优先用组件labels /translate /translator props,其次用I18nProvider目录;内置 zh-CN 默认值,en-US 为参考翻译。见Theming · i18n 与 RTL。
  • 对比度:高对比度 token、2px 控件边框与 3px 焦点环由主题层统一提供,见Foundations · Contrast。

用法说明

状态徽标,variant 控制语气;状态区分不要只靠颜色,配合文字或 aria-label。

语义档

success / warning / info 用于表达业务状态,分别走 accent 绿 / 黄 / 蓝的语义软底与配套前景, 浅色、暗色与高对比度自动适配,不需要再手写 bg-accent-*-light 或 dark: 覆盖:

<Badge variant="success">定稿</Badge>
<Badge variant="warning">未定稿</Badge>
<Badge variant="info">进行中</Badge>
<Badge variant="destructive">缺失</Badge>
  • 状态不要只靠颜色传达:保留文字标签,必要时补 aria-label。
  • 需要大面积语义底色时,同一套 token 可直接用于普通容器: bg-accent-green-soft text-accent-green-soft-foreground。
  • 既有 default / secondary / destructive / outline / ghost 行为不变。