# Badge
A short label for a status, a category or a count.
Source page: https://dinachi.dev/docs/components/badge

## Installation
```bash
npx @dinachi/cli@latest add badge
```
Adds to your project:
- `components/ui/badge/badge.tsx`
- `components/ui/badge/index.ts`
Installs:
- `class-variance-authority` ^0.7.1

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

export function DeployStatus() {
  return <Badge variant="success">Live</Badge>
}
```

## Examples

### Variants
Each colour scheme, from default to info.
```tsx
import { Badge } from '@/components/ui/badge';

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

### Default
A badge with the default style.
```tsx
import { Badge } from '@/components/ui/badge';

export function Example() {
  return <Badge>Badge</Badge>;
}
```

### Sizes
Small, default and large.
```tsx
import { Badge } from '@/components/ui/badge';

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

## Notes

### Removing a badge
`dismissible` only draws the button. Remove the badge from state in `onDismiss`:

```tsx
{tags.map((tag) => (
  <Badge key={tag} dismissible onDismiss={() => remove(tag)}>
    {tag}
  </Badge>
))}
```

### Styling other elements
`badgeVariants()` takes the same `variant`, `size` and `rounded` options, to style a link or button as a badge.

## API reference

### Badge
A styled div with optional icon and remove button.

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `variant` | `"default" \| "secondary" \| "destructive" \| "outline" \| "success" \| "warning" \| "info"` | `"default"` | The colour scheme. |
| `size` | `"sm" \| "default" \| "lg"` | `"default"` | Padding and text size. |
| `rounded` | `"default" \| "sm" \| "md" \| "lg" \| "none"` | `"default"` | Corner radius. `default` is fully rounded. |
| `icon` | `ReactNode` | - | Shown before the text. |
| `dismissible` | `boolean` | `false` | Adds a remove button after the text. |
| `onDismiss` | `() => void` | - | Runs on remove click. The badge does not hide itself. |

## Accessibility
- A plain div with no role; its text reads with the surrounding content.
- Put the status in the text; colour alone does not convey it.
- The remove button is a native button named "Remove" on every badge; let context identify which.
### Keyboard
| Keys | Does |
| --- | --- |
| `Enter` / `Space` | On the remove button, runs `onDismiss` |

## Source
`components/ui/badge/badge.tsx`
```tsx
"use client"

import * as React from "react"
import { cva, type VariantProps } from "class-variance-authority"
import { cn } from "@/lib/utils"

const badgeVariants = cva(
  "inline-flex items-center rounded-full border px-2.5 py-0.5 text-xs font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-ring focus:ring-offset-2",
  {
    variants: {
      variant: {
        default:
          "border-transparent bg-primary text-primary-foreground",
        secondary:
          "border-transparent bg-secondary text-secondary-foreground",
        destructive:
          "border-transparent bg-destructive text-destructive-foreground",
        outline: "text-foreground",
        success:
          "border-transparent bg-success text-success-foreground",
        warning:
          "border-transparent bg-warning text-warning-foreground",
        info:
          "border-transparent bg-info text-info-foreground",
      },
      size: {
        sm: "px-2 py-0.5 text-xs",
        default: "px-2.5 py-0.5 text-xs",
        lg: "px-3 py-1 text-sm",
      },
      rounded: {
        default: "rounded-full",
        sm: "rounded-sm",
        md: "rounded-md",
        lg: "rounded-lg",
        none: "rounded-none",
      },
    },
    defaultVariants: {
      variant: "default",
      size: "default",
      rounded: "default",
    },
  }
)

export interface BadgeProps
  extends React.HTMLAttributes<HTMLDivElement>,
    VariantProps<typeof badgeVariants> {
  icon?: React.ReactNode
  dismissible?: boolean
  onDismiss?: () => void
}

const Badge = React.forwardRef<HTMLDivElement, BadgeProps>(
  (
    {
      className,
      variant,
      size,
      rounded,
      children,
      icon,
      dismissible = false,
      onDismiss,
      ...props
    },
    ref
  ) => (
    <div
      data-slot="badge"
      ref={ref}
      className={cn(badgeVariants({ variant, size, rounded }), className)}
      {...props}
    >
      {icon && <span className="mr-1 shrink-0">{icon}</span>}
      {children}
      {dismissible && (
        <button
          type="button"
          onClick={onDismiss}
          className="ml-1 shrink-0 rounded-full p-0.5 hover:bg-foreground/10 focus:outline-none focus:ring-1 focus:ring-ring/40"
          aria-label="Remove"
        >
          <svg
            className="h-3 w-3"
            fill="none"
            stroke="currentColor"
            viewBox="0 0 24 24"
            aria-hidden="true"
          >
            <path
              strokeLinecap="round"
              strokeLinejoin="round"
              strokeWidth={2}
              d="M6 18L18 6M6 6l12 12"
            />
          </svg>
        </button>
      )}
    </div>
  )
)

Badge.displayName = "Badge"

export { Badge, badgeVariants }
```
