Class: Panda::Core::UI::Button

Inherits:
Base
  • Object
show all
Defined in:
app/components/panda/core/UI/button.rb

Overview

Modern ViewComponent-based button component with type-safe props.

Supports both

Examples:

Basic button

render Panda::Core::UI::Button.new(text: "Click me")

Button as link

render Panda::Core::UI::Button.new(
  text: "Edit",
  variant: :secondary,
  href: "/admin/posts/1/edit"
)

Primary action button

render Panda::Core::UI::Button.new(
  text: "Publish",
  variant: :primary,
  href: "/admin/posts/1/publish"
)

With custom attributes

render Panda::Core::UI::Button.new(
  text: "Submit",
  variant: :primary,
  class: "mt-4",
  data: { turbo_method: :post }
)

Constant Summary

Constants inherited from Base

Base::TAILWIND_MERGER

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(text: "", variant: :default, size: :medium, disabled: false, type: "button", href: nil, **attrs) ⇒ Button

Returns a new instance of Button.



37
38
39
40
41
42
43
44
45
# File 'app/components/panda/core/UI/button.rb', line 37

def initialize(text: "", variant: :default, size: :medium, disabled: false, type: "button", href: nil, **attrs)
  @text = text
  @variant = variant
  @size = size
  @disabled = disabled
  @type = type
  @href = href
  super(**attrs)
end

Instance Attribute Details

#disabledObject (readonly)

Returns the value of attribute disabled.



47
48
49
# File 'app/components/panda/core/UI/button.rb', line 47

def disabled
  @disabled
end

#hrefObject (readonly)

Returns the value of attribute href.



47
48
49
# File 'app/components/panda/core/UI/button.rb', line 47

def href
  @href
end

#sizeObject (readonly)

Returns the value of attribute size.



47
48
49
# File 'app/components/panda/core/UI/button.rb', line 47

def size
  @size
end

#textObject (readonly)

Returns the value of attribute text.



47
48
49
# File 'app/components/panda/core/UI/button.rb', line 47

def text
  @text
end

#typeObject (readonly)

Returns the value of attribute type.



47
48
49
# File 'app/components/panda/core/UI/button.rb', line 47

def type
  @type
end

#variantObject (readonly)

Returns the value of attribute variant.



47
48
49
# File 'app/components/panda/core/UI/button.rb', line 47

def variant
  @variant
end

Instance Method Details

#default_attrsObject



49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/components/panda/core/UI/button.rb', line 49

def default_attrs
  base = {
    class: button_classes
  }

  if @href
    base[:href] = @href
  else
    base[:type] = @type
    base[:disabled] = @disabled if @disabled
  end

  base
end