Class: Tramway::ButtonComponent

Inherits:
BaseComponent
  • Object
show all
Includes:
ColorsMethods
Defined in:
app/components/tramway/button_component.rb

Overview

Default Tramway button

Constant Summary collapse

DEFAULT_BUTTON_CLASSES =
%w[
  inline-flex items-center justify-center rounded-md font-medium ring-offset-background transition-colors
  focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-ring focus-visible:ring-offset-2
  disabled:pointer-events-none disabled:opacity-50
].freeze

Constants included from ColorsMethods

ColorsMethods::TYPE_COLOR_MAP

Constants included from Helpers::ViewsHelper

Helpers::ViewsHelper::FORM_SIZES

Instance Method Summary collapse

Methods included from ColorsMethods

#normalized_type, #resolved_color, #type_color

Methods included from Helpers::ViewsHelper

#tramway_back_button, #tramway_badge, #tramway_button, #tramway_cell, #tramway_chat, #tramway_container, #tramway_flash, #tramway_form_for, #tramway_header, #tramway_main_container, #tramway_row, #tramway_table, #tramway_title

Methods included from Helpers::ComponentHelper

#component

Methods included from Helpers::DecorateHelper

#tramway_decorate

Instance Method Details

#before_renderObject



25
26
27
28
29
30
31
32
33
34
35
# File 'app/components/tramway/button_component.rb', line 25

def before_render
  return if tag.present?

  @tag = if tag_button?
           :button
         elsif tag_a?
           :a
         else
           :form
         end
end

#classesObject



53
54
55
56
57
58
59
# File 'app/components/tramway/button_component.rb', line 53

def classes
  (default_button_classes +
    size_classes.split +
    color_classes +
    (@tag == :a ? %w[px-1 h-fit w-fit] : [cursor_class]) +
    options[:class].to_s.split).compact.join(' ')
end

#color_classesObject



61
62
63
# File 'app/components/tramway/button_component.rb', line 61

def color_classes
  theme_classes classic: color_classes_collection
end

#color_classes_collectionObject



65
66
67
68
69
70
71
72
73
74
75
76
# File 'app/components/tramway/button_component.rb', line 65

def color_classes_collection
  return %w[bg-gray-800 text-gray-500 shadow-inner] if disabled?

  case normalized_type
  when :default, :life, :secondary
    ['hover:bg-zinc-250', 'bg-zinc-50', 'text-zinc-950']
  when :inverse
    ['hover:bg-zinc-800', 'bg-zinc-950', 'text-zinc-50', 'border', 'border-zinc-800']
  else
    ["hover:bg-#{resolved_color}-900 bg-#{resolved_color}-900/30 text-#{resolved_color}-400"]
  end
end

#default_button_classesObject



49
50
51
# File 'app/components/tramway/button_component.rb', line 49

def default_button_classes
  DEFAULT_BUTTON_CLASSES
end

#disabled?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/components/tramway/button_component.rb', line 78

def disabled?
  options[:disabled] || false
end

#render_optionsObject



82
83
84
85
86
87
# File 'app/components/tramway/button_component.rb', line 82

def render_options
  base_options = options.except(:class)
  return base_options unless stop_cell_propagation?

  base_options.merge(onclick: merged_onclick(base_options[:onclick]))
end

#size_classesObject



37
38
39
40
41
42
43
44
45
46
47
# File 'app/components/tramway/button_component.rb', line 37

def size_classes
  unless normalized_size.in?(%i[small medium large])
    raise ArgumentError, "Invalid size: #{size}. Valid sizes are :small, :medium, :large."
  end

  {
    small: 'text-sm py-1 px-2 rounded',
    medium: 'text-sm py-2 px-4 h-10',
    large: 'text-xl px-5 py-3 h-12'
  }[normalized_size]
end