Class: Coco::Button

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

Constant Summary collapse

SIZES =
[:xs, :sm, :md, :lg, nil]
SIZE_ALIASES =
{
  default: [:sm, {xl: :md}]
}
THEMES =
[
  "primary",
  "text-primary",
  "secondary",
  "text-secondary",
  "positive",
  "text-positive",
  "negative",
  "text-negative",
  "warning",
  "text-warning",
  "info",
  "text-info",
  "toolbar",
  "toolbar-floating",
  "text-toolbar",
  "neutral-dark",
  "neutral-light",
  "text-neutral-light",
  "text-neutral-dark",
  "blank",
  nil
]
DEFAULT_THEME =
"primary"

Constants included from Concerns::AcceptsTagAttributes

Concerns::AcceptsTagAttributes::TAG_ATTRIBUTE_NAMES

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from ModalHelper

coco_link_to_modal, coco_modal_data_attributes, coco_modal_frame_id

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 ComponentsHelper

#coco_avatar, #coco_badge, #coco_button, #coco_button_group, #coco_button_to, #coco_color_picker_button, #coco_component, #coco_confirm_button, #coco_dropdown, #coco_embed, #coco_fields, #coco_form_for, #coco_form_with, #coco_icon, #coco_image, #coco_image_picker_button, #coco_layout_picker_button, #coco_link, #coco_link_to, #coco_menu, #coco_menu_button, #coco_menu_item, #coco_modal, #coco_modal_canvas, #coco_modal_dialog, #coco_modal_lightbox, #coco_notice, #coco_option_bar, #coco_page, #coco_pager_button, #coco_panel, #coco_placeholder, #coco_popover, #coco_prose, #coco_seamless_textarea, #coco_snackbar, #coco_spacer, #coco_stack, #coco_stamp, #coco_svg, #coco_system_banner, #coco_tabs, #coco_tag, #coco_toast, #coco_toolbar, #coco_tooltip, #resolve_component

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, static: nil, turbo: nil, turbo_frame: nil, **kwargs) ⇒ Button

Returns a new instance of Button.



78
79
80
81
82
83
84
85
86
87
# File 'app/components/coco/buttons/button/button.rb', line 78

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

Instance Attribute Details

#on_clickObject (readonly)

Returns the value of attribute on_click.



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

def on_click
  @on_click
end

#resizeObject (readonly)

Returns the value of attribute resize.



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

def resize
  @resize
end

#turbo_frameObject (readonly)

Returns the value of attribute turbo_frame.



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

def turbo_frame
  @turbo_frame
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  @active == true
end

#alpine_dataObject



188
189
190
# File 'app/components/coco/buttons/button/button.rb', line 188

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

#alpine_wrapper_attrsObject



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

def alpine_wrapper_attrs
  if dropdown? || confirm?
    {
      data: x_data("buttonDropdown"),
      dropdown: jsify_data({placement: get_option_value(:dropdown, :placement), flip: get_option_value(:dropdown, :flip)}.compact),
      bind: "root"
    }
  end
end

#button?Boolean

Returns:

  • (Boolean)


145
146
147
# File 'app/components/coco/buttons/button/button.rb', line 145

def button?
  button_tag == :button
end

#button_tagObject



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

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

#button_textObject



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

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

#confirm?Boolean

Returns:

  • (Boolean)


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

def confirm?
  get_option_value(:confirm)
end

#disabled?Boolean

Returns:

  • (Boolean)


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

def disabled?
  get_option_value(:disabled)
end

#icon_only?Boolean

Returns:

  • (Boolean)


149
150
151
152
# File 'app/components/coco/buttons/button/button.rb', line 149

def icon_only?
  (@states.none? && button_text.blank?) ||
    !states.find { _2[:text].present? }
end

#link?Boolean

Returns:

  • (Boolean)


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

def link?
  button_tag == :a
end

#loading?Boolean

Returns:

  • (Boolean)


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

def loading?
  @loading == true
end

#state_tooltipsObject



182
183
184
185
186
# File 'app/components/coco/buttons/button/button.rb', line 182

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

#statesObject



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

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

#static?Boolean

Returns:

  • (Boolean)


113
114
115
116
117
118
119
# File 'app/components/coco/buttons/button/button.rb', line 113

def static?
  @_static ||= if @static.nil?
    !(confirm? || dropdown? || tooltip? || @states.any? || on_click.present? || get_option_value(:collapsible) || tag_attrs.key?(:x))
  else
    @static
  end
end

#toggle?Boolean

Returns:

  • (Boolean)


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

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

#toggle_directionObject



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

def toggle_direction
  get_option_value(:toggle)
end

#tooltip?Boolean

Returns:

  • (Boolean)


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

def tooltip?
  get_option_value(:tooltip, :content).present?
end

#turbo_data_attr_valueObject



192
193
194
195
196
197
198
# File 'app/components/coco/buttons/button/button.rb', line 192

def turbo_data_attr_value
  if @turbo == false
    "false"
  elsif @turbo == true
    "true"
  end
end

#with_confirmationObject



93
94
95
# File 'app/components/coco/buttons/button/button.rb', line 93

def with_confirmation(...)
  with_dropdown_confirmation(...)
end

#with_dropdownObject



89
90
91
# File 'app/components/coco/buttons/button/button.rb', line 89

def with_dropdown(...)
  with_dropdown_content(...)
end