Class: DaisyUI::Badge

Inherits:
BaseComponent show all
Defined in:
app/components/daisy_ui/data_display/badge.rb

Overview

Badge component implementing DaisyUI’s badge styles

Examples:

Basic usage

<%= render(BadgeComponent.new(text: "Badge")) %>

Primary variant

<%= render(BadgeComponent.new(text: "Primary", color: :primary)) %>

Outline style

<%= render(BadgeComponent.new(text: "Outline", variant: :outline)) %>

Small size with icon

<%= render(BadgeComponent.new(
  text: "Icon",
  size: :sm,
  icon: helpers.check_icon
)) %>

Soft style with color

<%= render(BadgeComponent.new(
  text: "Soft",
  variant: :soft,
  color: :primary
)) %>

With block content

<%= render(BadgeComponent.new) do %>
  Complex <strong>content</strong>
<% end %>

With custom tag type

<%= render(BadgeComponent.new(text: "Badge", tag_type: :span)) %>

Constant Summary collapse

COLORS =

Available badge colors from DaisyUI

{
  primary: 'badge-primary',
  secondary: 'badge-secondary',
  accent: 'badge-accent',
  neutral: 'badge-neutral',
  ghost: 'badge-ghost',
  info: 'badge-info',
  success: 'badge-success',
  warning: 'badge-warning',
  error: 'badge-error'
}.freeze
SIZES =

Available badge sizes from DaisyUI

{
  xl: 'badge-xl',
  lg: 'badge-lg',
  md: 'badge-md',
  sm: 'badge-sm',
  xs: 'badge-xs'
}.freeze
VARIANTS =

Available badge variants from DaisyUI

{
  outline: 'badge-outline',
  soft: 'badge-soft',
  dash: 'badge-dash',
  ghost: 'badge-ghost'
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(text = nil, color: nil, size: nil, variant: nil, icon: nil, tag_type: :div, **system_arguments) ⇒ Badge



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/components/daisy_ui/data_display/badge.rb', line 75

def initialize(
  text = nil,
  color: nil,
  size: nil,
  variant: nil,
  icon: nil,
  tag_type: :div,
  **system_arguments
)
  @text = text
  @color = build_argument(color, COLORS, 'color')
  @size = build_argument(size, SIZES, 'size')
  @variant = build_argument(variant, VARIANTS, 'variant')
  @icon = icon
  @tag_type = tag_type

  super(**system_arguments)
end

Instance Method Details

#callObject



94
95
96
97
98
# File 'app/components/daisy_ui/data_display/badge.rb', line 94

def call
  tag.public_send(@tag_type, **html_attributes) do
    safe_join([render_icon, content || @text].compact)
  end
end