Class: IronAdmin::Ui::BadgeComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/iron_admin/ui/badge_component.rb

Overview

Displays a colored badge/tag for status indicators and labels.

Examples:

Basic usage

render IronAdmin::Ui::BadgeComponent.new(text: "Active", color: :green)

With size

render IronAdmin::Ui::BadgeComponent.new(text: "New", color: :blue, size: :sm)

Constant Summary collapse

COLORS =

Available color themes mapped to Tailwind classes.

Returns:

  • (Hash{Symbol => String})
{
  green: "bg-green-100 text-green-800",
  red: "bg-red-100 text-red-800",
  yellow: "bg-yellow-100 text-yellow-800",
  blue: "bg-blue-100 text-blue-800",
  indigo: "bg-indigo-100 text-indigo-800",
  purple: "bg-purple-100 text-purple-800",
  pink: "bg-pink-100 text-pink-800",
  orange: "bg-orange-100 text-orange-800",
  teal: "bg-teal-100 text-teal-800",
  gray: "bg-gray-100 text-gray-800",
}.freeze
SIZES =

Available sizes mapped to Tailwind classes.

Returns:

  • (Hash{Symbol => String})
{
  sm: "px-2 py-0.5 text-xs",
  md: "px-2.5 py-0.5 text-sm",
  lg: "px-3 py-1 text-sm",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text:, color: :gray, size: :md) ⇒ BadgeComponent

Returns a new instance of BadgeComponent.

Parameters:

  • text (String)

    The badge text

  • color (Symbol) (defaults to: :gray)

    Color theme (default: :gray)

  • size (Symbol) (defaults to: :md)

    Size variant (default: :md)



48
49
50
51
52
# File 'app/components/iron_admin/ui/badge_component.rb', line 48

def initialize(text:, color: :gray, size: :md)
  @text = text
  @color = color.to_sym
  @size = size.to_sym
end

Instance Attribute Details

#colorSymbol (readonly)

Returns The color theme (:green, :red, :yellow, :blue, etc.).

Returns:

  • (Symbol)

    The color theme (:green, :red, :yellow, :blue, etc.)



17
18
19
# File 'app/components/iron_admin/ui/badge_component.rb', line 17

def color
  @color
end

#sizeSymbol (readonly)

Returns The size (:sm, :md, :lg).

Returns:

  • (Symbol)

    The size (:sm, :md, :lg)



20
21
22
# File 'app/components/iron_admin/ui/badge_component.rb', line 20

def size
  @size
end

#textString (readonly)

Returns The text displayed in the badge.

Returns:

  • (String)

    The text displayed in the badge



14
15
16
# File 'app/components/iron_admin/ui/badge_component.rb', line 14

def text
  @text
end

Instance Method Details

#callString

Renders the badge span.

Returns:

  • (String)

    HTML content



68
69
70
# File 'app/components/iron_admin/ui/badge_component.rb', line 68

def call
  tag.span(text, class: "inline-flex items-center font-medium rounded-full #{color_classes} #{size_classes}")
end

#color_classesString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns CSS color classes for badge.

Returns:

  • (String)

    CSS color classes for badge



56
57
58
# File 'app/components/iron_admin/ui/badge_component.rb', line 56

def color_classes
  IronAdmin.configuration.badge_colors[@color] || COLORS[@color] || COLORS[:gray]
end

#size_classesString

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns CSS size classes for badge.

Returns:

  • (String)

    CSS size classes for badge



62
63
64
# File 'app/components/iron_admin/ui/badge_component.rb', line 62

def size_classes
  SIZES[@size] || SIZES[:md]
end