Class: Keystone::Ui::ButtonComponent

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/keystone/ui/button_component.rb

Constant Summary collapse

BASE_CLASSES =
"inline-flex items-center justify-center font-semibold rounded-lg border-0 cursor-pointer no-underline"
VARIANT_CLASSES =
{
  secondary: "bg-gray-500 text-white hover:bg-gray-400",
  danger: "bg-red-600 text-white hover:bg-red-500"
}.freeze
SIZE_CLASSES =
{
  sm: "text-sm px-3 py-1.5",
  md: "text-base px-4 py-2",
  lg: "text-lg px-5 py-3"
}.freeze

Instance Method Summary collapse

Constructor Details

#initialize(label:, href: nil, variant: :primary, size: :md, type: :submit, data: nil) ⇒ ButtonComponent

Returns a new instance of ButtonComponent.



19
20
21
22
23
24
25
26
# File 'app/components/keystone/ui/button_component.rb', line 19

def initialize(label:, href: nil, variant: :primary, size: :md, type: :submit, data: nil)
  @label = label
  @href = href
  @variant = variant
  @size = size
  @type = type.to_s
  @data = data
end

Instance Method Details

#button?Boolean

Returns:

  • (Boolean)


52
53
54
# File 'app/components/keystone/ui/button_component.rb', line 52

def button?
  @href.nil?
end

#classesObject



28
29
30
31
32
33
34
35
# File 'app/components/keystone/ui/button_component.rb', line 28

def classes
  variant_css = if @variant == :primary
    "bg-accent-600 text-white hover:bg-accent-500"
  else
    VARIANT_CLASSES.fetch(@variant)
  end
  [ BASE_CLASSES, variant_css, SIZE_CLASSES.fetch(@size) ].join(" ")
end

#tag_nameObject



37
38
39
# File 'app/components/keystone/ui/button_component.rb', line 37

def tag_name
  button? ? :button : :a
end

#tag_optionsObject



41
42
43
44
45
46
47
48
49
50
# File 'app/components/keystone/ui/button_component.rb', line 41

def tag_options
  options = { class: classes }
  if button?
    options[:type] = @type
  else
    options[:href] = @href
  end
  options[:data] = @data if @data
  options
end