# Badge

import { Callout } from "fumadocs-ui/components/callout";
import { Accordion, Accordions } from "fumadocs-ui/components/accordion";
import { Step, Steps } from "fumadocs-ui/components/steps";
import { Tabs, Tab } from "fumadocs-ui/components/tabs";
import { Badge, Card, CardContent, CardHeader, CardTitle } from "@pycolors/ui";
import {
  Accessibility,
  ArrowRight,
  CheckCircle,
  Info,
  MousePointerClick,
  Palette,
  Layers3,
  Tags,
  Type,
} from "lucide-react";
import { PreferAvoid } from "@/components/docs/prefer-avoid";
import { DocsLinks } from "@/components/docs/docs-links";
import { DocsFeatureGrid } from "@/components/docs/docs-feature-grid";

<div className="flex flex-wrap items-center gap-2">
  <Badge variant="outline">
    UI
  </Badge>

  <Badge>
    Badge
  </Badge>
</div>

## Compact metadata for product interfaces

The **Badge** component is a compact, non-interactive primitive used to display short information.

Use it for:

* status
* category
* count
* plan label
* metadata
* product state
* feature availability

Badges are lightweight, semantic, and aligned with PyColors design system tokens.

They are designed for **metadata**, not actions.

<Callout title="Core idea">
  A Badge communicates state or context. It should not replace a Button.
</Callout>

### Import

```tsx title="badge-import.tsx"
import { Badge } from "@pycolors/ui";
```

### Basic usage

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <Badge>
      Default
    </Badge>
  </Tab>

  <Tab value="Code">
    ```tsx title="basic-badge.tsx"
    import { Badge } from "@pycolors/ui";

    export function BasicBadge() {
      return <Badge>Default</Badge>;
    }
    ```
  </Tab>
</Tabs>

Badges inherit their colors from semantic tokens and adapt to light and dark mode.

## When to use Badge

Use Badge when the label helps the user understand context without adding heavy visual hierarchy.

<DocsFeatureGrid
  columns={3}
  tone="platform"
  items={[
  {
    title: "Metadata",
    description:
      "Use for labels such as category, plan, type, role, or version.",
    icon: Tags,
  },
  {
    title: "Status",
    description:
      "Use for small state labels such as active, paid, draft, shipped, or synced.",
    icon: CheckCircle,
    tone: "success",
  },
  {
    title: "Grouping",
    description:
      "Use to classify content without turning the label into a navigation or action element.",
    icon: Layers3,
  },
]}
/>

### Variants

Use variants to convey meaning or status.

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <div className="flex flex-wrap gap-2">
      <Badge>
        Default
      </Badge>

      <Badge variant="secondary">
        Secondary
      </Badge>

      <Badge variant="muted">
        Muted
      </Badge>

      <Badge variant="outline">
        Outline
      </Badge>

      <Badge variant="success">
        Success
      </Badge>

      <Badge variant="warning">
        Warning
      </Badge>

      <Badge variant="destructive">
        Destructive
      </Badge>

      <Badge variant="info">
        Info
      </Badge>
    </div>
  </Tab>

  <Tab value="Code">
    ```tsx title="badge-variants.tsx"
    import { Badge } from "@pycolors/ui";

    export function BadgeVariants() {
      return (
        <div className="flex flex-wrap gap-2">
          <Badge>Default</Badge>
          <Badge variant="secondary">Secondary</Badge>
          <Badge variant="muted">Muted</Badge>
          <Badge variant="outline">Outline</Badge>
          <Badge variant="success">Success</Badge>
          <Badge variant="warning">Warning</Badge>
          <Badge variant="destructive">Destructive</Badge>
          <Badge variant="info">Info</Badge>
        </div>
      );
    }
    ```
  </Tab>
</Tabs>

### Available variants

| Variant       | Purpose                                                   |
| ------------- | --------------------------------------------------------- |
| `default`     | Primary or neutral label                                  |
| `secondary`   | Supporting information                                    |
| `muted`       | Low-emphasis metadata                                     |
| `outline`     | Neutral bordered label                                    |
| `success`     | Positive or completed state                               |
| `warning`     | Caution or attention required                             |
| `destructive` | Error or critical state                                   |
| `info`        | Informational status — semantic neutral, uses card tokens |

<Callout type="info" title="Design rule">
  Prefer `muted` and `outline` for metadata. Reserve semantic state variants for
  actual product states.
</Callout>

### Sizes

Use size to control density and hierarchy.

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <div className="flex items-center gap-2">
      <Badge size="sm">
        Small
      </Badge>

      <Badge size="md">
        Default
      </Badge>

      <Badge size="lg">
        Large
      </Badge>
    </div>
  </Tab>

  <Tab value="Code">
    ```tsx title="badge-sizes.tsx"
    import { Badge } from "@pycolors/ui";

    export function BadgeSizes() {
      return (
        <div className="flex items-center gap-2">
          <Badge size="sm">Small</Badge>
          <Badge size="md">Default</Badge>
          <Badge size="lg">Large</Badge>
        </div>
      );
    }
    ```
  </Tab>
</Tabs>

### Available sizes

| Size | Purpose                               |
| ---- | ------------------------------------- |
| `sm` | Dense metadata, tables, compact lists |
| `md` | Default badge size                    |
| `lg` | Emphasized label or standout metadata |

<Callout title="Density rule">
  Use `sm` in tables and dense product views. Use `md` for most UI surfaces. Use
  `lg` sparingly.
</Callout>

### With left icon

Badges can include a left icon when the icon reinforces status, type, or category.

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <Badge>
      <CheckCircle className="size-3" aria-hidden="true" />

      Active
    </Badge>
  </Tab>

  <Tab value="Code">
    ```tsx title="badge-with-left-icon.tsx"
    import { Badge } from "@pycolors/ui";
    import { CheckCircle } from "lucide-react";

    export function BadgeWithLeftIcon() {
      return (
        <Badge variant="success">
          <CheckCircle className="size-3" aria-hidden="true" />
          Active
        </Badge>
      );
    }
    ```
  </Tab>
</Tabs>

<Callout type="info" title="Icon rule">
  Icons should reinforce meaning. The text must still explain the state.
</Callout>

### With right icon

Use a right icon when the badge points to a related surface, external context, or lightweight next step.

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <Badge variant="outline">
      Docs

      <ArrowRight className="size-3" aria-hidden="true" />
    </Badge>
  </Tab>

  <Tab value="Code">
    ```tsx title="badge-with-right-icon.tsx"
    import { Badge } from "@pycolors/ui";
    import { ArrowRight } from "lucide-react";

    export function BadgeWithRightIcon() {
      return (
        <Badge variant="outline">
          Docs
          <ArrowRight className="size-3" aria-hidden="true" />
        </Badge>
      );
    }
    ```
  </Tab>
</Tabs>

<Callout title="Placement rule">
  Use left icons for state or identity. Use right icons for direction,
  continuation, or linked context.
</Callout>

### As child

Use `asChild` when the badge should render as another semantic element.

<Tabs items={["Preview", "Code"]}>
  <Tab value="Preview">
    <Badge asChild>
      <a href="/docs">
        Docs
      </a>
    </Badge>
  </Tab>

  <Tab value="Code">
    ```tsx title="badge-as-child.tsx"
    import { Badge } from "@pycolors/ui";

    export function BadgeAsChild() {
      return (
        <Badge asChild>
          <a href="/docs">Docs</a>
        </Badge>
      );
    }
    ```
  </Tab>
</Tabs>

<Callout title="Interaction rule">
  Only use interactive badges when the underlying element is semantic, such as
  an anchor or button. If it triggers an important action, use Button instead.
</Callout>

### Usage patterns

<Steps>
  <Step>
    #### Choose the message

    Decide what the badge communicates: status, category, role, version, count, or plan.
  </Step>

  <Step>
    #### Pick the lowest useful emphasis

    Start with `muted` or `outline`. Move to semantic variants only when the state needs clear meaning.
  </Step>

  <Step>
    #### Keep the label short

    Badges work best with short labels: one or two words.
  </Step>

  <Step>
    #### Avoid turning metadata into actions

    A badge should not become a primary interaction. Use Button for actions.
  </Step>
</Steps>

## Badge vs Button vs Alert

| Situation                           | Use    |
| ----------------------------------- | ------ |
| Show a small status                 | Badge  |
| Show a category or plan label       | Badge  |
| Trigger an action                   | Button |
| Show persistent feedback or warning | Alert  |
| Confirm temporary background action | Toast  |

<Callout title="Decision rule">
  Badge communicates context. Button triggers intent. Alert explains important
  feedback.
</Callout>

## API

| Prop        | Type           | Default     | Description                 |
| ----------- | -------------- | ----------- | --------------------------- |
| `variant`   | `BadgeVariant` | `"default"` | Visual style                |
| `size`      | `BadgeSize`    | `"md"`      | Badge size                  |
| `asChild`   | `boolean`      | `false`     | Render via Radix Slot       |
| `className` | `string`       | —           | Additional Tailwind classes |

The Badge extends standard HTML span props:

```ts title="badge-props.ts"
React.HTMLAttributes<HTMLSpanElement>;
```

### TypeScript types

```ts title="badge-types.ts"
export type BadgeVariant =
  | "default"
  | "secondary"
  | "muted"
  | "outline"
  | "success"
  | "warning"
  | "destructive";

export type BadgeSize = "sm" | "md" | "lg";
```

### Accessibility

* Badges are non-interactive by default.
* Do not use a badge as a button.
* Do not rely on color alone to convey meaning.
* Icons must be supplementary, not the only indicator.
* Use `aria-hidden="true"` for decorative icons.
* When interactive, use `asChild` with a semantic element such as `a` or `button`.
* `success` and `warning` use dedicated `--success-foreground` / `--warning-foreground` design tokens (see `@pycolors/tokens`) tuned to meet WCAG AA contrast against their respective backgrounds — do not substitute a different text color on these two variants.
* `info` uses existing semantic card tokens; contrast is theme-dependent and must satisfy the same accessibility requirements as other Badge variants.

## Prefer / avoid

<PreferAvoid
  prefer={[
  "short labels",
  "semantic variants for real status",
  "`muted` or `outline` for metadata",
  "icons as supporting context only",
  "consistent badge usage across tables and cards",
]}
  avoid={[
  "using badges as primary actions",
  "long sentence-like badge text",
  "too many badges in one panel",
  "color-only meaning",
  "inventing one-off badge colors locally",
]}
/>

## Common questions

<Accordions type="single">
  <Accordion title="Can a badge be clickable?">
    Yes, but only when rendered with a semantic element through `asChild`. If it performs a primary action, use Button instead.
  </Accordion>

  <Accordion title="Should every status use a colored badge?">
    No. Use semantic color only when the state needs emphasis. For low-priority
    metadata, prefer `muted` or `outline`.
  </Accordion>

  <Accordion title="Can badges include icons?">
    Yes. Icons can reinforce meaning, but the text must still communicate the
    state.
  </Accordion>

  <Accordion title="Should icons appear on the left or the right?">
    Use left icons for state, identity, or category. Use right icons for
    direction, continuation, or linked context.
  </Accordion>

  <Accordion title="How many badges should a card have?">
    Use as few as possible. Badges should clarify the interface, not decorate it.
  </Accordion>
</Accordions>

## Related

<DocsLinks
  variant="list"
  tone="platform"
  items={[
  {
    title: "Colors",
    href: "/docs/design-system/colors",
    description: "Semantic color roles used by badges.",
    icon: Palette,
  },
  {
    title: "Typography",
    href: "/docs/design-system/typography",
    description: "Text scale and hierarchy for compact UI labels.",
    icon: Type,
  },
  {
    title: "Button",
    href: "/docs/ui/button",
    description: "Use buttons for actions, not badges.",
    icon: MousePointerClick,
  },
  {
    title: "Accessibility",
    href: "/docs/ui/accessibility",
    description: "Build clear, accessible labels and status indicators.",
    icon: Accessibility,
  },
]}
/>