Class: ElementComponent::Components::Button

Inherits:
Element
  • Object
show all
Defined in:
lib/element_component/components/button.rb

Constant Summary collapse

VALID_VARIANTS =
%i[primary secondary success danger warning info light dark link].freeze
VALID_SIZES =
%i[sm lg].freeze

Instance Attribute Summary

Attributes inherited from Element

#attributes, #contents, #element, #html

Instance Method Summary collapse

Methods inherited from Element

#add_attribute, #add_attribute!, #add_content, #add_content!, #new_element, #remove_attribute, #remove_attribute_value, #render, #reset_attributes!, #reset_contents!

Constructor Details

#initialize(variant: :primary, outline: false, size: nil, href: nil, **attributes, &block) ⇒ Button

Returns a new instance of Button.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/element_component/components/button.rb', line 9

def initialize(variant: :primary, outline: false, size: nil, href: nil, **attributes, &block)
  if href
    super("a", &block)
    add_attribute(href: href)
  else
    super("button", &block)
    add_attribute(type: "button")
  end

  add_attribute(class: "btn")
  add_attribute(class: outline ? "btn-outline-#{variant}" : "btn-#{variant}")
  add_attribute(class: "btn-#{size}") if size
  add_attribute(attributes) unless attributes.empty?
end