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]

Constants included from Concerns::AcceptsTagAttributes

Concerns::AcceptsTagAttributes::TAG_ATTRIBUTE_NAMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Concerns::AcceptsOptions

#accepted_options, #get_option_group

Methods inherited from Component

#accepts_options?, after_initialize, before_initialize, before_render, new

Methods included from AlpineHelper

#x_attrs, #x_data

Methods included from TagHelper

#prefix_attr_keys, #random_id, #style_str

Methods included from ComponentHelper

#coco_component, #resolve_component

Methods included from AppHelper

#coco_button, #coco_button_group, #coco_color_picker_button, #coco_confirm_button, #coco_dropdown_button, #coco_fields, #coco_form_button, #coco_form_for, #coco_form_with, #coco_image_picker_button, #coco_layout_picker_button, #coco_link, #coco_menu_button, #coco_notice, #coco_seamless_textarea, #coco_snackbar, #coco_system_banner, #coco_toast, #coco_toolbar

Methods included from BookHelper

#coco_editable_slide, #coco_media_slide

Methods included from BaseHelper

#coco_embed, #coco_icon, #coco_image, #coco_placeholder, #coco_svg, #coco_tag

Methods included from Concerns::Translatable

#tt

Methods included from Concerns::HasName

#component_name

Methods included from Concerns::AcceptsTagAttributes

#tag_attrs

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.



79
80
81
82
83
84
85
86
87
88
89
90
# File 'app/components/coco/base/button/button.rb', line 79

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_attrsObject (readonly)

Returns the value of attribute button_attrs.



77
78
79
# File 'app/components/coco/base/button/button.rb', line 77

def button_attrs
  @button_attrs
end

#on_clickObject (readonly)

Returns the value of attribute on_click.



77
78
79
# File 'app/components/coco/base/button/button.rb', line 77

def on_click
  @on_click
end

#propsObject (readonly)

Returns the value of attribute props.



77
78
79
# File 'app/components/coco/base/button/button.rb', line 77

def props
  @props
end

#resizeObject (readonly)

Returns the value of attribute resize.



77
78
79
# File 'app/components/coco/base/button/button.rb', line 77

def resize
  @resize
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  @active == true
end

#alpine_dataObject



185
186
187
188
189
190
191
192
# File 'app/components/coco/base/button/button.rb', line 185

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)


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

def button?
  button_tag_name == :button
end

#button_tag_nameObject



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

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

#button_textObject



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

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

#component_classesObject



150
151
152
153
154
155
156
157
158
159
# File 'app/components/coco/base/button/button.rb', line 150

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

#confirm?Boolean

Returns:

  • (Boolean)


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

def confirm?
  get_option_value(:confirm)
end

#disabled?Boolean

Returns:

  • (Boolean)


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

def disabled?
  get_option_value(:disabled)
end

Returns:

  • (Boolean)


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

def dropdown?
  false
end


140
141
142
143
144
145
146
147
148
# File 'app/components/coco/base/button/button.rb', line 140

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)


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

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

#link?Boolean

Returns:

  • (Boolean)


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

def link?
  button_tag_name == :a
end

#loading?Boolean

Returns:

  • (Boolean)


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

def loading?
  @loading == true
end

#state_tooltipsObject



179
180
181
182
183
# File 'app/components/coco/base/button/button.rb', line 179

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

#statesObject



161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'app/components/coco/base/button/button.rb', line 161

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)


92
93
94
# File 'app/components/coco/base/button/button.rb', line 92

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

#toggle_directionObject



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

def toggle_direction
  get_option_value(:toggle)
end