Class: Avo::ButtonComponent

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

Overview

A button/link can have the following settings: style: primary/outline/text/icon size: :xs :sm, :md, :lg, :xl color: :gray, :red, :green, :blue, or any other tailwind color icon: “heroicons/outline/paperclip” as specified in the docs (docs.avohq.io/3.0/icons.html)

Constant Summary

Constants included from Concerns::FindAssociationField

Concerns::FindAssociationField::ASSOCIATIONS

Instance Method Summary collapse

Methods inherited from BaseComponent

#component_name, #has_with_trial

Methods included from Concerns::FindAssociationField

#find_association_field

Instance Method Details

#argsObject



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

def args
  if @args[:loading]
    @args[:"data-controller"] = "loading-button"
    @args[:"data-action"] = "click->loading-button#attemptSubmit"
  end

  @args[:class] = button_classes
  @args[:aria] = @aria

  @args
end

#button_classesObject



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

def button_classes
  classes = "button-component inline-flex flex-grow-0 items-center font-semibold leading-6 fill-current whitespace-nowrap transition duration-100 transform transition duration-100 cursor-pointer disabled:cursor-not-allowed disabled:opacity-60 data-[disabled='true']:opacity-60 justify-center #{@class}"

  # For non-icon-styled buttons we should not add borders.
  classes += " border active:outline active:outline-1" unless is_icon?

  classes += " rounded" if @rounded.present?
  classes += style_classes
  classes += horizontal_padding_classes
  classes += vertical_padding_classes
  classes += text_size_classes

  classes
end

#callObject



79
80
81
82
83
84
85
# File 'app/components/avo/button_component.rb', line 79

def call
  if is_link?
    output_link
  else
    output_button
  end
end

#full_contentObject



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

def full_content
  result = ""
  # space out the icon from the text if text is present
  # add the icon height
  icon_classes = class_names(@icon_class, "pointer-events-none", icon_size_classes, "mr-1": content.present? && is_not_icon?)

  # Add the icon
  result += helpers.svg(@icon, class: icon_classes) if @icon.present?

  if is_not_icon? && content.present?
    result += content
  end

  result.html_safe
end

#is_icon?Boolean

Returns:

  • (Boolean)


55
56
57
# File 'app/components/avo/button_component.rb', line 55

def is_icon?
  @style == :icon
end

#is_link?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'app/components/avo/button_component.rb', line 51

def is_link?
  @is_link
end

#is_not_icon?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'app/components/avo/button_component.rb', line 59

def is_not_icon?
  !is_icon?
end

#output_buttonObject



93
94
95
96
97
98
99
100
101
102
103
# File 'app/components/avo/button_component.rb', line 93

def output_button
  if args.dig(:method).present? || args.dig(:data, :turbo_method).present?
    button_to args[:url], **args do
      full_content
    end
  else
    button_tag(**args) do
      full_content
    end
  end
end


87
88
89
90
91
# File 'app/components/avo/button_component.rb', line 87

def output_link
  link_to @path, **args do
    full_content
  end
end