Badge
A small label beside a name or in a table cell: a status, a category, a version, a count. It never takes focus and is not a control. For a chip the reader can remove or select use the tag; for a state with a live pulse use the status dot.
Install
npx shadcn@latest add https://thrumui.dev/r/badge.jsonAdds components/ui/badge.tsx. Nothing else in your project changes.
import { badgeClass } from "@/components/ui/badge";Props
| Prop | Type | Default | Description |
|---|---|---|---|
variant | "neutral" | "primary" | "outline" | "success" | "warning" | "destructive" | Default "neutral" | neutral, primary and outline set the weight. The three status variants put the text colour on a 10% tint of the status colour, inside a border of that colour. |
size | "sm" | "md" | Default "md" | sm is 20 pixels tall with 11 pixel text, md is 24 pixels tall with 12 pixel text. |
dot | boolean | Default false | A 6 pixel dot before the text. It takes the status colour of the variant, or the text colour on a plain variant. |
count | number | – | Count mode: the badge prints this number as a whole number, never below 0. Its children become a visually hidden unit, such as “unread messages”. |
max | number | Default 99 | In count mode, the highest number printed. Above it the badge reads “99+” and a screen reader still hears the real number. |
children | ReactNode | – | The label. A long one is cut with an ellipsis at the width of the container; plain text is also its tooltip. |
className | string | – | Extra classes, merged last. |
...props | ComponentProps<"span"> | – | Every other span attribute is passed through. |
badgeClass() | ({ variant, size }) => string | – | A second export: the same classes for an element that is not a badge, such as a link. |
badgeStyle() | (variant) => CSSProperties | undefined | – | The status colour of a variant as a style. Set it next to badgeClass() when the variant is success, warning or destructive. |
formatCount() | (count: number, max?: number) => string | – | The text count mode prints: formatCount(128) is “99+”. |
Motion and sound
Motion
- The badge does not move.
Sound
Silent. This component makes no sound.
Accessibility
- It is a span with no role, read as part of the text around it. Nothing in it takes focus.
- A status variant adds colour only, so the label has to name the state: “Paid”, “Past due”, “Failed”. The dot is hidden from screen readers.
- Status text stays in the theme's text colour on a 10% tint, so it keeps 4.5 to 1 whatever the status colours are.
- In count mode a capped number is hidden from screen readers and the real one is read instead, followed by the children: “128 unread messages”.
- It is not a live region. Where a change of count must be announced, put the badge inside an element with aria-live.
Source
"use client";
import type { ComponentProps, CSSProperties } from "react";
import { cn } from "@/lib/utils";
import { STATUS } from "@/lib/thrum-chart";
export type BadgeVariant = "neutral" | "primary" | "outline" | "success" | "warning" | "destructive";
export type BadgeSize = "sm" | "md";
export type BadgeProps = ComponentProps<"span"> & {
/** `neutral`, `primary` and `outline` set the weight. `success`, `warning` and `destructive` tint the badge with a status colour. */
variant?: BadgeVariant;
/** `sm` is 20 pixels tall, `md` is 24. */
size?: BadgeSize;
/** A 6 pixel dot before the text, in the status colour of the variant. */
dot?: boolean;
/** Count mode: the badge shows this number, and its children become a visually hidden unit ("unread"). */
count?: number;
/** In count mode, the highest number printed. Above it the badge reads "99+". */
max?: number;
};
const BASE =
"inline-flex max-w-full shrink-0 items-center justify-center gap-1.5 rounded-[min(12px,calc(var(--radius)*1.5))] border border-transparent font-medium whitespace-nowrap tabular-nums";
// A status variant is the text colour on a 12% tint of the status colour over the card surface, inside a border of that colour.
// The words stay in the text colour, so they keep their contrast whatever the theme's status colours are.
const TONED =
"border-[var(--badge-tone,currentColor)] bg-[color-mix(in_oklab,var(--badge-tone,currentColor)_12%,var(--card))] text-foreground";
const VARIANT: Record<BadgeVariant, string> = {
neutral: "bg-secondary text-secondary-foreground",
primary: "bg-primary text-primary-foreground",
outline: "border-input bg-card text-card-foreground",
success: TONED,
warning: TONED,
destructive: TONED,
};
const SIZE: Record<BadgeSize, string> = {
sm: "h-5 min-w-5 px-1.5 text-[0.6875rem] leading-4",
md: "h-6 min-w-6 px-2 text-xs leading-4",
};
const TONE: Partial<Record<BadgeVariant, string>> = { success: STATUS.ok, warning: STATUS.warn, destructive: STATUS.alarm };
/** The classes of a badge, for an element that is not one: a link that should look like a badge. Pair a status variant with `badgeStyle()`. */
export function badgeClass({ variant = "neutral", size = "md" }: { variant?: BadgeVariant; size?: BadgeSize } = {}): string {
return cn(BASE, VARIANT[variant], SIZE[size]);
}
/** The status colour of a variant, as the style a status badge reads it from. `undefined` for the three plain variants. */
export function badgeStyle(variant: BadgeVariant = "neutral"): CSSProperties | undefined {
const tone = TONE[variant];
return tone ? ({ "--badge-tone": tone } as CSSProperties) : undefined;
}
/** A count as a badge prints it: whole, never negative, and "99+" once it passes `max`. */
export function formatCount(count: number, max = 99): string {
const whole = Number.isFinite(count) ? Math.max(0, Math.trunc(count)) : 0;
return whole > max ? `${max}+` : String(whole);
}
/**
* A small label for a status, a category or a count. It is a `<span>` and never takes focus.
* A status variant adds colour only: the text has to say the state.
*/
export function Badge({ variant = "neutral", size = "md", dot = false, count, max = 99, className, style, children, ...props }: BadgeProps) {
const counting = count !== undefined;
const capped = counting && formatCount(count, max) !== String(count);
return (
<span data-slot="badge" data-surface={variant === "primary" ? "accent" : "control"} data-variant={variant} className={cn(badgeClass({ variant, size }), className)} style={{ ...badgeStyle(variant), ...style }} {...props}>
{dot && <span data-slot="badge-dot" aria-hidden="true" className="size-1.5 flex-none rounded-full bg-[var(--badge-tone,currentColor)]" />}
{counting ? (
<>
<span aria-hidden={capped || undefined}>{formatCount(count, max)}</span>
{/* A screen reader hears the real number, then the unit. */}
{(capped || children) && (
<span className="sr-only">
{capped && count} {children}
</span>
)}
</>
) : (
// Cut with an ellipsis when the container is narrower than the text; plain text stays reachable as a tooltip.
<span className="min-w-0 truncate" title={typeof children === "string" ? children : undefined}>
{children}
</span>
)}
</span>
);
}Files and dependencies
- Adds
components/ui/badge.tsx- Packages
- None
- Brings along
- Chart foundations
Changelog
- 1.0.0 · 2026-12-03First release.