---
title: "Badge"
description: "Badge with accessible presentation and local state."
---

# Badge

Badge with accessible presentation and local state.

## Installation

Install the shared package once in your Exact app. This version is a beta.

```sh
bun add --exact @rightui/ui@0.4.0-beta.4
```

Or choose another package manager:

```sh
npm install --save-exact @rightui/ui@0.4.0-beta.4
pnpm add --save-exact @rightui/ui@0.4.0-beta.4
yarn add --exact @rightui/ui@0.4.0-beta.4
```

Exact compilation uses Bun 1.4.2. Register Geist and Newsreader, declare the font marker and add the root provider using [Installation](/docs/installation).

### Manual source setup

Install an editable copy of the complete package:

```sh
mkdir -p vendor/rightui vendor/rightui-ui
npm pack @rightui/ui@0.4.0-beta.4 --pack-destination vendor/rightui
tar -xzf ./vendor/rightui/rightui-ui-0.4.0-beta.4.tgz -C vendor/rightui-ui --strip-components=1
bun add file:./vendor/rightui-ui
```

Keep package.json, the Contract files, ui.tamber, assets and licenses together.

### Import

```contract
use Badge, BadgeWithOptions, BadgeOptions, BadgeContent, BadgeIcon, BadgeSpinner from "@rightui/ui"
```

## Usage

Import Badge(label, variant) for the legacy wrapper, or BadgeWithOptions(label, variant, options) with BadgeOptions(icon, trailingIcon, href, fill, ink, direction, invalid). Supply every options field explicitly; empty strings select no icon/link or default colors. Use BadgeContent for slotted content, BadgeIcon for named SVG icons and BadgeSpinner(reduced, label) for status. BadgeContent requires variant, href, fill, ink, direction, invalid and iconPosition. Variants are default, secondary, destructive, outline, ghost and link; primary and success remain accepted.

## Example

```contract
use Badge, BadgeWithOptions, BadgeOptions, BadgeContent, BadgeIcon, BadgeSpinner, Select, uiFontFamily from "@rightui/ui"

component Demo
  props
    width: number
    reduced: bool
  inject
    uiFont: string
  derive narrow = width < 600
  state language = "Arabic"
  derive rtl = language != "English"
  derive hebrew = language == "Hebrew"
  action chooseLanguage(value: string)
    language = value
  view
    column gap="ui.space-16" min-width=0 max-width="100%"
      row gap="ui.space-8" flex-wrap="wrap" align-items="center"
        Badge(label="Badge", variant="default")
        Badge(label="Secondary", variant="secondary")
        Badge(label="Destructive", variant="destructive")
        Badge(label="Outline", variant="outline")
        Badge(label="Ghost", variant="ghost")
        BadgeWithOptions(label="Link", variant="link", options=BadgeOptions(icon="", trailingIcon=false, href="/docs/components/badge", fill="", ink="", direction="ltr", invalid=false))
      row gap="ui.space-8" flex-wrap="wrap" align-items="center"
        BadgeWithOptions(label="Verified", variant="secondary", options=BadgeOptions(icon="verified", trailingIcon=false, href="", fill="", ink="", direction="ltr", invalid=false))
        BadgeWithOptions(label="Bookmark", variant="outline", options=BadgeOptions(icon="bookmark", trailingIcon=true, href="", fill="", ink="", direction="ltr", invalid=false))
      row gap="ui.space-8" flex-wrap="wrap" align-items="center"
        BadgeContent(variant="destructive", iconPosition="start", href="", fill="", ink="", direction="ltr", invalid=false)
          BadgeSpinner(reduced=reduced, label="Deleting")
          text "Deleting" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        BadgeContent(variant="secondary", iconPosition="end", href="", fill="", ink="", direction="ltr", invalid=false)
          text "Generating" font-family=uiFontFamily(uiFont) data-rightui-font="body"
          BadgeSpinner(reduced=reduced, label="Generating")
      BadgeContent(href="/docs/components/badge", iconPosition="end", variant="default", fill="", ink="", direction="ltr", invalid=false)
        text "Open Link" font-family=uiFontFamily(uiFont) data-rightui-font="body"
        BadgeIcon(name="arrow-up-right")
      row gap="ui.space-8" flex-wrap="wrap" align-items="center"
        BadgeWithOptions(label="Blue", variant="default", options=BadgeOptions(icon="", trailingIcon=false, href="", fill="color-mix(in srgb, ui.chart-1 12%, ui.background)", ink="ui.chart-1", direction="ltr", invalid=false))
        BadgeWithOptions(label="Green", variant="default", options=BadgeOptions(icon="", trailingIcon=false, href="", fill="color-mix(in srgb, ui.success 12%, ui.background)", ink="ui.success", direction="ltr", invalid=false))
        BadgeWithOptions(label="Sky", variant="default", options=BadgeOptions(icon="", trailingIcon=false, href="", fill="color-mix(in srgb, ui.chart-2 12%, ui.background)", ink="ui.chart-2", direction="ltr", invalid=false))
        BadgeWithOptions(label="Purple", variant="default", options=BadgeOptions(icon="", trailingIcon=false, href="", fill="color-mix(in srgb, ui.chart-3 12%, ui.background)", ink="ui.chart-3", direction="ltr", invalid=false))
        BadgeWithOptions(label="Red", variant="default", options=BadgeOptions(icon="", trailingIcon=false, href="", fill="color-mix(in srgb, ui.danger 12%, ui.background)", ink="ui.danger", direction="ltr", invalid=false))
      Select(id="badge-language", label="Language", options=["English", "Arabic", "Hebrew"], value=language, disabled=false, reduced=reduced, change=chooseLanguage)
      row testId="badge-rtl-examples" direction=(rtl ? "rtl" : "ltr") gap="ui.space-8" flex-wrap="wrap" align-items="center"
        BadgeWithOptions(label=rtl ? (hebrew ? "תג" : "شارة") : "Badge", variant="default", options=BadgeOptions(icon="", trailingIcon=false, href="", fill="", ink="", direction=rtl ? "rtl" : "ltr", invalid=false))
        BadgeWithOptions(label=rtl ? (hebrew ? "משני" : "ثانوي") : "Secondary", variant="secondary", options=BadgeOptions(icon="", trailingIcon=false, href="", fill="", ink="", direction=rtl ? "rtl" : "ltr", invalid=false))
        BadgeWithOptions(label=rtl ? (hebrew ? "הרסני" : "مدمر") : "Destructive", variant="destructive", options=BadgeOptions(icon="", trailingIcon=false, href="", fill="", ink="", direction=rtl ? "rtl" : "ltr", invalid=false))
        BadgeWithOptions(label=rtl ? (hebrew ? "קווי מתאר" : "مخطط") : "Outline", variant="outline", options=BadgeOptions(icon="", trailingIcon=false, href="", fill="", ink="", direction=rtl ? "rtl" : "ltr", invalid=false))
        BadgeWithOptions(label=rtl ? (hebrew ? "מאומת" : "متحقق") : "Verified", variant="secondary", options=BadgeOptions(icon="verified", trailingIcon=false, href="", fill="", ink="", direction=rtl ? "rtl" : "ltr", invalid=false))
        BadgeWithOptions(label=rtl ? (hebrew ? "סימנייה" : "إشارة مرجعية") : "Bookmark", variant="outline", options=BadgeOptions(icon="bookmark", trailingIcon=true, href="", fill="", ink="", direction=rtl ? "rtl" : "ltr", invalid=false))

```

## API Reference

| Property | Type |
| --- | --- |
| BadgeContent.variant | string |
| BadgeContent.href | string |
| BadgeContent.fill | string |
| BadgeContent.ink | string |
| BadgeContent.direction | string |
| BadgeContent.invalid | bool |
| BadgeContent.iconPosition | string |
| BadgeIcon.name | string |
| BadgeSpinner.reduced | bool |
| BadgeSpinner.label | string |
| BadgeWithOptions.label | string |
| BadgeWithOptions.variant | string |
| BadgeWithOptions.options | BadgeOptions |
| Badge.label | string |
| Badge.variant | string |

## Source

File: packages/ui/badge.contract

```contract
use FontWeights from "./typography-context.contract"
use uiFontFamily from "./typography-context.contract"
use uiSpin from "./spinner.contract"

fn badgeFill(variant: string, hovered: bool): string = variant == "primary" or variant == "default" ? (hovered ? "ui.primary-hover" : "ui.primary") : variant == "destructive" ? (hovered ? "color-mix(in srgb, ui.destructive 20%, transparent)" : "light-dark(color-mix(in srgb, ui.destructive 10%, transparent), color-mix(in srgb, ui.destructive 20%, transparent))") : variant == "outline" or variant == "ghost" ? (hovered ? "ui.muted" : "transparent") : variant == "link" ? "transparent" : (hovered ? "ui.secondary-hover" : "ui.muted")
fn badgeInk(variant: string, hovered: bool): string = variant == "primary" or variant == "default" ? "ui.primary-foreground" : variant == "success" ? "ui.success" : variant == "destructive" ? "ui.destructive" : variant == "link" ? "ui.primary" : hovered and (variant == "outline" or variant == "ghost") ? "ui.muted-foreground" : "ui.foreground"

style BadgeSurface
  display="flex"
  width="auto"
  align-self="flex-start"
  flex-shrink=0
  align-items="center"
  justify-content="center"
  box-sizing="border-box"
  gap="ui.space-4"
  height="ui.space-20"
  padding="ui.space-2 ui.space-8"
  border-radius="ui.radius-4xl"
  border-width="ui.border-width"
  border-style="solid"
  white-space="nowrap"
  font-size="ui.text-xs"
  line-height="ui.leading-heading"
  font-weight="ui.weight-medium"
  text-decoration="none"
  outline="none"
  :focus-visible
    border-color="ui.ring"
    box-shadow="ui.shadow-focus"

// Slot composition keeps SVGs, spinners and custom content in one public surface.
component BadgeContent
  props
    variant: string
    href: string
    fill: string
    ink: string
    direction: string
    invalid: bool
    iconPosition: string
  inject
    uiWeights: FontWeights
    uiFont: string
  slot
  state hovered = false
  action hoverChanged(inside: bool)
    hovered = inside
  view
    if href != ""
      link href=href class=BadgeSurface font-weight=uiWeights.medium hover=hoverChanged direction=direction aria-invalid=invalid
        font-family=uiFontFamily(uiFont) data-rightui-font="body"
        padding-left=(iconPosition == "start" ? "ui.space-6" : "ui.space-8") padding-right=(iconPosition == "end" ? "ui.space-6" : "ui.space-8")
        background-color=(fill != "" ? fill : badgeFill(variant, hovered)) color=(ink != "" ? ink : badgeInk(variant, hovered))
        border-color=(invalid ? "ui.destructive" : variant == "outline" ? "ui.border" : "transparent")
        text-decoration=(variant == "link" and hovered ? "underline" : "none")
        children
    else
      row class=BadgeSurface font-weight=uiWeights.medium hover=hoverChanged direction=direction aria-invalid=invalid
        font-family=uiFontFamily(uiFont) data-rightui-font="body"
        padding-left=(iconPosition == "start" ? "ui.space-6" : "ui.space-8") padding-right=(iconPosition == "end" ? "ui.space-6" : "ui.space-8")
        background-color=(fill != "" ? fill : badgeFill(variant, variant == "ghost" and hovered)) color=(ink != "" ? ink : badgeInk(variant, variant == "ghost" and hovered))
        border-color=(invalid ? "ui.destructive" : variant == "outline" ? "ui.border" : "transparent")
        text-decoration=(variant == "link" and hovered ? "underline" : "none")
        children

component BadgeIcon
  props
    name: string
  view
    svg width="ui.space-12" height="ui.space-12" flex-shrink=0 viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.stroke-standard" stroke-linecap="round" stroke-linejoin="round" aria-hidden=true
      if name == "verified" or name == "check" or name == "✓"
        path d="M9 3 12 2 15 3 18 3 20 6 21 9 22 12 21 15 20 18 18 21 15 21 12 22 9 21 6 21 4 18 3 15 2 12 3 9 4 6 6 3Z"
        path d="m8 12 3 3 5-6"
      else if name == "bookmark"
        path d="M6 3h12v18l-6-4-6 4Z"
      else if name == "arrow-up-right" or name == "↗"
        path d="M7 17 17 7M7 7h10v10"
      else if name == "plus" or name == "+"
        path d="M12 5v14M5 12h14"

component BadgeSpinner
  props
    reduced: bool
    label: string
  view
    row role="status" aria-label=label width="ui.space-12" height="ui.space-12" flex-shrink=0
      svg width="ui.space-12" height="ui.space-12" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="ui.stroke-standard" stroke-linecap="round" aria-hidden=true
        animation=(reduced ? "none" : "uiSpin ui.duration-spin ui.easing-linear infinite")
        circle cx=12 cy=12 r=9 opacity="ui.opacity-faint"
        path d="M12 3a9 9 0 0 1 9 9"

shape BadgeOptions
  icon: string
  trailingIcon: bool
  href: string
  fill: string
  ink: string
  direction: string
  invalid: bool

component BadgeWithOptions
  props
    label: string
    variant: string
    options: BadgeOptions
  inject
    uiWeights: FontWeights
    uiFont: string
  view
    BadgeContent(variant=variant, href=options.href, fill=options.fill, ink=options.ink, direction=options.direction, invalid=options.invalid, iconPosition=options.icon == "" ? "" : options.trailingIcon ? "end" : "start")
      if options.icon != "" and not options.trailingIcon
        BadgeIcon(name=options.icon)
      text label font-size="ui.text-xs" line-height="ui.leading-heading" font-weight=uiWeights.medium font-family=uiFontFamily(uiFont) data-rightui-font="body"
      if options.icon != "" and options.trailingIcon
        BadgeIcon(name=options.icon)

// Preserve the original two-prop API on SDKs without prop defaults.
component Badge
  props
    label: string
    variant: string
  view
    BadgeWithOptions(label=label, variant=variant, options=BadgeOptions(icon="", trailingIcon=false, href="", fill="", ink="", direction="inherit", invalid=false))

```
