← Gallery

Badge

UI primitive
badge

A short, static label for a status, a category or a count.

Live preview

DefaultSecondaryOutlineDestructive

A short, static label for a status, a category or a count. It informs; it does not act. For a selectable chip or filter use Toggle (or a Button at size xs); there is no separate chip component.

Live examples

default
Solid (primary role)
Paid
secondary
Neutral
Paid
outline
Line
Paid
destructive
Critical
Paid

Anatomy

  • Container — a <span> (or the child element with asChild, for example a link).
  • Optional icon — 12px SVG before the text.
  • Text — one to three words.

From the code

Tokens consumed
accentaccent-foregrounddestructiveforegroundprimaryprimary-foregroundringsecondarysecondary-foreground
Radius
fixed rounded-md (not the recipe control radius)
Focus
dark:focus-visible:ring-destructive/40 · focus-visible:border-ring · focus-visible:ring-[3px] · focus-visible:ring-destructive/20 · focus-visible:ring-ring/50
Motion
transition-[color,box-shadow]

Variants

CodeDesigner nameUse when
defaultSolid (primary role)The one label that should stand out in a group.
secondaryNeutralMost labels: categories, counts, metadata.
outlineLineQuiet labels in dense tables and lists.
destructiveCriticalAn error or blocking status.

States

StateLooksNote
StaticPer variant.The normal case.
Link hoverBackground at 90% when rendered as a link (asChild with <a>).Only when the badge navigates.
Focus3px ring in the ring role at 50%, border in ring.Only when focusable (link).
InvalidBorder and ring in destructive when aria-invalid.Rare; inherited from the form styles.

Do

  • Pair colour with text or an icon; never let colour alone carry the status (DESIGN-LANGUAGE §41).
  • Keep the text short and in one form across the product ("Paid", not "Payment completed" in one place and "Paid" in another).
  • Use secondary by default and save default for the one label that matters.

Don't

  • Don't attach a click handler to a badge; use Toggle or Button.
  • Don't use destructive for a status that is not an error.
  • Don't invent a colour per category through className; categories that need colour need tokens.

Known gaps

  • Badge uses the primary and secondary roles while Button's primary uses brand. In a skin where primary and brand differ, a default badge and a primary button do not match.
  • The radius is a fixed rounded-md, not the recipe control radius that Button and Tabs use.
  • The theme has success, warning and info roles but Badge has no variants for them; DESIGN-LANGUAGE §11 gives urgent and info badge colours as hex (#C85A54, #7A7A7A) instead of roles.
  • Link hover darkens by opacity (/90), which DESIGN-LANGUAGE §46 rejects for buttons.

Sources: DESIGN-LANGUAGE §11 Badge color rules; §41 Accessibility (colour not alone); §46 Button design rules

Size
1.6 KB
Exports
1
Imports
4
Type
ui

Exports

Badge

Imports

  • react
  • @radix-ui/react-slot
  • class-variance-authority
  • ./utils
import * as React from "react";
import { Slot } from "@radix-ui/react-slot";
import { cva, type VariantProps } from "class-variance-authority";

import { cn } from "./utils";

const badgeVariants = cva(
  "inline-flex items-center justify-center rounded-md border px-2 py-0.5 text-xs font-medium w-fit whitespace-nowrap shrink-0 [&>svg]:size-3 gap-1 [&>svg]:pointer-events-none focus-visible:border-ring focus-visible:ring-ring/50 focus-visible:ring-[3px] aria-invalid:ring-destructive/20 dark:aria-invalid:ring-destructive/40 aria-invalid:border-destructive transition-[color,box-shadow] overflow-hidden",
  {
    variants: {
      variant: {
        default:
          "border-transparent bg-primary text-primary-foreground [a&]:hover:bg-primary/90",
        secondary:
          "border-transparent bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/90",
        destructive:
          "border-transparent bg-destructive text-white [a&]:hover:bg-destructive/90 focus-visible:ring-destructive/20 dark:focus-visible:ring-destructive/40 dark:bg-destructive/60",
        outline:
          "text-foreground [a&]:hover:bg-accent [a&]:hover:text-accent-foreground",
      },
    },
    defaultVariants: {
      variant: "default",
    },
  },
);

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

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

export { Badge, badgeVariants };