# Badge

> Animated badge for React and shadcn for statuses and counts: counts roll their digits, and a count-only badge leaves at zero.

A status or count chip in six tones: counts roll their digits, and a count-only badge pops in above zero and leaves when cleared. A live dot breathes slowly and stops under reduced motion.

Also known as a chip.

- Tier: Free (MIT)
- Interaction: Press
- Page: https://eased.dev/components/badge
- Installs: motion
- Registry: https://eased.dev/r/badge.json

## Install

```bash
npx shadcn add https://eased.dev/r/badge.json
```

A status or count chip: counts roll their digits, a count-only badge comes and goes with its count, and a live dot breathes.

## When to use

- A short status on a row or a card: Operational, Degraded, Beta, Draft.
- An unread or pending count on a nav item or a tab, which should disappear at zero.
- A label with a number: "Queued 4".

## When not to use

- A message that needs a sentence or an action: use an [Alert](/components/alert).
- Something a person can click, toggle or remove: use a [Button](/components/button) or a [Toggle Group](/components/toggle-group).
- A large standalone number: use [Number Roll](/components/number-roll) at the size you need.

## Usage

```tsx service-status.tsx
import { Badge } from "@/components/eased/badge/badge";

export function ServiceStatus({ unread }: { unread: number }) {
  return (
    <div className="flex items-center gap-2">
      <Badge tone="success" dot="live">Operational</Badge>
      <Badge count={unread} tone="brand" aria-label={`${unread} unread`} />
    </div>
  );
}
```

## Examples

### Tones and dots

Six tones, each a subtle fill with text in the state colour. `dot` adds a status dot in the text colour.

```tsx
<Badge tone="warning" dot>Degraded</Badge>
<Badge tone="info">Beta</Badge>
<Badge tone="danger" dot>Failed</Badge>
```

### Counts

A badge with a label shows its count after the label. A count-only badge leaves at 0 unless `showZero` is set; keep it mounted and let the count drive it.

```tsx
<Badge count={4}>Queued</Badge>
<Badge count={128} max={99} />  {/* shows 99+ */}
<Badge count={0} showZero />
```

## API reference

### Badge

| Prop | Type | Default | Description |
|---|---|---|---|
| `children` | `ReactNode` | | The label. Optional for count-only badges. |
| `count` | `number` | | Rolls when it changes. Shown after the label. |
| `max` | `number` | `99` | Counts above this show as `max+`. |
| `showZero` | `boolean` | `false` | Keep a count-only badge at 0; otherwise it leaves. |
| `dot` | `boolean \| "live"` | `false` | A status dot before the label; `"live"` breathes. |
| `tone` | `"neutral" \| "brand" \| "success" \| "warning" \| "danger" \| "info"` | `"neutral"` | |
| `size` | `"sm" \| "md"` | `"sm"` | 20px or 24px tall. |
| `locale` | `Intl.LocalesArgument` | | Formats the count. |
| `aria-label` | `string` | | Spoken instead of the visible text. |
| `className` | `string` | `""` | Appended to the badge's classes. |

## Accessibility

- With `aria-label`, the label is read and the visible text and count are hidden from assistive tech, so "3 unread" is read instead of "3".
- Without it, the visible text and the formatted count are read as they are.
- The dot is decorative and hidden; the tone colour is never the only signal, so the label must say the state.
- The badge is not a live region: a changing count is not announced on its own.

## Motion

Counts roll their digits in the direction of change on the snappy spring. A count-only badge pops in from 0.85 scale on the snappy spring and leaves in 100ms to 0.9, faster than it came. The live dot's halo swells to 2.25× and fades back every 2.4s, and pauses while off-screen. Reduced motion: the badge fades in and out in 100ms with no scale, the count changes without rolling, and the halo stops.

## Notes for AI

Use Badge for short statuses and counts. Pass `count` rather than putting a number in `children`, so it rolls and respects `max`, and give count-only badges an `aria-label` that says what is counted.

Do not wrap a count-only badge in your own conditional to hide it at zero: keep it mounted so it can animate out. Do not use the tone alone to carry meaning; the label should name the state.

## Related

- [Number Roll](/components/number-roll): the rolling digits on their own.
- [Avatar](/components/avatar): a picture to pair with a status badge.
- [Alert](/components/alert): a status that needs a sentence.

## Example

`components/eased/badge/badge-demo.tsx`

```tsx
"use client";

import { useState } from "react";
import { Button } from "../button/button";
import { DemoFrame } from "../lib/demo-frame";
import { rowGridRow } from "../lib/layout";
import { Badge } from "./badge";

type Folder = { id: string; name: string; count: number; icon: string };

const START: Folder[] = [
  { id: "inbox", name: "Inbox", count: 12, icon: "M3 9.5h3.5l1 2h3l1-2H15M3 9.5V13a1 1 0 0 0 1 1h10a1 1 0 0 0 1-1V9.5M3 9.5 5 4h8l2 5.5" },
  { id: "mentions", name: "Mentions", count: 3, icon: "M11.5 9a2.5 2.5 0 1 1-5 0 2.5 2.5 0 0 1 5 0Zm0 0v1a1.75 1.75 0 0 0 3.5 0V9a6 6 0 1 0-2.4 4.8" },
  { id: "review", name: "Needs review", count: 98, icon: "M4 9.5 7.5 13 14 5" },
  { id: "drafts", name: "Drafts", count: 0, icon: "M11.5 4.5l2 2L7 13H5v-2l6.5-6.5Z" },
];

export function BadgeDemo() {
  const [folders, setFolders] = useState(START);
  const [selected, setSelected] = useState("inbox");

  // Opening a folder reads it, and its badge leaves.
  const open = (id: string) => {
    setSelected(id);
    setFolders((f) => f.map((x) => (x.id === id ? { ...x, count: 0 } : x)));
  };
  // New mail lands in the folders you're not looking at; "Needs review" crosses 99 → 99+.
  const deliver = () =>
    setFolders((f) =>
      f.map((x) =>
        x.id === selected || x.id === "drafts" ? x : { ...x, count: x.count + 1 + Math.floor(Math.random() * 3) },
      ),
    );

  return (
    <DemoFrame
      title="Mail"
      actions={
        <Button size="sm" onClick={deliver}>
          Receive mail
        </Button>
      }
    >
      <nav aria-label="Folders" className="p-1.5 pb-2.5">
        <ul>
          {folders.map((f) => (
            <li key={f.id}>
              <button
                type="button"
                onClick={() => open(f.id)}
                aria-current={selected === f.id ? "page" : undefined}
                className={`flex h-9 w-full cursor-pointer items-center gap-2.5 rounded-control px-2.5 text-left text-ui-sm transition-[color,background-color,border-color,box-shadow] duration-150 ease-standard focus-ring ${
                  selected === f.id ? "bg-surface-hover text-text" : "text-text-secondary hover-fine:bg-surface-hover hover-fine:text-text"
                }`}
              >
                <svg viewBox="0 0 18 18" aria-hidden className="size-4 shrink-0 fill-none stroke-current" strokeWidth={1.5} strokeLinecap="round" strokeLinejoin="round">
                  <path d={f.icon} />
                </svg>
                <span className="min-w-0 flex-1 truncate">{f.name}</span>
                <Badge count={f.count} tone={f.id === "mentions" ? "brand" : "neutral"} aria-label={`${f.count} unread`} />
              </button>
            </li>
          ))}
        </ul>
      </nav>

      <div className="mx-4 border-t border-line-subtle" />

      <section aria-labelledby="badge-demo-status" className="flex flex-col gap-3 p-4">
        <h3 id="badge-demo-status" className="text-ui-xs font-medium tracking-caps text-text-muted uppercase">
          Services
        </h3>
        {/* The badge track is as wide as the widest badge, so every badge starts on one x. */}
        <ul className="grid grid-cols-[minmax(0,1fr)_auto] items-center gap-x-3 text-ui-sm">
          <Service name="API" badge={<Badge tone="success" dot="live">Operational</Badge>} />
          <Service name="Webhooks" badge={<Badge tone="warning" dot>Degraded</Badge>} />
          <Service name="Search" badge={<Badge tone="info">Beta</Badge>} />
          <Service name="Exports" badge={<Badge tone="neutral" count={4}>Queued</Badge>} />
        </ul>
      </section>
    </DemoFrame>
  );
}

function Service({ name, badge }: { name: string; badge: React.ReactNode }) {
  return (
    <li className={`${rowGridRow} h-9`}>
      <span className="truncate text-text-secondary">{name}</span>
      <span className="flex">{badge}</span>
    </li>
  );
}
```
