oakoss/ui
Components

Badge

A short label or count beside other content.

Source
NewDraftPaidOverdue

Installation

npx shadcn@latest add @oakoss/badge

Usage

import { Badge } from '#/components/ui/data/badge';

<Badge intent="success" variant="soft">
  Paid
</Badge>;

A badge labels something; it isn't a control. A set of labels the user can select or remove is a Tag Group, with keyboard removal and selection built in.

Upstream's variant values map to intent and variant:

UpstreamHere
defaultintent="primary" (the default)
secondaryintent="neutral" variant="soft"
destructiveintent="destructive" variant="soft"
outlineintent="neutral" variant="outline"
ghostPlain text, or intent="neutral" variant="soft"
linkA Link styled with badgeStyles

Examples

Looks

intent sets the color and variant the look: solid, soft or outline. Keep labels to a word or two; a badge stays on one line rather than truncating, which would hide its text.

neutralprimarysuccesswarningdestructiveinfo
neutralprimarysuccesswarningdestructiveinfo
neutralprimarysuccesswarningdestructiveinfo

A badge that goes somewhere is React Aria's Link with badgeStyles, which add hover, press and focus styles and a 44×44 pointer target around the 20px badge. Pass targetSize: false where badges sit closer than 44px apart, or their targets overlap.

Count

A number alone doesn't say what it counts. Put the words in text screen readers read (sr-only), and cap a long count for the eye while reading the whole number. If the count changes while the user is on the page, put it in an element with role="status" that's rendered before the change, so the new count is announced.

Inbox128 unread

Accessibility

A badge is a span with no role, read as part of the text around it. Its color only adds to its text: a status badge's word ("Paid", "Overdue") says what the color does, and an icon beside it, hidden from screen readers, helps people who can't tell the colors apart. Digits are tabular, so a changing count doesn't shift its neighbors.

Hover, press and focus styles come only through badgeStyles, so a plain badge never looks clickable. A badge link or button gets a 44×44 pointer target, and a disabled one takes the system's disabled color in Windows High Contrast, where every badge keeps its border. React Aria adds no keys; a badge link or button follows Link or Button.

API

Badge

A span.

Props

Prop

Type

Also accepts span props.

Data attributes

AttributePresent when
data-slotAlways: badge
data-intentAlways; the color
data-variantAlways; the look

CSS variables

VariableSets
--badge-bgThe solid fill
--badge-hoverThe solid fill on hover and press, through badgeStyles
--badge-fgText on the solid fill
--badge-subtleThe soft fill
--badge-borderThe outline border
--badge-textText, and the hover and press tint, for other looks

badgeStyles

Returns the classes for a React Aria Link or Button that looks like a badge. Takes intent, variant, targetSize (default true) and className, which wins over conflicting classes.