Class: NitroKit::Badge

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/badge.rb

Constant Summary collapse

VARIANTS =
%i[default outline].freeze
SIZES =
%i[xs sm md].freeze
COLORS =
%i[
  zinc red orange amber yellow lime green emerald teal cyan sky blue indigo
  violet purple fuchsia pink rose neutral info success warning danger
].freeze

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text = nil, variant: :default, size: :md, color: :zinc, id: nil, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ Badge

Returns a new instance of Badge.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/components/nitro_kit/badge.rb', line 12

def initialize(
  text = nil,
  variant: :default,
  size: :md,
  color: :zinc,
  id: nil,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  if !text.nil? && text.to_s.strip.empty?
    raise ArgumentError, "Badge label content is required"
  end

  @text = text
  @variant = validate_choice!(:variant, variant, VARIANTS)
  @size = validate_choice!(:size, size, SIZES)
  @color = validate_choice!(:color, color, COLORS)

  super(
    component: :badge,
    attributes: {
      id:,
      data: { color: }
    },
    html:,
    aria:,
    data:,
    variant:,
    size:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#colorObject (readonly)

Returns the value of attribute color.



47
48
49
# File 'app/components/nitro_kit/badge.rb', line 47

def color
  @color
end

#sizeObject (readonly)

Returns the value of attribute size.



47
48
49
# File 'app/components/nitro_kit/badge.rb', line 47

def size
  @size
end

#textObject (readonly)

Returns the value of attribute text.



47
48
49
# File 'app/components/nitro_kit/badge.rb', line 47

def text
  @text
end

#variantObject (readonly)

Returns the value of attribute variant.



47
48
49
# File 'app/components/nitro_kit/badge.rb', line 47

def variant
  @variant
end

Instance Method Details

#view_template(&block) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/components/nitro_kit/badge.rb', line 49

def view_template(&block)
  if block && !text.nil?
    raise ArgumentError, "Badge accepts label text or block content, not both"
  end
  if !block && text.nil?
    raise ArgumentError, "Badge label content is required"
  end

  span(**root_attributes) do
    span(**slot_attributes(:label)) do
      block ? text_or_block(nil, &block) : plain(text.to_s)
    end
  end
end