Badge

A short label for a status or a count.

Status variants are tints of their color, so a row of them stays calm. Wrap the text in a link to make a badge clickable.

Preview
variants.tsx
import { Badge } from "~/components/ui/badge"

export default function Example() {
  return (
    <>
      <Badge>Published</Badge>
      <Badge variant="secondary">Draft</Badge>
      <Badge variant="outline">Archived</Badge>
      <Badge variant="success">Ready</Badge>
      <Badge variant="warning">19 warnings</Badge>
      <Badge variant="destructive">2 errors</Badge>
    </>
  )
}

Dependencies

What it's built from and what's built from it in Base, read from the imports in its source.

UsesTextprimitive
Used byNothing yet.
Packages tailwind-variants@4.1.0

Installation

Set the project up for Base once first, as Getting started shows. Then add it with the CLI, or by hand.

With the CLI

terminal
npx shadcn@latest add https://design.zepez.dev/r/base/badge.json

Manually

Install its packages:

terminal
npm install tailwind-variants@4.1.0

Add the components it uses first, listed under Dependencies.

Then copy the source:

components/ui/badge.tsx
import type * as React from "react"
import { tv, type VariantProps } from "tailwind-variants"

import { textVariants } from "~/components/ui/text"
import { cn } from "~/lib/utils"

const badgeVariants = tv({
  base: [
    textVariants({ size: "xs", weight: "medium" }),
    "inline-flex h-5 w-fit shrink-0 items-center justify-center gap-2xs overflow-hidden rounded-lg border border-transparent px-sm py-3xs whitespace-nowrap",
    "transition-colors outline-none focus-visible:border-ring focus-visible:ring-3 focus-visible:ring-ring/30",
    "[&>svg]:pointer-events-none [&>svg]:size-3!",
  ],
  variants: {
    variant: {
      primary: "bg-primary text-primary-foreground [a&]:hover:bg-primary/80",
      secondary: "bg-secondary text-secondary-foreground [a&]:hover:bg-secondary/80",
      outline: "border-border text-foreground [a&]:hover:bg-muted",
      destructive:
        "bg-destructive/10 text-destructive dark:bg-destructive/20 [a&]:hover:bg-destructive/20",
      warning: "bg-warning/10 text-warning dark:bg-warning/20 [a&]:hover:bg-warning/20",
      success: "bg-success/10 text-success dark:bg-success/20 [a&]:hover:bg-success/20",
    },
  },
  defaultVariants: { variant: "primary" },
})

type BadgeProps = React.ComponentProps<"span"> & {
  /** What the badge says about its subject. The status variants are tints. */
  variant?: NonNullable<VariantProps<typeof badgeVariants>["variant"]>
}

/** A short label for a status or count. Render it as a link with an `<a>` of your own around its text. */
function Badge({ variant, className, ...props }: BadgeProps) {
  return <span data-slot="badge" className={cn(badgeVariants({ variant }), className)} {...props} />
}

export { Badge, badgeVariants }
export type { BadgeProps }

API reference

Generated from Base's types. Props marked ours are added here; the rest come from the ui-sdk part it wraps. Every part also accepts its element's native attributes.

Badge

A short label for a status or count. Render it as a link with an <a> of your own around its text.

variantours

What the badge says about its subject. The status variants are tints.

Type NonNullable<"primary" | "destructive" | "warning" | "success" | "secondary" | "outline">