Class: Coco::Button

Inherits:
Component
  • Object
show all
Includes:
Concerns::AcceptsOptions, Concerns::AcceptsTheme, Concerns::Extendable, Concerns::WithIcon, Concerns::WithTooltip
Defined in:
app/components/coco/base/button/button.rb

Direct Known Subclasses

App::Elements::Button

Constant Summary collapse

SIZES =
[:sm, :md, :lg, nil]
SIZE_ALIASES =
{
  default: [:sm, {xl: :md}]
}
THEMES =
[]
DEFAULT_THEME =
nil
BUTTON_ATTRS =
%i[type value name disabled href]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::AcceptsOptions

#accepted_options, #get_option_group

Constructor Details

#initialize(click: nil, resize: nil, props: nil, states: nil, loading: false, active: false, button_element: {}, **kwargs) ⇒ Button

Returns a new instance of Button.



84
85
86
87
88
89
90
91
92
93
94
95
# File 'app/components/coco/base/button/button.rb', line 84

def initialize(click: nil, resize: nil, props: nil, states: nil, loading: false, active: false, button_element: {}, **kwargs)
  @on_click = click
  @props = props.to_h
  @resize = resize.to_h
  @states = states.to_h
  @loading = loading
  @active = active
  @alpine_data = {}
  @button_element_attrs = button_element

  super(**kwargs)
end

Instance Attribute Details

#button_attrs ⇒ Object (readonly)

Returns the value of attribute button_attrs.



82
83
84
# File 'app/components/coco/base/button/button.rb', line 82

def button_attrs
  @button_attrs
end

#on_click ⇒ Object (readonly)

Returns the value of attribute on_click.



82
83
84
# File 'app/components/coco/base/button/button.rb', line 82

def on_click
  @on_click
end

#props ⇒ Object (readonly)

Returns the value of attribute props.



82
83
84
# File 'app/components/coco/base/button/button.rb', line 82

def props
  @props
end

#resize ⇒ Object (readonly)

Returns the value of attribute resize.



82
83
84
# File 'app/components/coco/base/button/button.rb', line 82

def resize
  @resize
end

Instance Method Details

#active? ⇒ Boolean

Returns:

  • (Boolean)


117
118
119
# File 'app/components/coco/base/button/button.rb', line 117

def active?
  @active == true
end

#alpine_data ⇒ Object



190
191
192
193
194
195
196
197
# File 'app/components/coco/base/button/button.rb', line 190

def alpine_data
  if props.present? || state_tooltips.present?
    {
      tooltips: (state_tooltips if state_tooltips.present?),
      props: (props if props.present?)
    }.compact
  end
end

#button? ⇒ Boolean

Returns:

  • (Boolean)


133
134
135
# File 'app/components/coco/base/button/button.rb', line 133

def button?
  button_tag_name == :button
end

#button_tag_name ⇒ Object



105
106
107
# File 'app/components/coco/base/button/button.rb', line 105

def button_tag_name
  button_attrs[:href].present? ? :a : :button
end

#button_text ⇒ Object



109
110
111
# File 'app/components/coco/base/button/button.rb', line 109

def button_text
  text&.to_s || content&.to_s || ""
end

#component_classes ⇒ Object



155
156
157
158
159
160
161
162
163
164
# File 'app/components/coco/base/button/button.rb', line 155

def component_classes
  [
    "coco-button",
    tag_attrs[:class],
    {
      "icon-only": icon_only?,
      "with-icon": (icon? && !icon_only?)
    }
  ]
end

#confirm? ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
# File 'app/components/coco/base/button/button.rb', line 121

def confirm?
  get_option_value(:confirm)
end

#disabled? ⇒ Boolean

Returns:

  • (Boolean)


125
126
127
# File 'app/components/coco/base/button/button.rb', line 125

def disabled?
  get_option_value(:disabled)
end

Returns:

  • (Boolean)


141
142
143
# File 'app/components/coco/base/button/button.rb', line 141

def dropdown?
  false
end


145
146
147
148
149
150
151
152
153
# File 'app/components/coco/base/button/button.rb', line 145

def dropdown_opts
  if dropdown?
    jsify_data({
      appendTo: "parent",
      offset: [0, 1],
      placement: get_option_value(:dropdown, :placement)
    }.compact)
  end
end

#icon_only? ⇒ Boolean

Returns:

  • (Boolean)


137
138
139
# File 'app/components/coco/base/button/button.rb', line 137

def icon_only?
  !states.find { _2[:text].present? }
end

#link? ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'app/components/coco/base/button/button.rb', line 129

def link?
  button_tag_name == :a
end

#loading? ⇒ Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/components/coco/base/button/button.rb', line 113

def loading?
  @loading == true
end

#state_tooltips ⇒ Object



184
185
186
187
188
# File 'app/components/coco/base/button/button.rb', line 184

def state_tooltips
  @_tooltips = states.map do |name, props|
    [name, props[:tooltip]] if props[:tooltip].present?
  end.compact.to_h
end

#states ⇒ Object



166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'app/components/coco/base/button/button.rb', line 166

def states
  @_states ||= begin
    states = default_states.deep_merge(@states)

    states.each do |name, props|
      if props.key?(:icon)
        if props[:icon] == false
          props.except!(:icon) # explicitly no icon
        else
          props[:icon] = render_icon(props[:icon])
        end
      elsif icon?
        props[:icon] = icon # no icon specified, use the icon for the default state
      end
    end.compact.transform_keys { _1.to_s.camelcase(:lower) }
  end
end

#toggle? ⇒ Boolean

Returns:

  • (Boolean)


97
98
99
# File 'app/components/coco/base/button/button.rb', line 97

def toggle?
  toggle_direction.present? && button_text.present?
end

#toggle_direction ⇒ Object



101
102
103
# File 'app/components/coco/base/button/button.rb', line 101

def toggle_direction
  get_option_value(:toggle)
end