Badge

Displays a badge or a component that looks like a badge.

Badge

Installation

Copy and paste the following code into your project.

import { type VariantProps, tv } from "tailwind-variants";
 
export const BadgeStyles = tv({
  base: [
    "inline-flex items-center rounded-md border px-2.5 py-0.5 font-semibold",
    "text-xs outline-none ring-offset-2 ring-offset-background transition",
  ],
  variants: {
    variant: {
      default: [
        "border-transparent bg-primary text-primary-foreground shadow",
        "hover:bg-primary/80",
      ],
      secondary: [
        "border-transparent bg-secondary text-secondary-foreground",
        "hover:bg-secondary/80",
      ],
      destructive: [
        "border-transparent bg-destructive text-destructive-foreground shadow",
        "hover:bg-destructive/80",
      ],
      outline: ["text-foreground"],
    },
    nativeFocus: {
      true: ["focus:ring-2 focus:ring-ring"],
      false: ["data-[focused=true]:ring-2 data-[focused=true]:ring-ring"],
    },
  },
  defaultVariants: {
    variant: "default",
    nativeFocus: true,
  },
});
 
export type BadgeProps = React.ComponentProps<"div"> &
  VariantProps<typeof BadgeStyles>;
 
export function Badge({
  className,
  variant,
  nativeFocus,
  ...props
}: BadgeProps) {
  return (
    <div
      {...props}
      className={BadgeStyles({ className, variant, nativeFocus })}
    />
  );
}

Update the import paths to match your project setup.

Usage

import { Badge } from "~/components/ui/badge";
<Badge variant="outline">Badge</Badge>

You can use the BadgeStyles helper to create a link that looks like a badge.

import { BadgeStyles } from "~/components/ui/badge";
<Link className={BadgeStyles({ variant: "outline" })}>Badge</Link>

Examples

Default

Badge

Secondary

Secondary

Outline

Outline

Destructive

Destructive

API Reference

PropTypeDefaultDescription
variant"default" | "secondary" | "outline" | "destructive""default"The visual style of the badge.
nativeFocustrue | falsetrueUse CSS native :focus or [data-focused="true"] to show the ring when badge is focused.