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_app_header, #coco_button, #coco_button_group, #coco_color_picker_button, #coco_confirm_button, #coco_dropdown_button, #coco_fields, #coco_form_for, #coco_form_with, #coco_image_picker_button, #coco_layout, #coco_layout_picker_button, #coco_link, #coco_menu_button, #coco_menu_item, #coco_notice, #coco_seamless_textarea, #coco_sidebar_nav, #coco_snackbar, #coco_system_banner, #coco_toast, #coco_toolbar

Methods included from BookHelper

#coco_editable_slide, #coco_media_slide

Methods included from BaseHelper

#coco_avatar, #coco_embed, #coco_icon, #coco_image, #coco_link_to_modal, #coco_modal, #coco_modal_data_attributes, #coco_modal_dialog, #coco_modal_frame_id, #coco_modal_lightbox, #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, 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
# File 'app/components/coco/base/button/button.rb', line 79

def initialize(click: nil, resize: nil, states: nil, loading: false, active: false, button_element: {}, **kwargs)
  @on_click = click
  @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

#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)


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

def active?
  @active == true
end

#alpine_dataObject



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

def alpine_data
  {tooltips: state_tooltips} if state_tooltips.present?
end

#button?Boolean

Returns:

  • (Boolean)


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

def button?
  button_tag_name == :button
end

#button_tag_nameObject



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

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

#button_textObject



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

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

#component_classesObject



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

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

#confirm?Boolean

Returns:

  • (Boolean)


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

def confirm?
  get_option_value(:confirm)
end

#disabled?Boolean

Returns:

  • (Boolean)


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

def disabled?
  get_option_value(:disabled)
end

Returns:

  • (Boolean)


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

def dropdown?
  false
end


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

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)


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

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

#link?Boolean

Returns:

  • (Boolean)


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

def link?
  button_tag_name == :a
end

#loading?Boolean

Returns:

  • (Boolean)


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

def loading?
  @loading == true
end

#state_tooltipsObject



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

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

#statesObject



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

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)


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

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

#toggle_directionObject



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

def toggle_direction
  get_option_value(:toggle)
end