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 CoreComponentsHelper

#coco_avatar, #coco_badge, #coco_button, #coco_button_group, #coco_button_to, #coco_color_picker_button, #coco_component, #coco_confirm_button, #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_button, #coco_menu_item, #coco_modal, #coco_modal_dialog, #coco_modal_lightbox, #coco_notice, #coco_option_bar, #coco_page, #coco_pager_button, #coco_panel, #coco_placeholder, #coco_prose, #coco_seamless_textarea, #coco_snackbar, #coco_spacer, #coco_stack, #coco_stamp, #coco_svg, #coco_system_banner, #coco_tag, #coco_toast, #coco_toolbar, #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, **kwargs) ⇒ Button

Returns a new instance of Button.



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

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

Instance Attribute Details

#on_clickObject (readonly)

Returns the value of attribute on_click.



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

def on_click
  @on_click
end

#resizeObject (readonly)

Returns the value of attribute resize.



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

def resize
  @resize
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  @active == true
end

#alpine_dataObject



185
186
187
# File 'app/components/coco/buttons/button/button.rb', line 185

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

#alpine_wrapper_attrsObject



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

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

#button?Boolean

Returns:

  • (Boolean)


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

def button?
  button_tag == :button
end

#button_tagObject



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

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

#button_textObject



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

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

#confirm?Boolean

Returns:

  • (Boolean)


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

def confirm?
  get_option_value(:confirm)
end

#disabled?Boolean

Returns:

  • (Boolean)


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

def disabled?
  get_option_value(:disabled)
end

#icon_only?Boolean

Returns:

  • (Boolean)


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

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

#link?Boolean

Returns:

  • (Boolean)


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

def link?
  button_tag == :a
end

#loading?Boolean

Returns:

  • (Boolean)


118
119
120
# File 'app/components/coco/buttons/button/button.rb', line 118

def loading?
  @loading == true
end

#state_tooltipsObject



179
180
181
182
183
# File 'app/components/coco/buttons/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/buttons/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

#static?Boolean

Returns:

  • (Boolean)


110
111
112
113
114
115
116
# File 'app/components/coco/buttons/button/button.rb', line 110

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)


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

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

#toggle_directionObject



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

def toggle_direction
  get_option_value(:toggle)
end

#tooltip?Boolean

Returns:

  • (Boolean)


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

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

#with_confirmationObject



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

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

#with_dropdownObject



86
87
88
# File 'app/components/coco/buttons/button/button.rb', line 86

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