Skip to Content
Habi

Badge

A short status label, like New, Open or Closing soon.

New

Installation

npm install class-variance-authority clsx tailwind-merge

Add the cn helper if you haven't already:

lib/utils.ts
import { clsx, type ClassValue } from "clsx";
import { extendTailwindMerge } from "tailwind-merge";

const twMerge = extendTailwindMerge({
  extend: {
    theme: {
      text: [
        "display",
        "heading-xl",
        "heading-lg",
        "heading-md",
        "heading-sm",
        "heading-xs",
        "body-lg",
        "body-md",
        "body-sm",
        "label",
        "helper",
        "button",
      ],
      "font-weight": ["regular"],
    },
  },
});

export function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

Add the component:

components/ui/badge.tsx
import type { ComponentProps } from "react";
import { cva, type VariantProps } from "class-variance-authority";

import { cn } from "@/lib/utils";

const badgeVariants = cva(
  "inline-flex w-fit shrink-0 items-center gap-1 rounded-full px-2 py-0.5 text-label whitespace-nowrap [&_svg]:size-4 [&_svg]:shrink-0",
  {
    variants: {
      kind: {
        neutral: "bg-badge-neutral-bg text-badge-neutral-text",
        info: "bg-badge-info-bg text-badge-info-text",
        success: "bg-badge-success-bg text-badge-success-text",
        warning: "bg-badge-warning-bg text-badge-warning-text",
        error: "bg-badge-error-bg text-badge-error-text",
      },
    },
    defaultVariants: {
      kind: "neutral",
    },
  },
);

type BadgeProps = ComponentProps<"span"> & VariantProps<typeof badgeVariants>;

function Badge({ className, kind, ...props }: BadgeProps) {
  return (
    <span
      data-slot="badge"
      className={cn(badgeVariants({ kind }), className)}
      {...props}
    />
  );
}

export { Badge, badgeVariants, type BadgeProps };

Usage

import { Badge } from "@/components/ui/badge";

<Badge kind="success">Open</Badge>;

Examples

Kinds

Pick the kind that matches the meaning: neutral for general labels, info for news, success for something ready or open, warning for something that needs attention soon, and error for something closed or failed.

DraftNewOpenClosing soonClosed

With an icon

An icon can reinforce the meaning. Use the 16px icons from @heroicons/react/16/solid.

ReadyClosing soon

In context

If the badge describes something nearby, add hidden text so screen reader users get the same context.

NBI clearance renewalStatus: Open

Props

PropTypeDefaultDescription
kind"neutral" | "info" | "success" | "warning" | "error""neutral"The meaning of the badge, which sets its color.

Badge also accepts every prop of the HTML <span> element.

Accessibility

  • Words, not color: the label carries the meaning. Color only reinforces it, so a badge still makes sense in grayscale or to people who can't tell colors apart (WCAG 1.4.1).
  • Short labels: one or two words. Badges don't wrap.
  • Not interactive: badges can't be clicked or focused. If people need to pick an option, use a Tag. If it goes somewhere, use a Link.
  • Context: add visually hidden text (sr-only) when the badge only makes sense next to something else, like "Status: Open".
  • Contrast: every kind's text and background pass WCAG AA (5.3–6.8:1).

On this page