Skip to content

Badge

Labels an item with a short word, such as a category, a count or a tag.

SteelBearingsFastenersRecalled

Installation

pnpm dlx shadcn@latest add @silkware/badge

To copy the component by hand, install its dependencies:

pnpm add class-variance-authority cn

Then copy the source into components/ui/badge.tsx:

components/ui/badge.tsx
import { cva, type VariantProps } from "class-variance-authority"import { cn } from "cn"
const badgeVariants = cva(  "group/badge inline-flex h-5 w-fit shrink-0 items-center justify-center gap-1 overflow-hidden rounded-4xl border border-transparent px-2 py-0.5 text-xs font-medium whitespace-nowrap transition-all focus-visible:border-ring focus-visible:ring-[3px] focus-visible:ring-ring/50 has-data-[icon=inline-end]:pr-1.5 has-data-[icon=inline-start]:pl-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-success/10 text-success focus-visible:ring-success/20 dark:bg-success/20 dark:focus-visible:ring-success/40 [a]:hover:bg-success/20",        warning:          "bg-warning/10 text-warning focus-visible:ring-warning/20 dark:bg-warning/20 dark:focus-visible:ring-warning/40 [a]:hover:bg-warning/20",        info:          "bg-info/10 text-info focus-visible:ring-info/20 dark:bg-info/20 dark:focus-visible:ring-info/40 [a]:hover:bg-info/20",        outline:          "border-border text-foreground [a]:hover:bg-muted [a]:hover:text-muted-foreground",        ghost:          "hover:bg-muted hover:text-muted-foreground dark:hover:bg-muted/50",        link: "text-primary underline-offset-4 hover:underline",      },    },    defaultVariants: {      variant: "default",    },  })
function Badge({  className,  variant = "default",  render,  ...props}: React.ComponentProps<"span"> &  VariantProps<typeof badgeVariants> & {    render?: (props: React.HTMLAttributes<HTMLElement>) => React.ReactNode  }) {  if (render) {    const renderProps = {      "data-slot": "badge",      "data-variant": variant,      className: cn(badgeVariants({ variant }), className),      ...props,    }
    return render(renderProps)  }
  return (    <span      data-slot="badge"      data-variant={variant}      className={cn(badgeVariants({ variant }), className)}      {...props}    />  )}
export { Badge, badgeVariants }

Usage

import { Badge } from "@/components/ui/badge"
<Badge variant="secondary">Bearings</Badge>

A badge is a span. It is a label, not a control: it has no role, takes no focus and does nothing when pressed. To make it a link, use the render prop shown below.

Examples

Variants

default is the ink fill, secondary the soft grey, outline a hairline border, destructive a crimson tint, success, warning and info a green, amber and blue tint, ghost a badge that only shows its shape on hover, and link a text link. In the Silkware theme a badge is a 6px chip, never a pill.

DefaultSecondaryOutlineDestructiveSuccessWarningInfoGhostLink

With icon

Put data-icon="inline-start" or data-icon="inline-end" on an icon so the badge tightens its padding on that side. Icons are 12px.

12 palletsBergamo

Success

The outcome of a check or a task was good: an inspection passed, a payment cleared. Pine green tint, with CircleCheckIcon and a word.

Inspection QI-319Passed

Warning

The item needs care soon: low stock, a certificate about to expire, a delivery not confirmed. Amber tint, with TriangleAlertIcon and a word.

Gommarda seals, lot 7745Low stock

Info

A neutral fact that helps someone scan a list: a new supplier, an item synced overnight. Cobalt tint, with InfoIcon and a word.

Tornival machined partsNew supplier

The render prop receives the badge props and returns an element, so the badge can be an a. Hover styles apply to badges rendered as links. Because render is a function, the file that uses it needs "use client".

Workflow state

Where an order, a task or a document stands in its flow is shown with the state square next to a word, not with a coloured badge. The square is ink when done, crimson when waiting on a person, an outline when plain and grey when off. The word carries the meaning; the square adds a quick scan.

  • Received
  • Awaiting approval
  • In transit
  • Cancelled

Accessibility

KeyAction
TabDoes nothing on a plain badge. A badge rendered as a link takes focus.
EnterFollows the link when the badge is rendered as an anchor.

React Aria provides nothing here: the badge is a styled span.

You provide:

  • Text that makes sense out of context. A count alone ("3") needs a label for a screen reader, for example an aria-label or visually hidden text.
  • Meaning in words. Colour, including the crimson of destructive and the tints of success, warning and info, is never the only signal: put the status in the word, and add the icon of the status.
  • The right element: a badge that goes somewhere is a link.

Guidelines

When to use

  • To tag an item with a category or attribute: "Fasteners", "Bergamo", "Draft".
  • To show a short count next to a label, such as open orders.
  • To report how a check turned out next to the item it is about: "Passed", "Low stock", "New supplier". Use the success, warning or info variant with its icon.

When not to use

  • To show where an item stands in a workflow, such as "Awaiting approval" or "In transit". Use the state square with a word, as in the workflow state example. See Table for it in a row.
  • To make something clickable. Use a Button or a link.
  • To mark an error. Use an Alert or the error of the field.
  • For text longer than two words. Use plain text or a Tooltip.
  • For a toggleable filter. Use a Toggle.

Content

  • One or two words, in sentence case: "Awaiting approval", not "AWAITING APPROVAL".
  • A noun or an adjective, not a verb. A verb reads as an action.
  • A badge never wraps: shorten the word instead.

Status

Two kinds of status, two tools. The state square says where an item stands in its flow: received, awaiting approval, in transit, cancelled. A status variant says how a check or an event turned out: success when it went well, warning when it needs care, info for a neutral fact, destructive when it failed. A square never takes a status colour, and a status badge never stands for a step in a flow.

Keep status badges sparing. In a long list, colour only the rows that need attention and leave the rest plain, so the colour still points at something.

ReceivedAwaiting approval
Do

Show where an item stands in its flow as a state square and a word.

ReceivedAwaiting approval
Don’t

Don’t choose a variant to stand for a workflow step. "Awaiting approval" in crimson reads as an error.

Inspection QI-319Passed

Do

Put the outcome in the word and add the icon of the status.

InspectionQI-319

Don’t

Don’t let the colour say "passed" on its own. Without the word and the icon, the badge says nothing to someone who cannot tell green from grey.

API reference

Badge

Renders a span, or the element returned by render. It accepts every span prop and the props below.

PropTypeDefaultDescription
variant?"default" | "secondary" | "destructive" | "success" | "warning" | "info" | "outline" | "ghost" | "link""default"The visual weight of the badge, or the status it reports: destructive, success, warning or info.
render?(props: HTMLAttributes<HTMLElement>) => ReactNodeNoneRenders another element, such as an anchor, with the badge classes and attributes.
className?stringNoneClasses merged after the variant classes.

badgeVariants

The class function behind the component, from class-variance-authority. Call it to give another element the badge styles: badgeVariants({ variant: "outline" }).