Class: Tailwinds::ButtonComponent

Inherits:
BaseComponent show all
Defined in:
app/components/tailwinds/button_component.rb

Overview

Default Tramway button

Constant Summary

Constants inherited from BaseComponent

Tailwinds::BaseComponent::TYPE_COLOR_MAP

Constants included from Tramway::Helpers::ViewsHelper

Tramway::Helpers::ViewsHelper::FORM_SIZES

Instance Method Summary collapse

Methods inherited from BaseComponent

#normalized_type, #resolved_color, #type_color

Methods included from Tramway::Helpers::ViewsHelper

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

Methods included from Tramway::Helpers::ComponentHelper

#component

Methods included from Tramway::Helpers::DecorateHelper

#tramway_decorate

Instance Method Details

#before_renderObject



17
18
19
20
21
22
23
24
25
26
27
# File 'app/components/tailwinds/button_component.rb', line 17

def before_render
  return if tag.present?

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

#classesObject



41
42
43
44
45
# File 'app/components/tailwinds/button_component.rb', line 41

def classes
  (default_classes +
    color_classes +
    (@tag == :a ? %w[px-1 h-fit w-fit] : [cursor_class])).compact.join(' ')
end

#color_classesObject



56
57
58
59
60
61
62
63
64
65
66
# File 'app/components/tailwinds/button_component.rb', line 56

def color_classes
  if disabled?
    %w[bg-gray-800 text-gray-500 shadow-inner]
  else
    [
      "bg-#{resolved_color}-700", "hover:bg-#{resolved_color}-800", 'text-white'
    ]
  end => classes_collection

  theme_classes classic: classes_collection
end

#default_classesObject



47
48
49
50
51
52
53
54
# File 'app/components/tailwinds/button_component.rb', line 47

def default_classes
  base_classes = theme_classes(
    classic: %w[btn btn-primary flex flex-row font-semibold rounded-xl whitespace-nowrap items-center gap-1
                shadow-md]
  )

  base_classes + [size_classes.to_s, options[:class].to_s]
end

#disabled?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/components/tailwinds/button_component.rb', line 68

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

#render_optionsObject



72
73
74
75
76
77
# File 'app/components/tailwinds/button_component.rb', line 72

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



29
30
31
32
33
34
35
36
37
38
39
# File 'app/components/tailwinds/button_component.rb', line 29

def size_classes
  unless 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: 'py-2 px-4 h-10',
    large: 'text-xl px-5 py-3 h-12'
  }[size]
end