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

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_page, #coco_panel, #coco_seamless_textarea, #coco_sidebar_nav, #coco_sidebar_nav_menu, #coco_snackbar, #coco_stamp, #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, tooltip: nil, **kwargs) ⇒ Button

Returns a new instance of Button.



68
69
70
71
72
73
74
# File 'app/components/coco/base/button/button.rb', line 68

def initialize(click: nil, resize: nil, states: nil, loading: false, active: false, tooltip: nil, **kwargs)
  @on_click = click
  @resize = resize.to_h
  @states = states.to_h
  @loading = loading
  @active = active
end

Instance Attribute Details

#on_click ⇒ Object (readonly)

Returns the value of attribute on_click.



66
67
68
# File 'app/components/coco/base/button/button.rb', line 66

def on_click
  @on_click
end

#resize ⇒ Object (readonly)

Returns the value of attribute resize.



66
67
68
# File 'app/components/coco/base/button/button.rb', line 66

def resize
  @resize
end

Instance Method Details

#active? ⇒ Boolean

Returns:

  • (Boolean)


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

def active?
  @active == true
end

#alpine_data ⇒ Object



161
162
163
# File 'app/components/coco/base/button/button.rb', line 161

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

#alpine_wrapper_attrs ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
# File 'app/components/coco/base/button/button.rb', line 124

def alpine_wrapper_attrs
  if dropdown? || confirm?
    {
      data: x_data("buttonDropdown"),
      dropdown: jsify_data({offset: [0, 1], placement: get_option_value(:dropdown, :placement)}.compact),
      "@dropdown:show": ("button.setState('active')" if dropdown?),
      "@dropdown:hide": ("button.resetState()" if dropdown?),
      "@confirmation:confirm": ("button.approveAndRun($event)" if confirm?),
      "@confirmation:cancel": ("button.cancelConfirmation($event)" if confirm?)
    }
  end
end

#button? ⇒ Boolean

Returns:

  • (Boolean)


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

def button?
  button_tag == :button
end

#button_tag ⇒ Object



84
85
86
# File 'app/components/coco/base/button/button.rb', line 84

def button_tag
  tag_attr(:href).present? ? :a : :button
end

#button_text ⇒ Object



88
89
90
# File 'app/components/coco/base/button/button.rb', line 88

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

#confirm? ⇒ Boolean

Returns:

  • (Boolean)


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

def confirm?
  get_option_value(:confirm)
end

#disabled? ⇒ Boolean

Returns:

  • (Boolean)


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

def disabled?
  get_option_value(:disabled)
end

Returns:

  • (Boolean)


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

def dropdown?
  false
end

#icon_only? ⇒ Boolean

Returns:

  • (Boolean)


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

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

#link? ⇒ Boolean

Returns:

  • (Boolean)


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

def link?
  button_tag == :a
end

#loading? ⇒ Boolean

Returns:

  • (Boolean)


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

def loading?
  @loading == true
end

#state_tooltips ⇒ Object



155
156
157
158
159
# File 'app/components/coco/base/button/button.rb', line 155

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

#states ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'app/components/coco/base/button/button.rb', line 137

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)


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

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

#toggle_direction ⇒ Object



80
81
82
# File 'app/components/coco/base/button/button.rb', line 80

def toggle_direction
  get_option_value(:toggle)
end