Components
Badge
A small label for a status, a category or a count, and a way to pin one to the corner of another element.
Introduction
Badge is a short label with a color: "Paid", "New", "Beta", "3". It says something about what is next to it, such as the status of an order, the category of a post or the number of unread messages, and it does nothing by itself.
variantis the color: neutral, success, warning or danger, from the theme.appearanceis how it is used: a soft tint (the default), a solid fill, or an outline. All of them are readable in the light and the dark theme.- Three sizes, a pill or a square, a dot or an icon before the text, and a remove button. A number over
maxshows as99+. BadgeAnchorpins a badge to a corner of another element, for counts and status dots, and has a place for the text screen readers hear.
A badge is not interactive. For something the user can press, use a button or a link.
Installation
centerUI has no CLI or registry yet: components are copied into your project.
# bun
bun add tailwind-variants tailwind-merge clsx
# pnpm
pnpm add tailwind-variants tailwind-merge clsx
# npm
npm install tailwind-variants tailwind-merge clsxManual installation
Copy these files into your project and keep the paths, or adjust the imports.
1. Add the styles
src/types/badge-styles.tsShow code
import { tv } from "tailwind-variants";
// Styles for Badge. The colors are the theme's own: `variant` is the color
// (default, success, warning, danger) and `appearance` is how it is used
// (soft tint, solid fill, or an outline). Warning is a yellow that is too light
// to be text on a tint, so its text is mixed with the foreground color.
export const badgeStyles = tv({
slots: {
root: "inline-flex shrink-0 items-center justify-center gap-1 font-medium leading-none whitespace-nowrap tabular-nums [&_svg]:size-[1em] [&_svg]:shrink-0",
dot: "size-1.5 shrink-0 rounded-full bg-current",
remove:
"-mr-1 ml-0.5 inline-flex size-3.5 shrink-0 cursor-pointer items-center justify-center rounded-full opacity-70 outline-none transition hover:bg-current/15 hover:opacity-100 focus-visible:ring-2 focus-visible:ring-current [&_svg]:size-2.5",
},
variants: {
variant: { default: {}, success: {}, warning: {}, danger: {} },
appearance: { soft: {}, solid: {}, outline: {} },
size: {
sm: { root: "h-4 min-w-4 px-1.5 text-[10px]" },
md: { root: "h-5 min-w-5 px-2 text-xs" },
lg: { root: "h-6 min-w-6 px-2.5 text-sm" },
},
isSquare: {
true: { root: "rounded-md" },
false: { root: "rounded-full" },
},
},
compoundVariants: [
// Solid
{
appearance: "solid",
variant: "default",
class: { root: "bg-fg text-bg" },
},
{
appearance: "solid",
variant: "success",
class: { root: "bg-success text-success-fg" },
},
{
appearance: "solid",
variant: "warning",
class: { root: "bg-warning text-warning-fg" },
},
{
appearance: "solid",
variant: "danger",
class: { root: "bg-danger text-danger-fg" },
},
// Soft
{
appearance: "soft",
variant: "default",
class: { root: "bg-muted text-fg ring-1 ring-border ring-inset" },
},
{
appearance: "soft",
variant: "success",
class: { root: "bg-success/12 text-success" },
},
{
appearance: "soft",
variant: "warning",
class: {
root: "bg-warning/20 text-[color-mix(in_oklab,var(--warning)_55%,var(--fg))]",
},
},
{
appearance: "soft",
variant: "danger",
class: { root: "bg-danger/12 text-danger" },
},
// Outline
{
appearance: "outline",
variant: "default",
class: { root: "border bg-transparent text-fg" },
},
{
appearance: "outline",
variant: "success",
class: { root: "border border-success/50 bg-transparent text-success" },
},
{
appearance: "outline",
variant: "warning",
class: {
root: "border border-warning/60 bg-transparent text-[color-mix(in_oklab,var(--warning)_55%,var(--fg))]",
},
},
{
appearance: "outline",
variant: "danger",
class: { root: "border border-danger/50 bg-transparent text-danger" },
},
],
defaultVariants: {
variant: "default",
appearance: "soft",
size: "md",
isSquare: false,
},
});
// Styles for BadgeAnchor: a badge fixed to the corner of another element.
export const badgeAnchorStyles = tv({
slots: {
root: "relative inline-flex shrink-0",
badge: "absolute z-10 ring-2 ring-bg",
},
variants: {
placement: {
"top-right": {},
"top-left": {},
"bottom-right": {},
"bottom-left": {},
},
// A round anchor, such as an avatar, has no corner: the badge sits on its
// rim, so it is moved in from the corner of the box around it.
isCircle: { true: {}, false: {} },
isInvisible: { true: { badge: "scale-0 opacity-0" }, false: {} },
},
compoundVariants: [
{
placement: "top-right",
isCircle: false,
class: { badge: "top-0 right-0 translate-x-1/3 -translate-y-1/3" },
},
{
placement: "top-left",
isCircle: false,
class: { badge: "top-0 left-0 -translate-x-1/3 -translate-y-1/3" },
},
{
placement: "bottom-right",
isCircle: false,
class: { badge: "right-0 bottom-0 translate-x-1/3 translate-y-1/3" },
},
{
placement: "bottom-left",
isCircle: false,
class: { badge: "bottom-0 left-0 -translate-x-1/3 translate-y-1/3" },
},
{
placement: "top-right",
isCircle: true,
class: { badge: "top-[6%] right-[6%] translate-x-1/3 -translate-y-1/3" },
},
{
placement: "top-left",
isCircle: true,
class: { badge: "top-[6%] left-[6%] -translate-x-1/3 -translate-y-1/3" },
},
{
placement: "bottom-right",
isCircle: true,
class: {
badge: "right-[6%] bottom-[6%] translate-x-1/3 translate-y-1/3",
},
},
{
placement: "bottom-left",
isCircle: true,
class: {
badge: "bottom-[6%] left-[6%] -translate-x-1/3 translate-y-1/3",
},
},
],
defaultVariants: {
placement: "top-right",
isCircle: false,
isInvisible: false,
},
});2. Add the Badge file
src/components/ui/badge/badge.tsxShow code
import type { ReactNode } from "react";
import { XIcon } from "@/components/icons";
import { cn } from "@/lib/cn";
import { badgeAnchorStyles, badgeStyles } from "@/types/badge-styles";
export interface BadgeProps {
/**
* The text. A number is shown as it is, or as `99+` when it is more than
* `max`.
*/
children?: ReactNode;
/** The color: the neutral one, or the theme's success, warning or danger. */
variant?: "default" | "success" | "warning" | "danger";
/** How the color is used: a soft tint, a solid fill, or an outline. */
appearance?: "soft" | "solid" | "outline";
size?: "sm" | "md" | "lg";
/** Rounded corners instead of a pill. */
isSquare?: boolean;
/** A small dot before the text, in the color of the text, for a status. */
hasDot?: boolean;
/** An icon before the text. */
icon?: ReactNode;
/** When `children` is a number over this, show this number and a `+`. */
max?: number;
/** Add a button that removes the badge. Pair it with `onRemove`. */
isRemovable?: boolean;
/** Called when the remove button is pressed. */
onRemove?: () => void;
/** Names the remove button. Defaults to "Remove". */
removeLabel?: string;
/** Makes the whole badge one image with this name, for a badge that is only a dot or a number. */
"aria-label"?: string;
role?: "img";
className?: string;
}
/**
* A small label for a status, a category or a count: "Paid", "New", "3". It is
* text with a color, and does nothing by itself, so it is not a button. Use it
* to say something about what is next to it.
*
* Pick the color with `variant` and how it is used with `appearance`. To pin
* one to the corner of another element, use `BadgeAnchor`.
*
* @example
* <Badge variant="success" hasDot>Paid</Badge>
* <Badge variant="danger" appearance="solid" max={99}>120</Badge>
*/
export function Badge({
children,
variant,
appearance,
size,
isSquare,
hasDot,
icon,
max,
isRemovable,
onRemove,
removeLabel = "Remove",
className,
...aria
}: BadgeProps) {
const styles = badgeStyles({ variant, appearance, size, isSquare });
const content =
typeof children === "number" && max !== undefined && children > max
? `${max}+`
: children;
return (
<span {...aria} className={cn(styles.root(), className)}>
{hasDot ? <span aria-hidden="true" className={styles.dot()} /> : null}
{icon}
{content}
{isRemovable ? (
<button
type="button"
aria-label={removeLabel}
onClick={onRemove}
className={styles.remove()}
>
<XIcon />
</button>
) : null}
</span>
);
}
export interface BadgeAnchorProps
extends Pick<
BadgeProps,
"variant" | "appearance" | "size" | "isSquare" | "max" | "icon"
> {
/** The element the badge is pinned to: an avatar, an icon button, a tab. */
children: ReactNode;
/** What the badge says. Leave it out for a plain dot. */
content?: ReactNode;
/** The corner it sits on. */
placement?: "top-right" | "top-left" | "bottom-right" | "bottom-left";
/** The element is round, such as an avatar: the badge sits on its rim. */
isCircle?: boolean;
/** Hide the badge, for a count that reached zero. It keeps its place. */
isInvisible?: boolean;
/**
* What the badge means for screen readers, for example "3 unread messages".
* A dot has no text, so it needs one.
*/
label?: string;
className?: string;
}
/**
* Pins a `Badge` to the corner of something: the number of unread messages on
* a bell, a status dot on an avatar. Without `content` the badge is a dot.
*
* @example
* <BadgeAnchor content={3} variant="danger" appearance="solid" label="3 unread">
* <Avatar name="Ana" />
* </BadgeAnchor>
*/
export function BadgeAnchor({
children,
content,
placement,
isCircle,
isInvisible,
label,
variant = "danger",
appearance = "solid",
size = "sm",
isSquare,
max,
icon,
className,
}: BadgeAnchorProps) {
const styles = badgeAnchorStyles({ placement, isCircle, isInvisible });
const isDot = content === undefined && icon === undefined;
return (
<span className={cn(styles.root(), className)}>
{children}
<Badge
variant={variant}
appearance={appearance}
size={size}
isSquare={isSquare}
max={max}
icon={icon}
// The label stands for the whole badge, so it is a single image.
{...(label ? { role: "img", "aria-label": label } : {})}
className={cn(
styles.badge(),
isDot && "size-2.5 min-w-0 p-0",
isInvisible && "pointer-events-none",
)}
>
{content}
</Badge>
</span>
);
}It imports XIcon from @/components/icons for the remove button. It has no client code, so it works in Server Components.
3. Use it
import { Badge, BadgeAnchor } from "@/components/ui/badge/badge";Anatomy
A label, and a wrapper that pins one to another element.
<Badge variant="success" hasDot>Paid</Badge>
<BadgeAnchor content={3} label="3 unread messages">
<Avatar name="Ana Wijaya" />
</BadgeAnchor>| Part | Rendered as | Notes |
|---|---|---|
| Badge | span | The label: text with a color. It is not a button and does nothing by itself. |
| Dot | span, hidden from screen readers | A small circle before the text, with hasDot. |
| Icon | any element | Before the text, with icon. |
| Remove button | button | At the end, with isRemovable. It calls onRemove. |
| BadgeAnchor | span around the element | Pins a Badge to a corner of what it wraps. Without content the badge is a dot. |
Examples
Usage
Variants
The color. Default is neutral, and the others are the theme's success, warning and danger.
Appearance
How the color is used: a soft tint, a solid fill for more weight, or an outline for less.
Sizes
Square
isSquare rounds the corners instead of making a pill.
With a dot
A dot in the color of the text, for a status.
With an icon
Counts
A number is shown as it is. Set max, and a number over it shows as max and a +.
Removable
isRemovable adds a button that calls onRemove. Name it with removeLabel, so a screen reader hears which one it removes.
Next to text
A small badge beside a heading or a menu item.
Billing Beta
Badge anchor
BadgeAnchor pins a badge to a corner of the element it wraps. It is a solid, small, danger badge by default, for counts.
On an element
A count on an icon button. The label is what a screen reader hears for the badge, since a number alone says little.
On an avatar
A round element has no corner. isCircle puts the badge on its rim.
Placement
The corner it sits on. Top right is the default.
Dot
Without content the badge is a plain dot, for something new or a status. Give it a label, since it has no text.
Invisible
isInvisible hides the badge without moving anything, for a count that reached zero.
Component API
Badge and BadgeAnchor take the props below.
Badge
| Prop | Type | Default |
|---|---|---|
| children | ReactNode | number | - |
| variant | "default" | "success" | "warning" | "danger" | "default" |
| appearance | "soft" | "solid" | "outline" | "soft" |
| size | "sm" | "md" | "lg" | "md" |
| isSquare | boolean | false |
| hasDot | boolean | false |
| icon | ReactNode | - |
| max | number | - |
| isRemovable | boolean | false |
| onRemove | () => void | - |
| removeLabel | string | "Remove" |
| aria-label | string | - |
| className | string | - |
BadgeAnchor
| Prop | Type | Default |
|---|---|---|
| children | ReactNode | - (the element, required) |
| content | ReactNode | number | - (a dot) |
| placement | "top-right" | "top-left" | "bottom-right" | "bottom-left" | "top-right" |
| isCircle | boolean | false |
| isInvisible | boolean | false |
| label | string | - |
| variant | as Badge | "danger" |
| appearance | as Badge | "solid" |
| size | as Badge | "sm" |
| max, icon, isSquare | as Badge | - |
| className | string | - |