# Badge

A short label in a pill: a status, a stage, or a tag a lawyer can remove. `tone` picks the colour from the status palette and `variant` the treatment, soft or outline. An icon can sit before the text, and a remove × or an icon after it. CountBadge is the quiet count beside a tab or heading.

- Beta: ready to use; its API can still change in a minor release.
- Category: Status & indicators

```tsx
import { Badge, BadgeGroup, BadgePresence, CountBadge } from "@leap-crm/design-system";
```

## Best practices

- Do: Write the status in the badge, like Overdue or Converted. The tone adds emphasis, but colour is never the only signal.
- Do: Pick the colour with `tone` and the treatment with `variant`: soft for most badges, outline where a tinted fill would crowd a dense row.
- Do: Use `size="sm"` in dense rows like a table cell, and `xs` only for small labels like Beta.
- Do: Use `CountBadge` for the number of items beside a tab or section heading, and pass "99+" past 99 so the pill stays short.
- Do: Wrap badges that come and go in `BadgeGroup`, or a single one in `BadgePresence`, so a removed badge fades and the row closes up instead of jumping.
- Don't: Put a clickable icon in `endIcon`. It is decorative; for a label a lawyer can take off, pass `onRemove`, which gives the × a name and a 24 px target.
- Don't: Tint a whole row or card in a badge's tone. Colour is an accent: keep it in the badge and leave the surface neutral.

## Related

- [Tag Input](./tag-input.md)
- [Status Mark](./status-mark.md)
- [Interaction Type Badge](./interaction-type-badge.md)
- [Record Chip](./record-chip.md)

## Examples

What the examples import:

```tsx
import { Fragment, useState } from "react";
import { Badge, BadgeGroup, BadgePresence, Button, CheckIcon, ClockIcon, CountBadge } from "@leap-crm/design-system";
```

Scaffolding the examples call. It lives in the docs page, not in the library:

```tsx
const tagPool = [
  { id: "vip", label: "VIP", tone: "purple" },
  { id: "referral", label: "Referral", tone: "teal" },
  { id: "overdue", label: "Overdue", tone: "warning" },
  { id: "on-hold", label: "On hold", tone: "yellow" },
] as const;

const matterTags = [
  { name: "Urgent", tone: "destructive" },
  { name: "Family law", tone: "purple" },
  { name: "Referral", tone: "teal" },
  { name: "VIP", tone: "yellow" },
] as const;

function RemovableTags() {
  const [names, setNames] = useState<string[]>(matterTags.map(tag => tag.name));
  return (
    <div className="flex flex-col items-start gap-3">
      <BadgeGroup className="min-h-5.5">
        {matterTags
          .filter(tag => names.includes(tag.name))
          .map(tag => (
            <Badge
              key={tag.name}
              tone={tag.tone}
              onRemove={() =>
                setNames(current => current.filter(name => name !== tag.name))
              }
            >
              {tag.name}
            </Badge>
          ))}
      </BadgeGroup>
      <Button
        intent="ghost"
        size="xs"
        onClick={() => setNames(matterTags.map(tag => tag.name))}
      >
        Put them back
      </Button>
    </div>
  );
}

function ComingAndGoing() {
  const [count, setCount] = useState(0);
  const [tags, setTags] = useState<string[]>(["vip"]);
  const toggle = (id: string) =>
    setTags(current =>
      current.includes(id)
        ? current.filter(tag => tag !== id)
        : tagPool
            .map(tag => tag.id)
            .filter(tag => tag === id || current.includes(tag)),
    );
  return (
    <div className="flex flex-col gap-5">
      <div className="flex items-center gap-4">
        <span className="text-body text-neutral-text flex items-center gap-2">
          Tasks
          <BadgePresence show={count > 0}>
            <CountBadge value={count} />
          </BadgePresence>
          <span className="text-neutral-text-muted">due today</span>
        </span>
        <Button
          intent="outline"
          size="sm"
          onClick={() => setCount(value => value + 1)}
        >
          Add task
        </Button>
        <Button intent="ghost" size="sm" onClick={() => setCount(0)}>
          Clear
        </Button>
      </div>
      <div className="flex flex-col gap-2">
        <BadgeGroup className="min-h-5.5">
          {tagPool
            .filter(tag => tags.includes(tag.id))
            .map(tag => (
              <Badge key={tag.id} tone={tag.tone}>
                {tag.label}
              </Badge>
            ))}
        </BadgeGroup>
        <div className="flex flex-wrap gap-2">
          {tagPool.map(tag => (
            <Button
              key={tag.id}
              intent="ghost"
              size="xs"
              aria-pressed={tags.includes(tag.id)}
              onClick={() => toggle(tag.id)}
            >
              {tag.label}
            </Button>
          ))}
        </div>
      </div>
    </div>
  );
}

const tones = [
  "default",
  "info",
  "success",
  "warning",
  "destructive",
  "teal",
  "purple",
  "yellow",
  "slate",
  "plum",
] as const;

const labels: Record<(typeof tones)[number], string> = {
  default: "Lead",
  info: "Enquired",
  success: "Converted",
  warning: "Overdue",
  destructive: "Lost",
  teal: "Agreement",
  purple: "Referral",
  yellow: "On hold",
  slate: "Archived",
  plum: "Disqualified",
};
```

### Tones and sizes

md (22 px, 12/16) by default, sm (18 px, 11/16) in dense rows, xs (15 px, 10/14) for labels like Beta.

```tsx
<div className="grid grid-cols-[auto_repeat(3,auto)] items-center justify-start justify-items-start gap-x-6 gap-y-3">
  <span />
  {(["md", "sm", "xs"] as const).map(size => (
    <span key={size} className="text-small text-neutral-text-muted">
      {size}
    </span>
  ))}
  {tones.map(tone => (
    <Fragment key={tone}>
      <span className="text-small text-neutral-text-muted capitalize">
        {tone}
      </span>
      {(["md", "sm", "xs"] as const).map(size => (
        <Badge key={`${tone}-${size}`} tone={tone} size={size}>
          {labels[tone]}
        </Badge>
      ))}
    </Fragment>
  ))}
</div>
```

### Soft and outline

soft is the tone's tinted fill and border. outline keeps the tone's border and text on no fill.

```tsx
<div className="grid grid-cols-[auto_repeat(5,auto)] items-center justify-start justify-items-start gap-x-4 gap-y-3">
  {(["soft", "outline"] as const).map(variant => (
    <Fragment key={variant}>
      <span className="text-small text-neutral-text-muted">
        {variant}
      </span>
      {(
        [
          "default",
          "info",
          "success",
          "warning",
          "destructive",
        ] as const
      ).map(tone => (
        <Badge key={tone} tone={tone} variant={variant}>
          {labels[tone]}
        </Badge>
      ))}
    </Fragment>
  ))}
</div>
```

### Icons

icon sits before the text and endIcon after it, at 12 px in md and 10 px in sm and xs. The pill tightens on the icon's side.

```tsx
<div className="flex flex-wrap items-center gap-3">
  <Badge tone="success" icon={<CheckIcon />}>
    Approved
  </Badge>
  <Badge tone="warning" icon={<ClockIcon />}>
    Due today
  </Badge>
  <Badge tone="success" size="sm" icon={<CheckIcon />}>
    Approved
  </Badge>
  <Badge tone="info" endIcon={<ClockIcon />}>
    Scheduled
  </Badge>
</div>
```

### Removable

onRemove puts a × after the text. Its target reaches 24 px without growing the badge, and it is named Remove <text>. In a BadgeGroup the row closes up behind a removed badge.

```tsx
<RemovableTags />
```

### Count

Muted fill, secondary numeral at 9 px semibold: md (16 px) beside tabs and buttons, sm (14 px) beside inline section heads.

```tsx
<div className="text-body flex items-center gap-6">
  <span className="inline-flex items-center gap-1.5">
    Open tasks <CountBadge value={12} />
  </span>
  <span className="inline-flex items-center gap-1.5">
    Notes <CountBadge value={3} />
  </span>
  <span className="text-small inline-flex items-center gap-1">
    Documents <CountBadge value={128} size="sm" />
  </span>
</div>
```

### Coming and going

BadgePresence shows one badge when there's something to show, like a count above zero: its space opens and it turns in, and leaving it fades before the row closes up, so the words beside it slide rather than jump. BadgeGroup does the same for a row of keyed badges, like a record's tags. Pass floating for a badge pinned over an icon, which turns in where it sits. Reduced motion fades.

```tsx
<ComingAndGoing />
```

## Props

Badge also takes every attribute of a span.

### Badge

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` | `ReactNode` |  | What it shows inside. |
| `tone` | `Tone` | `"default"` | The colour, from the status palette. Pair it with words: the colour is never the only signal. |
| `variant` | `"soft" \| "outline"` | `"soft"` | soft (the default) is the tone's tinted fill and border; outline keeps the tone's border and text on no fill. |
| `size` | `"md" \| "sm" \| "xs"` | `"md"` | md (22 px) by default; sm (18 px) in dense rows; xs (15 px) for labels like Beta. |
| `icon` | `ReactNode` |  | An icon before the text, like a tick on Approved. |
| `endIcon` | `ReactNode` |  | A decorative icon after the text. With onRemove, the × takes this place. |
| `onRemove` | `() => void` |  | Shows a remove × after the text, like a tag a lawyer added. Called when it's pressed. |
| `removeLabel` | `string` |  | The remove button's name. "Remove <text>" by default. |

### CountBadge

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `value` (required) | `number \| string` |  | The count. Past 99, pass "99+". |
| `size` | `"md" \| "sm"` | `"md"` | md (16 px) beside tabs and buttons; sm (14 px) beside inline section heads. |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |

### BadgePresence

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `show` (required) | `boolean` |  | Whether the badge shows. Turning it off plays the badge out before it unmounts. |
| `gap` | `RowGap` | `8` | The flex gap, in px, between the badge and what sits before it, so that gap opens and closes with it. Leave it out for a floating badge. |
| `floating` | `string` |  | A badge pinned over something, like a count on an icon, takes no space in the row: it turns in and out where it sits. Pass its position classes here, like "absolute -end-0.5 -top-0.5". |
| `children` (required) | `ReactNode` |  | The badge, usually a Badge or CountBadge. |

### BadgeGroup

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `children` (required) | `ReactNode` |  | The badges, each with a key, like a contact's tags. One added or removed opens or closes its own space. |
| `gap` | `RowGap` | `8` | The gap between badges, in px: 4, 6 or 8 (the default). |
| `className` | `string` |  | Classes added to the root element, merged with its own. Tokens only. |
