Badge

A lightweight labeling component for highlighting status, metadata, or categorization in your application.

Key Features

  • Multiple Variants, Five distinct visual styles: solid, outline, ghost, soft, and link
  • Semantic Colors, Full color palette including primary, success, danger, warning, and info
  • Flexible Sizing, Seven size options from xs to xl2 for any use case
  • Shape Options, Choose from rounded, pill, square, or circle shapes
  • Icon Support, Leading and trailing icons with automatic color inheritance
  • Interactive Features, Dismissible badges with close buttons
  • Notification Counters, Display numeric counters for notifications and counts
  • Pulsing Animation, Attention-grabbing animation for important badges
  • Clickable Badges, Transform badges into links with URL support
  • GitHub Style, Special GitHub-themed badges for topics and labels
  • Accessibility - Full ARIA support with keyboard navigation
  • JavaScript API - Programmatic control with data-action attributes

Basic Usage

The badge component supports both positional and keyword argument styles. By default, badges are small pills with a primary color.

<%= rui_badge("New") %>
<%= rui_badge("Success", color: :success) %>
<%= rui_badge(text: "Featured", color: :info) %>
New Success Featured

Colors

All semantic and Tailwind colors are supported. The default is primary.

Primary Secondary Success Danger Warning Info Error Blue Red Green Yellow Purple
<%= rui_badge(text: "Primary", color: :primary) %>
<%= rui_badge(text: "Success", color: :success) %>
<%= rui_badge(text: "Danger", color: :danger) %>

Sizes

Seven size options from xs to xl2. The default is xs.

XS SM Base MD LG XL XL2
<%= rui_badge(text: "XS", size: :xs) %>
<%= rui_badge(text: "SM", size: :sm) %>
<%= rui_badge(text: "Base", size: :base) %>
<%= rui_badge(text: "LG", size: :lg) %>

Shapes

Four border radius options. The default is pill (fully rounded).

Rounded
<%= rui_badge(text: "Rounded", shape: :rounded) %>
Pill Default
<%= rui_badge(text: "Pill", shape: :pill) %>
Square
<%= rui_badge(text: "Square", shape: :square) %>
5
<%= rui_badge(text: "5", shape: :circle) %>

Variants

Five distinct visual styles. The default is solid.

Solid Default
<%= rui_badge(text: "Solid", variant: :solid) %>
Outline
<%= rui_badge(text: "Outline", variant: :outline) %>
Ghost
<%= rui_badge(text: "Ghost", variant: :ghost) %>
Soft
<%= rui_badge(text: "Soft", variant: :soft) %>
Link
<%= rui_badge(text: "Link", variant: :link) %>

With Icons

Badges support leading and trailing icons. Icons automatically inherit the badge color.

Star Check Warning Error Info Next

<%= rui_badge(text: "Star", color: :primary) do |badge| %>
  <% badge.with_icon(:star) %>
<% end %>

<%= rui_badge(text: "Next", color: :primary) do |badge| %>
  <% badge.with_icon(:arrow_right, position: :trailing) %>
<% end %>

<%= rui_badge(text: "Custom", color: :blue) do |badge| %>
  <% badge.with_icon(:star, color: :yellow) %>
<% end %>

Interactive Badges

Badges can be dismissible or include close buttons for interactive functionality.

Note

Interactive badges require the Utilities controller from RapidRailsUI. These examples demonstrate the markup and styling.

Dismissible Badges

Click anywhere on the badge to dismiss it.

Click to Dismiss Dismissible
<%= rui_badge(text: "Click to Dismiss", dismissible: true) %>

Close Button

Shows a close icon button for dismissing the badge.

Close Me With Close
<%= rui_badge(text: "Close Me", close_button: true) %>

Counters

Display numeric counters for notifications, counts, or quantities.

Messages 5 Notifications 12 Inbox 99 3
<%= rui_badge(text: "Messages", counter: 5) do |badge| %>
  <% badge.with_icon(:mail) %>
<% end %>

<%= rui_badge(counter: 3, color: :success) %>

Pulsing Animation

Add a pulsing animation to draw attention to important badges.

Live New Alert
<%= rui_badge(text: "Live", pulsing: true, color: :danger) do |badge| %>
  <% badge.with_icon(:radio) %>
<% end %>

Clickable Badges

Add a URL to make badges clickable links. The badge automatically renders as an <a> tag.

<%= rui_badge(text: "View Profile", url: "/profile", color: :primary) do |badge| %>
  <% badge.with_icon(:user) %>
<% end %>

GitHub Style Badges

Special GitHub-themed badges using gh_ prefix colors.

ruby javascript rails bug enhancement documentation

<%= rui_badge(text: "ruby", color: :gh_topic) %>
<%= rui_badge(text: "rails", color: :gh_topic) %>

<%= rui_badge(text: "bug", color: :gh_label) %>
<%= rui_badge(text: "enhancement", color: :gh_label) %>

Real-World Examples

See how badges work in common UI patterns and real-world scenarios.

Status Indicators

Display status with color-coded badges.

Order #1234: Pending
Order #1235: Shipped
Order #1236: Delivered
Order #1237: Cancelled
<%= rui_badge(text: "Pending", color: :warning, variant: :soft) do |badge| %>
  <% badge.with_icon(:clock) %>
<% end %>

<%= rui_badge(text: "Delivered", color: :success, variant: :soft) do |badge| %>
  <% badge.with_icon(:check_circle) %>
<% end %>

Product Tags

Tag products with category badges.

Premium Laptop

New Arrival Featured Sale -20%

High-performance laptop with the latest specifications.

$1,599 $1,999
<div class="product-card">
  <h4>Premium Laptop</h4>
  <div class="flex flex-wrap gap-2">
    <%= rui_badge(text: "New Arrival", color: :success, size: :sm) %>
    <%= rui_badge(text: "Featured", color: :primary, size: :sm) %>
    <%= rui_badge(text: "Sale -20%", color: :danger, size: :sm, pulsing: true) %>
  </div>
</div>

User Profiles

Display user roles and badges.

John Doe
Admin Verified

Senior Developer

<div class="flex items-center gap-2">
  <h5>John Doe</h5>
  <%= rui_badge(text: "Admin", color: :purple, size: :xs) %>
  <%= rui_badge(text: "Verified", color: :blue, size: :xs) do |badge| %>
    <% badge.with_icon(:badge_check) %>
  <% end %>
</div>

Notification System

Show notification counts with counters.

<div class="flex items-center gap-2">
  <span>Messages</span>
  <%= rui_badge(counter: 5, color: :danger, shape: :circle, size: :xs) %>
</div>

Filter Tags

Active filters with remove buttons.

Active Filters:
Category: Electronics Price: $500-$1000 Rating: 4+ stars In Stock
<%= rui_badge(text: "Category: Electronics", close_button: true, color: :blue, variant: :soft) %>
<%= rui_badge(text: "Price: $500-$1000", close_button: true, color: :blue, variant: :soft) %>

Accessibility

The Badge component follows WAI-ARIA best practices for status and label elements.

ARIA Attributes

  • Close button includes aria-label="Remove" for screen readers
  • Clickable badges use role="link" when rendered as anchors
  • All color variants meet WCAG AA contrast ratio (≥4.5:1)

Keyboard Navigation

  • Tab focuses interactive badges (dismissible, closeable, clickable)
  • Enter or Space activates interactive badges
  • Focus states are clearly visible with ring styles

Semantic HTML

  • Renders as <span> for static badges (informational labels)
  • Renders as <button> for interactive badges (dismissible/closeable)
  • Renders as <a> for clickable badges with URL

Screen Reader Support

  • Badge text is announced as inline content without special roles
  • Don't rely solely on color - use text and icons to convey meaning
  • Use badges as supplementary labels, not primary content

JavaScript API

The Badge component exposes Stimulus actions for programmatic control.

Stimulus Actions

Use these actions via data-action attributes:

Action Description
badge#toggle Toggles badge visibility (adds/removes hidden class)
badge#close Removes the badge from DOM completely
badge#dismiss Hides the badge (adds hidden class, keeps in DOM)
Using Stimulus Actions
<%# Badge with close button %>
<%= rui_badge("New", closeable: true) %>

<%# External control button %>
<button data-action="badge#toggle">Toggle Badge</button>

<%# Custom close behavior %>
<div data-controller="badge">
  <span class="...">Status</span>
  <button data-action="click->badge#close">×</button>
</div>

Programmatic Control

Control the badge from JavaScript using the Stimulus controller:

Programmatic Control
// Get the badge controller
const badgeElement = document.querySelector('[data-controller="badge"]')
const badgeController = application.getControllerForElementAndIdentifier(
  badgeElement,
  "badge"
)

// Toggle visibility
badgeController.toggle()

// Hide badge (keeps in DOM)
badgeController.dismiss()

// Remove from DOM
badgeController.close()

API Reference

rui_badge

Lightweight labeling component for status, metadata, or categorization

Parameter Type Default Description
text String Badge text content (can be positional or keyword)

Appearance

Visual styling options

Parameter Type Default Description
color Symbol :primary Color theme - semantic (:primary, :success, :danger, :warning, :info) or any Tailwind color
size Symbol :xs Badge size
:xs :sm :base :md :lg :xl :xl2
variant Symbol :solid Visual style
:solid :outline :ghost :soft :link
shape Symbol :pill Border radius style
:rounded :pill :square :circle

Features

Additional badge features

Parameter Type Default Description
counter Integer Display numeric counter (for notifications, counts)
pulsing Boolean false Add pulsing animation for attention
url String Makes badge clickable (renders as <a> instead of <span>)

Interactive

Dismissible and close button options

Parameter Type Default Description
close_button Boolean false Show close button (X icon) on the badge
dismissible Boolean false Make entire badge dismissible on click

GitHub Style

GitHub-specific color presets for repository badges

Parameter Type Default Description
:gh_* Symbol GitHub colors: :gh_open, :gh_closed, :gh_merged, :gh_draft, :gh_issue, :gh_pr, :gh_sponsor, :gh_discussions

Slots

Content slots for customizing component parts

Slot Description
icon Icon via badge.with_icon(:name, position:) - supports :leading or :trailing position

Related Components