# Badge

> A small label for a status, a count or a category; for the state of an order, unread messages or a tag.

Source: https://shadleaf.dev/1.2.0/components/badge/

A small, rounded label next to other content, in the same variants as the button.

**Example: Badge.**

```html
<sl:badge>New</sl:badge>
```

## When to use

- The state of something in a table row, such as Paid, Pending or Failed.
- A count, such as unread messages.
- A tag or a category on an item or a card.

Use something else for:

- A count next to a link in the sidebar: the sidebar's [menu badge](https://shadleaf.dev/1.2.0/components/sidebar.md#badges).
- A key on the keyboard: a [kbd](https://shadleaf.dev/1.2.0/components/kbd.md).
- A message the user should read: an [alert](https://shadleaf.dev/1.2.0/components/alert.md).

## Variants

The same variants as the [button](https://shadleaf.dev/1.2.0/components/button.md).

**Example: Variants.**

```html
<sl:badge>Default</sl:badge>
<sl:badge variant="secondary">Secondary</sl:badge>
<sl:badge variant="destructive">Destructive</sl:badge>
<sl:badge variant="outline">Outline</sl:badge>
<sl:badge variant="ghost">Ghost</sl:badge>
<sl:badge variant="link">Link</sl:badge>
```

## Status variants

`success`, `info` and `warning` are Shadleaf's own, for a state such as Paid, In progress or Due soon. They stand next
to `destructive` and are tinted the same way, in the status colours: `--success`, `--info` and `--warning` for the
tint, `--success-text`, `--info-text` and `--warning-text` for the text (the same tokens as a
[toast's rich colours](https://shadleaf.dev/1.2.0/components/toast.md#rich-colours)). The text keeps 4.5:1 or more on the tint, in light and dark mode.

**Example: Status variants.** Shadleaf's own, in the status colours.

```html
<sl:badge variant="success">Paid</sl:badge>
<sl:badge variant="info">In progress</sl:badge>
<sl:badge variant="warning">Due soon</sl:badge>
<sl:badge variant="destructive">Overdue</sl:badge>
<sl:badge as="a" href="/orders?status=paid" variant="success">
  <sl:slot name="icon-start"><sl:icon name="circle-check"/></sl:slot>
  Paid
</sl:badge>
```

## With an icon

Put an [`<sl:icon>`](https://shadleaf.dev/1.2.0/components/icon.md) in the `icon-start` or `icon-end` slot. It takes the badge's colour and a 12 pixel size.

**Example: With an icon.**

```html
<sl:badge variant="secondary">
  <sl:slot name="icon-start"><sl:icon name="badge-check"/></sl:slot>
  Verified
</sl:badge>
<sl:badge variant="outline">
  Details
  <sl:slot name="icon-end"><sl:icon name="arrow-right"/></sl:slot>
</sl:badge>
```

## As a link

`as="a"` renders an anchor, for a badge that filters a list or opens a detail page. Only a linked badge reacts to hover
and shows a focus ring; a plain badge is not interactive, so it does not pretend to be.

**Example: As a link.** Only a linked badge changes on hover and takes keyboard focus.

```html
<sl:badge as="a" href="/orders?status=open">Open</sl:badge>
<sl:badge as="a" href="/orders?status=late" variant="destructive">Late</sl:badge>
<sl:badge as="a" href="/orders" variant="outline">
  All orders
  <sl:slot name="icon-end"><sl:icon name="arrow-up-right"/></sl:slot>
</sl:badge>
```

A badge is text, so a screen reader reads it where it stands. When a bare number or a single word would not make sense
out of context, write the context into the text, or next to the badge.

## Props from expressions

**Example: Props from an expression.**

```html
<sl:badge th:variant="${order.late ? 'destructive' : 'secondary'}"
          th:text="${order.status}">Open</sl:badge>
```

## Everything else passes through

**Example: Pass-through attributes.** Anything that is not a prop lands on the rendered element; class is appended.

```html
<sl:badge variant="outline" class="app-wide" id="order-status" data-testid="status">Shipped</sl:badge>
```

## Attributes

Attributes of `<sl:badge>`: A small label for a status, a count or a category; with `as="a"`, a link that looks the same. Every attribute that is not a prop passes through; `class` is appended to `badge`.

| Attribute | Values | Default | Description |
| --- | --- | --- | --- |
| `variant` | `default`, `secondary`, `destructive`, `success`, `info`, `warning`, `outline`, `ghost`, `link` | `default` | Visual style of the badge. `success`, `info` and `warning` are Shadleaf's own, in the status colours. |
| `as` | `span`, `a` | `span` | Element to render: a `span`, or an anchor (`a`) for a badge that links somewhere. Only a linked badge reacts to hover. |

Every prop can also be set from an expression: `th:variant="${...}"`. Any other attribute passes through to the rendered element, and `class` is appended.

## Styling hooks

The classes and attributes your own CSS can select to restyle the component, as explained in
[your own CSS rules](https://shadleaf.dev/1.2.0/guides/adjustability.md#5-your-own-css-rules).

Styling hooks of `<sl:badge>`.

**Parts**

| Class | Element |
| --- | --- |
| `.badge` | `<a>`, `<span>` |
| `.badge-icon` | `<span>` |

**Props**

| Prop | Selector | Values |
| --- | --- | --- |
| `variant` | `.badge[data-variant="…"]`, `.badge:not([data-variant])` | `secondary`, `destructive`, `success`, `info`, `warning`, `outline`, `ghost`, `link`; `default` (default) |

**States**

| Selector | Values |
| --- | --- |
| `.badge[aria-invalid="…"]` | `true` |
| `.badge-icon[data-icon="…"]` | `inline-end`, `inline-start` |
