Module: UI::ButtonBehavior

Included in:
Button, ButtonComponent, InputGroupButton
Defined in:
app/behaviors/ui/button_behavior.rb

Overview

UI::ButtonBehavior

Instance Method Summary collapse

Instance Method Details

#button_classesObject

Returns combined CSS classes for the button



55
56
57
58
59
60
61
62
63
# File 'app/behaviors/ui/button_behavior.rb', line 55

def button_classes
  classes_value = respond_to?(:classes, true) ? classes : @classes
  TailwindMerge::Merger.new.merge([
    button_base_classes,
    button_variant_classes,
    button_size_classes,
    classes_value
  ].compact.join(" "))
end

#button_html_attributesObject

Returns HTML attributes for the button element



46
47
48
49
50
51
52
# File 'app/behaviors/ui/button_behavior.rb', line 46

def button_html_attributes
  {
    class: button_classes,
    type: @type || "button",
    disabled: @disabled ? true : nil
  }.compact
end

#render_button(&content_block) ⇒ String

Renders the button HTML This method can be used by both ERB partials and ViewComponents

Parameters:

  • content_block (Proc)

    Block that returns the button content

Returns:

  • (String)

    HTML string for the button



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/behaviors/ui/button_behavior.rb', line 28

def render_button(&content_block)
  all_attributes = button_html_attributes

  # Merge classes with TailwindMerge before deep_merge
  if @attributes&.key?(:class)
    button_class = all_attributes[:class] || ""
    attr_class = @attributes[:class] || ""
    merged_class = TailwindMerge::Merger.new.merge([button_class, attr_class].join(" "))
    all_attributes = all_attributes.merge(class: merged_class)
  end

  # Deep merge other attributes (excluding class which we already handled)
  all_attributes = all_attributes.deep_merge(@attributes&.except(:class) || {})

  (:button, **all_attributes, &content_block)
end