Class: LesliView::Elements::Button

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
lib/lesli_view/elements/button.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label = nil, url: nil, icon: nil, loading: false, solid: false, info: false, success: false, warning: false, danger: false, small: false, dispatch: nil, method: nil, params: nil, css_class: nil) ⇒ Integer

Adds two numbers together.

Parameters:

  • a (Integer)

    The first number.

  • b (Integer)

    The second number.



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/lesli_view/elements/button.rb', line 44

def initialize(
    label=nil, 
    url:nil,
    icon: nil, 
    loading: false, 
    solid: false, 
    info: false, 
    success: false, 
    warning: false, 
    danger: false, 
    small: false,
    dispatch:nil,
    method: nil,
    params:nil,
    css_class:nil
)
    @label = label
    @url = url
    @icon = icon
    @loading = loading
    @solid = solid
    @info = info
    @success = success
    @warning = warning
    @danger = danger
    @small = small
    @dispatch = dispatch
    @method = method
    @params = params
    @css_class = css_class
end

Instance Attribute Details

#css_classObject (readonly)

Returns the value of attribute css_class.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def css_class
  @css_class
end

#dangerObject (readonly)

Returns the value of attribute danger.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def danger
  @danger
end

#dispatchObject (readonly)

Returns the value of attribute dispatch.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def dispatch
  @dispatch
end

#iconObject (readonly)

Returns the value of attribute icon.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def icon
  @icon
end

#infoObject (readonly)

Returns the value of attribute info.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def info
  @info
end

#labelObject (readonly)

Returns the value of attribute label.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def label
  @label
end

#loadingObject (readonly)

Returns the value of attribute loading.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def loading
  @loading
end

#methodObject (readonly)

Returns the value of attribute method.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def method
  @method
end

#paramsObject (readonly)

Returns the value of attribute params.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def params
  @params
end

#smallObject (readonly)

Returns the value of attribute small.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def small
  @small
end

#solidObject (readonly)

Returns the value of attribute solid.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def solid
  @solid
end

#successObject (readonly)

Returns the value of attribute success.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def success
  @success
end

#urlObject (readonly)

Returns the value of attribute url.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def url
  @url
end

#warningObject (readonly)

Returns the value of attribute warning.



38
39
40
# File 'lib/lesli_view/elements/button.rb', line 38

def warning
  @warning
end

Instance Method Details

#alpine_attributesObject



82
83
84
85
86
87
88
89
# File 'lib/lesli_view/elements/button.rb', line 82

def alpine_attributes
    return {} unless @dispatch

    {
    "x-data": "",
    "@click": "$dispatch('#{@dispatch}')"
    }
end

#button_contentObject



112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/lesli_view/elements/button.rb', line 112

def button_content
    if @icon
        safe_join([
            (:span,
                (:span, @icon, class: "material-symbols"),
                class: "icon #{'is-small' if @small}"
            ),
            (@icon_only ? nil : (:span, @label))
        ].compact)
    else
        @label
    end
end

#button_variantObject



100
101
102
103
104
105
106
# File 'lib/lesli_view/elements/button.rb', line 100

def button_variant
    return "is-warning" if warning
    return "is-success" if success
    return "is-danger" if danger
    return "is-info" if info
    "is-primary"
end

#html_classesObject



91
92
93
94
95
96
97
98
# File 'lib/lesli_view/elements/button.rb', line 91

def html_classes
    classes = ["button", button_variant]
    classes << "is-light" << "is-outlined" unless solid
    classes << "is-loading" if loading
    classes << "is-small" if small
    classes << css_class 
    classes.join(" ")
end

#icon_only?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'lib/lesli_view/elements/button.rb', line 108

def icon_only?
    icon && !label
end

#modeObject



76
77
78
79
80
# File 'lib/lesli_view/elements/button.rb', line 76

def mode
    return :button_to if @method.present?
    return :link if @url.present?
    :button
end