# Badge

A word circled or coloured in: a pill pulled once around it, shaded, hatched or in red pen.

Docs: https://ballpoint.st9wd.com/docs/badge

```bash
npx shadcn@latest add @ballpoint/badge
```

```tsx
import { Badge } from "@/components/ui/badge"

<Badge>New</Badge>
<Badge variant="outline">v1.2</Badge>
<Badge variant="link" render={<a href="/changelog" />}>Changelog</Badge>
```

Props:

- `variant`: `"default" | "secondary" | "destructive" | "outline" | "ghost" | "link"` (default `"default"`). Coloured in, hatched, red pen, circled, circled on hover, or underlined.
- `render`: `ReactElement`. Render as another element, e.g. a link.
- `size`: `"sm" | "default" | "lg"` (default `"default"`). 20, 24 or 32px high, with the type to match.
- `seed`: `string | number`. Pins the drawing.

Pen settings: `draw`, `roughness`, `radius`, `fill`, `weight`, `speed` (see Pen settings).

## Example

```tsx
import { Badge } from "@/components/ui/badge";

export default function BadgeDemo() {
  return (
    <div className="flex flex-wrap items-center gap-4">
      <Badge seed="badge-default">New</Badge>
      <Badge variant="secondary" seed="badge-secondary">
        Draft
      </Badge>
      <Badge variant="outline" seed="badge-outline">
        v1.2
      </Badge>
      <Badge variant="destructive" seed="badge-destructive">
        Overdue
      </Badge>
      <Badge variant="ghost" seed="badge-ghost">
        Hover me
      </Badge>
      <Badge variant="link" render={<a href="#badge" />} seed="badge-link">
        A link
      </Badge>
      <Badge radius={4} roughness={1.6} seed="badge-square">
        Boxed
      </Badge>
    </div>
  );
}
```

## With icons

Glyphs and counts inside the badge.

```tsx
import { Badge } from "@/components/ui/badge";
import { InkGlyph } from "@/lib/ink-glyphs";

export default function BadgeIcons() {
  return (
    <div className="flex flex-wrap items-center gap-4">
      <Badge seed="bi-verified">
        <InkGlyph name="check" /> Verified
      </Badge>
      <Badge variant="secondary" seed="bi-info">
        <InkGlyph name="info" /> Heads up
      </Badge>
      <Badge variant="destructive" seed="bi-alert">
        <InkGlyph name="alert" /> Failed
      </Badge>
      <Badge variant="outline" seed="bi-count">
        Unread · 12
      </Badge>
    </div>
  );
}
```

## Status in a list

One variant per state, aligned to the right of each row.

```tsx
import { Badge } from "@/components/ui/badge";

const orders = [
  ["Blue ballpoints, box of 50", "Paid", "default"],
  ["Cream notebooks, A5", "Pending", "secondary"],
  ["Fineliner set", "Shipped", "outline"],
  ["Legal pads, pack of 6", "Refunded", "destructive"],
] as const;

export default function BadgeStatus() {
  return (
    <ul className="flex w-full max-w-md flex-col gap-4">
      {orders.map(([item, status, variant]) => (
        <li key={item} className="flex items-center justify-between gap-4">
          <span>{item}</span>
          <Badge variant={variant} seed={`bs-${status}`}>
            {status}
          </Badge>
        </li>
      ))}
    </ul>
  );
}
```

## Sizes

Small, default and large, in three variants.

```tsx
import { Badge } from "@/components/ui/badge";

const sizes = ["sm", "default", "lg"] as const;

export default function BadgeSizes() {
  return (
    <div className="flex flex-col gap-6">
      {(["default", "secondary", "outline"] as const).map((variant) => (
        <div key={variant} className="flex flex-wrap items-center gap-4">
          {sizes.map((size) => (
            <Badge key={size} variant={variant} size={size} seed={`bs-${variant}-${size}`}>
              {size}
            </Badge>
          ))}
        </div>
      ))}
    </div>
  );
}
```

## Filter chips

Badges rendered as toggle buttons: pressed ones are coloured in.

```tsx
"use client";

import { useState } from "react";
import { Badge } from "@/components/ui/badge";

const topics = ["Letters", "Sketches", "Lists", "Recipes", "Margins"];

export default function BadgeFilters() {
  const [on, setOn] = useState<string[]>(["Letters", "Lists"]);
  const toggle = (topic: string) => setOn((list) => (list.includes(topic) ? list.filter((t) => t !== topic) : [...list, topic]));
  return (
    <div className="flex w-full max-w-md flex-col gap-4">
      <div role="group" aria-label="Topics" className="flex flex-wrap gap-2">
        {topics.map((topic) => (
          <Badge
            key={topic}
            size="lg"
            variant={on.includes(topic) ? "default" : "outline"}
            seed={`bf-${topic}`}
            render={<button type="button" aria-pressed={on.includes(topic)} onClick={() => toggle(topic)} className="cursor-pointer" />}
          >
            {topic}
          </Badge>
        ))}
      </div>
      <p className="text-ink-2">{on.length ? `Showing ${on.join(", ").toLowerCase()}.` : "Showing everything."}</p>
    </div>
  );
}
```

## Removable tags

A close button inside the badge, and a way to put them back.

```tsx
"use client";

import { useState } from "react";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import { InkGlyph } from "@/lib/ink-glyphs";

const start = ["ink", "paper", "margins", "coffee"];

export default function BadgeRemovable() {
  const [tags, setTags] = useState(start);
  return (
    <div className="flex w-full max-w-md flex-col items-start gap-4">
      <ul aria-label="Tags" className="flex flex-wrap gap-2">
        {tags.map((tag) => (
          <li key={tag}>
            <Badge variant="secondary" size="lg" seed={`br-${tag}`} className="gap-1.5 pr-1.5">
              {tag}
              <button type="button" aria-label={`Remove ${tag}`} onClick={() => setTags((t) => t.filter((x) => x !== tag))} className="grid size-5 cursor-pointer place-items-center text-ink-2 hover:text-ink">
                <InkGlyph name="close" className="size-3" />
              </button>
            </Badge>
          </li>
        ))}
        {tags.length === 0 && <li className="text-ink-3">No tags left.</li>}
      </ul>
      {tags.length < start.length && (
        <Button variant="ghost" size="sm" seed="br-reset" onClick={() => setTags(start)}>
          Put them back
        </Button>
      )}
    </div>
  );
}
```

## Pen settings

- `draw`: `"auto" | "mount" | "none"` (default `"auto"`). When the strokes draw themselves in: the first time they scroll into view, as soon as they render, or never (already drawn).
- `roughness`: `number` (default `1`). 0 is ruler-neat, 1 a quick confident hand, 2 a scrawl.
- `radius`: `number | "full"` (default `0`). Corner radius in px; "full" draws a pill.
- `fill`: `"shade" | "hatch" | "scribble" | "flat"`. How an area is coloured in. Solid parts default to shade, light ones to hatch.
- `weight`: `number` (default `1`). Line weight multiplier. Also settable in CSS as --ink-weight.
- `speed`: `number` (default `1`). Drawing speed multiplier. Also settable in CSS as --ink-speed.
