← Components

Communication · Material 3: Badges

Badge

A static label for status and counts. Counts roll; gaining a digit stretches the badge. A dot variant marks something new.

Examples and variants

Variants and tones

subtle (default) and solid.

DraftBetaLiveReviewFailedNewUrgent
Example
function BadgeVariants() {
  return (
    <div className="flex flex-wrap items-center justify-center gap-2">
      <Badge>Draft</Badge>
      <Badge tone="info">Beta</Badge>
      <Badge tone="success">Live</Badge>
      <Badge tone="warning">Review</Badge>
      <Badge tone="danger">Failed</Badge>
      <Badge variant="solid">New</Badge>
      <Badge variant="solid" tone="danger">
        Urgent
      </Badge>
    </div>
  )
}

Count and dot

Press +1: the count rolls; from 9 to 10 it stretches.

9
Example
function BadgeCount() {
  const [count, setCount] = useState(9)
  return (
    <div className="flex items-center gap-4">
      <span className="relative inline-flex">
        <Button size="icon" variant="secondary" aria-label={`Notifications, ${count} unread`}>
          <BellIcon />
        </Button>
        <Badge variant="solid" tone="danger" count={count} className="absolute -top-1 -right-2" />
      </span>
      <span className="relative inline-flex">
        <Button size="icon" variant="secondary" aria-label="Messages, new">
          <BellIcon />
        </Button>
        <Badge dot aria-label="New" className="absolute top-1 right-1" />
      </span>
      <Button size="sm" variant="secondary" onClick={() => setCount((value) => value + 1)}>
        +1
      </Button>
    </div>
  )
}

Spec drawing

Measured from the live component at default density. Each code maps to the token that sets it; change the token and the drawing follows.

Install

With the CLI

The shadcn CLI only delivers files: it copies our code from the Isotopi registry into your project and installs the packages it uses. Add the foundation once, then the component.

Terminal
npx shadcn@latest add https://isotopi.com/r/minimal.json
npx shadcn@latest add https://isotopi.com/r/badge.json

By hand

No CLI needed. Add the foundation once (see Getting started), then follow these steps. They include every component this one builds on.

  1. Install the packages it uses.

    Terminal
    npm install class-variance-authority cn
  2. Copy this file to the same paths in your project. Imports use the @/ alias for your project root.

    • components/ui/badge.tsxbadge
      components/ui/badge.tsx
      "use client"
      
      import * as React from "react"
      import { cva, type VariantProps } from "class-variance-authority"
      import { cn } from "cn"
      
      /*
        Badge: a static label for status, counts or categories. Not interactive
        (removable or pressable tokens are Chip). With `count`, the number rolls up
        or down when it changes, and the badge stretches when it gains a digit.
      */
      const badgeVariants = cva(
        "inline-flex h-(--badge-height) shrink-0 items-center justify-center gap-1 overflow-hidden rounded-(--badge-corner) px-(--badge-padding-x) text-xs font-medium whitespace-nowrap tabular-nums [&_svg]:size-3",
        {
          variants: {
            variant: { subtle: "", solid: "" },
            tone: { neutral: "", info: "", success: "", warning: "", danger: "" },
          },
          compoundVariants: [
            { variant: "subtle", tone: "neutral", className: "bg-muted text-foreground" },
            { variant: "subtle", tone: "info", className: "bg-(--color-info-subtle) text-(--color-info-fg)" },
            { variant: "subtle", tone: "success", className: "bg-(--color-success-subtle) text-(--color-success-fg)" },
            { variant: "subtle", tone: "warning", className: "bg-(--color-warning-subtle) text-(--color-warning-fg)" },
            { variant: "subtle", tone: "danger", className: "bg-(--color-danger-subtle) text-(--color-danger-fg)" },
            { variant: "solid", tone: "neutral", className: "bg-primary text-primary-foreground" },
            { variant: "solid", tone: "danger", className: "bg-(--color-danger) text-(--color-fg-on-accent)" },
            { variant: "solid", tone: "success", className: "bg-(--color-success) text-(--color-fg-on-accent)" },
            { variant: "solid", tone: "info", className: "bg-(--color-info) text-(--color-fg-on-accent)" },
            { variant: "solid", tone: "warning", className: "bg-(--color-warning) text-foreground" },
          ],
          defaultVariants: { variant: "subtle", tone: "neutral" },
        }
      )
      
      type BadgeProps = React.ComponentProps<"span"> &
        VariantProps<typeof badgeVariants> & {
          /** A number that rolls when it changes. Shown after children. */
          count?: number
          /** Above this, shows "max+". */
          max?: number
          /** A small dot with no label (for navigation items and icons). Needs an aria-label. */
          dot?: boolean
        }
      
      function Badge({ className, variant, tone, count, max = 99, dot = false, children, ...props }: BadgeProps) {
        const previous = React.useRef(count)
        const [roll, setRoll] = React.useState<{ dir: "up" | "down"; grew: boolean; id: number } | null>(null)
      
        React.useEffect(() => {
          const before = previous.current
          previous.current = count
          if (before === undefined || count === undefined || before === count) return
          setRoll({
            dir: count > before ? "up" : "down",
            grew: String(Math.min(count, max + 1)).length > String(Math.min(before, max + 1)).length,
            id: Date.now(),
          })
        }, [count, max])
      
        const shown = count === undefined ? null : count > max ? `${max}+` : String(count)
      
        if (dot) {
          return (
            <span
              data-slot="badge"
              data-dot=""
              role="status"
              className={cn(badgeVariants({ variant: variant ?? "solid", tone: tone ?? "danger" }), "size-(--badge-dot-size) p-0", className)}
              {...props}
            />
          )
        }
      
        return (
          <span
            data-slot="badge"
            key={roll?.grew ? roll.id : undefined}
            className={cn(badgeVariants({ variant, tone }), roll?.grew && "badge-stretch", className)}
            {...props}
          >
            {children}
            {shown !== null ? (
              <span key={roll?.id ?? "n"} className={roll ? `badge-roll-${roll.dir}` : undefined}>
                {shown}
              </span>
            ) : null}
          </span>
        )
      }
      
      export { Badge, badgeVariants }
      

Imports

What the examples on this page import:

Imports
import { BellIcon } from "lucide-react"
import { Badge } from "@/components/ui/badge"
import { Button } from "@/components/ui/button"

Anatomy

  1. 1Container · --badge-height, --badge-padding-x, --badge-corner; dot: --badge-dot-size.
  2. 2Label or count · --badge-label-size, tabular numbers.

Props

PropTypeDefaultDescription
variant"subtle" | "solid""subtle"Emphasis.
tone"neutral" | "info" | "success" | "warning" | "danger""neutral"Meaning.
countnumber—Rolls on change.
maxnumber99Shows max+ above it.
dotboolean—A dot with no label. Needs aria-label.

Tokens

Every visual spec is a component token in styles/tokens/components.css, pointing at a semantic token. Change a token to restyle the component; themes change the semantic layer.

TokenPoints toLightDark
--badge-heightvar(--space-5)20pxsame
--badge-padding-xvar(--space-2)8pxsame
--badge-cornervar(--shape-full)999pxsame
--badge-label-sizevar(--type-caption-size)11pxsame
--badge-dot-sizevar(--space-2)8pxsame

Motion

TriggerWhat happensTokens
Count changeDigits roll up or down; a new digit stretches the badge.--spring-snappy, --spring-bouncy

Calm keeps the motion without overshoot. Still (and reduced motion) keeps a designed frame with no movement.

Accessibility

  • Counts on icons belong in the button's aria-label.
  • Don't rely on colour alone.

Usage

Do

  • Use for status and counts.

Don’t

  • Don't make badges clickable (use Chip).

Code

components/ui/badge.tsx
"use client"

import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "cn"

/*
  Badge: a static label for status, counts or categories. Not interactive
  (removable or pressable tokens are Chip). With `count`, the number rolls up
  or down when it changes, and the badge stretches when it gains a digit.
*/
const badgeVariants = cva(
  "inline-flex h-(--badge-height) shrink-0 items-center justify-center gap-1 overflow-hidden rounded-(--badge-corner) px-(--badge-padding-x) text-xs font-medium whitespace-nowrap tabular-nums [&_svg]:size-3",
  {
    variants: {
      variant: { subtle: "", solid: "" },
      tone: { neutral: "", info: "", success: "", warning: "", danger: "" },
    },
    compoundVariants: [
      { variant: "subtle", tone: "neutral", className: "bg-muted text-foreground" },
      { variant: "subtle", tone: "info", className: "bg-(--color-info-subtle) text-(--color-info-fg)" },
      { variant: "subtle", tone: "success", className: "bg-(--color-success-subtle) text-(--color-success-fg)" },
      { variant: "subtle", tone: "warning", className: "bg-(--color-warning-subtle) text-(--color-warning-fg)" },
      { variant: "subtle", tone: "danger", className: "bg-(--color-danger-subtle) text-(--color-danger-fg)" },
      { variant: "solid", tone: "neutral", className: "bg-primary text-primary-foreground" },
      { variant: "solid", tone: "danger", className: "bg-(--color-danger) text-(--color-fg-on-accent)" },
      { variant: "solid", tone: "success", className: "bg-(--color-success) text-(--color-fg-on-accent)" },
      { variant: "solid", tone: "info", className: "bg-(--color-info) text-(--color-fg-on-accent)" },
      { variant: "solid", tone: "warning", className: "bg-(--color-warning) text-foreground" },
    ],
    defaultVariants: { variant: "subtle", tone: "neutral" },
  }
)

type BadgeProps = React.ComponentProps<"span"> &
  VariantProps<typeof badgeVariants> & {
    /** A number that rolls when it changes. Shown after children. */
    count?: number
    /** Above this, shows "max+". */
    max?: number
    /** A small dot with no label (for navigation items and icons). Needs an aria-label. */
    dot?: boolean
  }

function Badge({ className, variant, tone, count, max = 99, dot = false, children, ...props }: BadgeProps) {
  const previous = React.useRef(count)
  const [roll, setRoll] = React.useState<{ dir: "up" | "down"; grew: boolean; id: number } | null>(null)

  React.useEffect(() => {
    const before = previous.current
    previous.current = count
    if (before === undefined || count === undefined || before === count) return
    setRoll({
      dir: count > before ? "up" : "down",
      grew: String(Math.min(count, max + 1)).length > String(Math.min(before, max + 1)).length,
      id: Date.now(),
    })
  }, [count, max])

  const shown = count === undefined ? null : count > max ? `${max}+` : String(count)

  if (dot) {
    return (
      <span
        data-slot="badge"
        data-dot=""
        role="status"
        className={cn(badgeVariants({ variant: variant ?? "solid", tone: tone ?? "danger" }), "size-(--badge-dot-size) p-0", className)}
        {...props}
      />
    )
  }

  return (
    <span
      data-slot="badge"
      key={roll?.grew ? roll.id : undefined}
      className={cn(badgeVariants({ variant, tone }), roll?.grew && "badge-stretch", className)}
      {...props}
    >
      {children}
      {shown !== null ? (
        <span key={roll?.id ?? "n"} className={roll ? `badge-roll-${roll.dir}` : undefined}>
          {shown}
        </span>
      ) : null}
    </span>
  )
}

export { Badge, badgeVariants }