# Badge

> Shows a status or a short label.

A badge shows the status of an item, such as a release or a document, or a short label such as "Baru".

## When to use

- Use the status variants for the state of a record: `success`, `warning`, `info`, `destructive`.
- Always put the status in words; color alone never carries a status.
- Use `default` or `outline` for labels that are not a status.
- Don't use a badge as a button.

_Live preview of the React and Vue versions: https://design.sosialoka.tech/components/badge/_

### Do and don't

- **Do:** Name the status in words, with its color as a second signal.
- **Don't:** Don't show a status by color alone; not everyone can tell the colors apart.

## Anatomy

1. Status color text
2. Soft background
3. Outline variant

Short text, `text-xs` medium, on `radius-sm` (6px). Badges are never pills. Status badges use the soft fill of their status with the status color as text.

## Variants

| Variant | Looks | Use it for |
| --- | --- | --- |
| `success` | `success` text on `success-soft` | Done, approved, published. |
| `warning` | `warning` text on `warning-soft` | Pending, waiting for review. |
| `info` | `info` text on `info-soft` | Neutral information. |
| `destructive` | `destructive` text on `destructive-soft` | Rejected, failed, cancelled. |
| `default` | `primary` fill | A label in the product color, such as "Baru". |
| `secondary` | `secondary` fill | A quiet label. |
| `outline` | `border` outline | A label that should stay in the background, such as "Draf". |
| `ghost` | No fill until hover | A quiet label inside a link or a menu. |
| `link` | `primary-text`, underlined on hover | A badge that is itself a link. |

The `default` badge on Cover Clearance orange uses the same contrast exception as the primary button. Keep its label short.

## Props

| React | Vue | Flutter | Type | Default | Use |
| --- | --- | --- | --- | --- | --- |
| `variant` | `variant` | named constructor, or `backgroundColor` + `foregroundColor` for status | see Variants | `default` | Meaning and weight. |
| `render` | `as` / `as-child` | `child` | element | `span` | Renders the badge as another element, such as a link. |

## Code

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

export default function BadgeDemo() {
  return (
    <div className="flex flex-wrap items-center gap-2">
      <Badge variant="success">Selesai</Badge>
      <Badge variant="warning">Menunggu</Badge>
      <Badge variant="info">Info</Badge>
      <Badge variant="destructive">Ditolak</Badge>
      <Badge>Baru</Badge>
      <Badge variant="outline">Draf</Badge>
    </div>
  )
}
```

**Vue**
```vue
<script setup lang="ts">
import { Badge } from "@/components/ui/badge"
</script>

<template>
  <div class="flex flex-wrap items-center gap-2">
    <Badge variant="success">Selesai</Badge>
    <Badge variant="warning">Menunggu</Badge>
    <Badge variant="info">Info</Badge>
    <Badge variant="destructive">Ditolak</Badge>
    <Badge>Baru</Badge>
    <Badge variant="outline">Draf</Badge>
  </div>
</template>
```

**Flutter**
```dart
// Status colors come from the Sosialoka theme: context.sosialoka.
import 'package:flutter/widgets.dart';
import 'package:shadcn_ui/shadcn_ui.dart';
import 'package:sosialoka_ui/sosialoka_ui.dart';

class BadgeDemo extends StatelessWidget {
  const BadgeDemo({super.key});

  ShadBadge status(Color text, Color soft, String label) =>
      ShadBadge(backgroundColor: soft, foregroundColor: text, child: Text(label));

  @override
  Widget build(BuildContext context) {
    final c = context.sosialoka;
    return Wrap(
      spacing: 8,
      runSpacing: 8,
      children: [
        status(c.success, c.successSoft, 'Selesai'),
        status(c.warning, c.warningSoft, 'Menunggu'),
        status(c.info, c.infoSoft, 'Info'),
        status(c.destructive, c.destructiveSoft, 'Ditolak'),
        const ShadBadge(child: Text('Baru')),
        const ShadBadge.outline(child: Text('Draf')),
      ],
    );
  }
}
```

## Install

**React**
```bash
npx shadcn@latest add https://design.sosialoka.tech/r/react/badge.json
```

**Vue**
```bash
npx shadcn-vue@latest add https://design.sosialoka.tech/r/vue/badge.json
```

## Accessibility

- Badge text is read as normal text; keep it meaningful on its own.
- Status colors pass 4.5:1 on their soft fill in every theme.
