Skip to the content
BooleanPress UI

Misc

Badge

A small label that shows a status, a count or a category next to other content.

Import

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

Usage

A badge is a short piece of text in a pill. It sits beside the thing it describes.

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

export function Row() {
  return (
    <p>
      ada@example.com <Badge variant="success">Delivered</Badge>
    </p>
  )
}

variant sets the look: default, secondary, outline, ghost and link are for categories and counts; success, warning, info and destructive are for a status. The status variants (this package's addition to stock) are on the status tokens.

Pass asChild and a link to make a badge that goes somewhere: the badge's classes move onto the link, and hover styles apply only then. A badge is not a button: for an action, use Button.

Examples

Variants

The neutral variants.

Status

The four status variants in a delivery list, each with its word.

With an icon

A 12 px icon before the text; the badge sizes it.

asChild puts the badge's look on a link, which keeps its role and address.

Accessibility

Semantics
A span, or the element you pass with asChild. It has no role, so a screen reader reads its text in the flow of the sentence.
Labels
Its text is its content. A status badge must say the status in words: colour alone fails WCAG 1.4.1. An icon-only badge needs a visually hidden text.
Focus
A badge is not focusable and has no keyboard behaviour of its own, so it takes no keyboard rows. A badge made a link with asChild is in the tab order and gets the link's keys and the focus ring.
Known limits
  • A badge does not announce a change to its text. For a status that changes while people watch, put it in a live region the page owns.

Keyboard

Keyboard
KeyBehaviour

API

Badge

Renders a span and passes it every other prop.

Badge props
PropTypeDefaultDescription
asChildbooleanfalseRender the child element instead (usually a link), with the badge's classes merged onto it.

Also exported: badgeVariants, the class names of Badge's variants and sizes (cva), to give another element the same look.

Every part takes className, merged with its defaults by cn(), and ref, which reaches the element it renders.

Data attributes: data-slot="badge" (Badge), and data-variant.

Theming

success fills with --success-strong and warning with --warning, info with --info, each with its -foreground token for the text. bui-contrast checks every pair.

The tokens its classes read. Change their values in your theme, and it follows.

Theme tokens
TokenUsed for
--accentbackground
--accent-foregroundtext
--borderborder
--destructiveborder, focus ring, background
--foregroundtext
--infobackground
--info-foregroundtext
--primarybackground, text
--primary-foregroundtext
--ringborder, focus ring
--secondarybackground
--secondary-foregroundtext
--success-foregroundtext
--success-strongbackground
--warningbackground
--warning-foregroundtext