Class: BulmaPhlex::Button
- Defined in:
- lib/bulma_phlex/button.rb
Overview
Renders a Bulma button element.
The component can generate a <button>, <a>, or <input> element depending on the
arguments provided. Supports Bulma options for color, size, style mode,
and layout (responsive, fullwidth, outlined, inverted, rounded), as well as
optional icons on the left and/or right side of the label.
The label can be passed into the component as a string or from a block.
Class Method Summary collapse
-
.classes_for(color: nil, mode: nil, size: nil, responsive: false, fullwidth: false, outlined: false, inverted: false, rounded: false) ⇒ Object
Returns an array of CSS classes for the button based on the provided options.
-
.new(label = nil, color: nil, mode: nil, size: nil, responsive: false, fullwidth: false, outlined: false, inverted: false, rounded: false, icon: nil, icon_left: nil, icon_right: nil, input: nil, **html_attributes) ⇒ Object
Parameters.
Instance Method Summary collapse
-
#initialize(label = nil, color: nil, mode: nil, size: nil, responsive: false, fullwidth: false, outlined: false, inverted: false, rounded: false, icon: nil, icon_left: nil, icon_right: nil, input: nil, **html_attributes) ⇒ Button
constructor
A new instance of Button.
- #view_template ⇒ Object
Constructor Details
#initialize(label = nil, color: nil, mode: nil, size: nil, responsive: false, fullwidth: false, outlined: false, inverted: false, rounded: false, icon: nil, icon_left: nil, icon_right: nil, input: nil, **html_attributes) ⇒ Button
Returns a new instance of Button.
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bulma_phlex/button.rb', line 67 def initialize(label = nil, color: nil, mode: nil, size: nil, responsive: false, fullwidth: false, outlined: false, inverted: false, rounded: false, icon: nil, icon_left: nil, icon_right: nil, input: nil, **html_attributes) @label = label @classes = self.class.classes_for(color:, mode:, size:, responsive:, fullwidth:, outlined:, inverted:, rounded:) @input = input @icon_left = icon || icon_left @icon_right = icon_right @html_attributes = html_attributes end |
Class Method Details
.classes_for(color: nil, mode: nil, size: nil, responsive: false, fullwidth: false, outlined: false, inverted: false, rounded: false) ⇒ Object
Returns an array of CSS classes for the button based on the provided options.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# File 'lib/bulma_phlex/button.rb', line 14 def self.classes_for(color: nil, # rubocop:disable Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity mode: nil, size: nil, responsive: false, fullwidth: false, outlined: false, inverted: false, rounded: false) classes = ["button"] classes << "is-#{color}" if color classes << "is-#{mode}" if mode classes << "is-#{size}" if size classes << "is-responsive" if responsive classes << "is-fullwidth" if fullwidth classes << "is-outlined" if outlined classes << "is-inverted" if inverted classes << "is-rounded" if rounded classes end |
.new(label = nil, color: nil, mode: nil, size: nil, responsive: false, fullwidth: false, outlined: false, inverted: false, rounded: false, icon: nil, icon_left: nil, icon_right: nil, input: nil, **html_attributes) ⇒ Object
Parameters
label(positional) — Optionally pass in button label as a stringcolor— Button color:"primary","link","info","success","warning","danger"mode— Button style mode:"light"or"dark"size— Button size:"small","normal","medium","large"responsive— Iftrue, makes the button responsivefullwidth— Iftrue, makes the button full widthoutlined— Iftrue, renders the button outlinedinverted— Iftrue, renders the button invertedrounded— Iftrue, renders the button roundedicon— Icon class added to the button (e.g."fa-solid fa-check"); shorthand foricon_lefticon_left— Icon class added to the left of the button texticon_right— Icon class added to the right of the button textinput— If set, renders an<input>element of this type ("button","reset","submit")**html_attributes— Additional HTML attributes for the button element
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/bulma_phlex/button.rb', line 50 def self.new(label = nil, color: nil, mode: nil, size: nil, responsive: false, fullwidth: false, outlined: false, inverted: false, rounded: false, icon: nil, icon_left: nil, icon_right: nil, input: nil, **html_attributes) super end |
Instance Method Details
#view_template ⇒ Object
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/bulma_phlex/button.rb', line 89 def view_template(&) block_content = capture(&) = mix({ class: @classes }, @html_attributes) if @input input(**, type: @input) else element_type = @html_attributes.key?(:href) ? :a : :button tag(element_type, **) do Icon(@icon_left) if @icon_left (block_content) Icon(@icon_right) if @icon_right end end end |