Skip to content
Caseroom

Components · Display

Badge

A short status or count next to text.

1.4% of the 2,062 sites in The UI Index have a tag (28 sites).

NewDraftv0.1Failed
  • northwind.devLive
  • Preview #142Building
  • Preview #141Failed

Both previews render the same markup; the HTML tab below is that markup without a framework.

Install

React
npx shadcn@latest add https://www.themes.dev/caseroom/r/badge.json
Astro
npx shadcn@latest add https://www.themes.dev/caseroom/r/astro/badge.json

Or copy the files below into your project. They need clsx, tailwind-merge, and the Caseroom theme. The HTML needs only the compiled CSS.

badge.tsx
import type { HTMLAttributes } from 'react';
import { cn } from '@/lib/utils';
import { badgeVariants, type BadgeVariants } from './badge.variants';

export type BadgeProps = HTMLAttributes<HTMLSpanElement> & BadgeVariants;

/** A short status or count. Not interactive: for a removable tag or a filter, use a button. */
export function Badge({ variant, className, ...props }: BadgeProps) {
  return <span className={cn(badgeVariants({ variant }), className)} {...props} />;
}

export { badgeVariants };
badge.variants.ts
import { cva, type VariantProps } from 'class-variance-authority';

export const badgeVariants = cva(
  'inline-flex w-fit shrink-0 items-center gap-1 rounded-sm border px-1.5 py-0.5 text-xs font-medium whitespace-nowrap [&_svg]:size-3',
  {
    variants: {
      variant: {
        // Foreground, not primary: a badge is a status, so an accent preset (which marks actions) leaves it alone.
        default: 'border-transparent bg-foreground text-background',
        secondary: 'border-transparent bg-secondary text-secondary-foreground',
        outline: 'border-border text-foreground',
        destructive: 'border-transparent bg-destructive text-destructive-foreground',
      },
    },
    defaultVariants: { variant: 'default' },
  },
);

export type BadgeVariants = VariantProps<typeof badgeVariants>;