Class: IronAdmin::Ui::ButtonComponent
- Inherits:
-
ViewComponent::Base
- Object
- ViewComponent::Base
- IronAdmin::Ui::ButtonComponent
- Defined in:
- app/components/iron_admin/ui/button_component.rb
Overview
Renders a styled button or link styled as a button.
Constant Summary collapse
- VARIANTS =
Variant style mappings (uses theme configuration).
{ primary: -> { IronAdmin.configuration.theme.btn_primary }, secondary: -> { IronAdmin.configuration.theme.btn_secondary }, danger: -> { IronAdmin.configuration.theme.btn_danger }, ghost: -> { IronAdmin.configuration.theme.btn_ghost }, }.freeze
- SIZES =
Size class mappings.
{ sm: "px-3 py-1.5 text-sm", md: "px-4 py-2 text-sm", lg: "px-5 py-2.5 text-base", }.freeze
Instance Attribute Summary collapse
-
#confirm ⇒ String?
readonly
Confirmation message.
-
#disabled ⇒ Boolean
readonly
Whether button is disabled.
-
#href ⇒ String?
readonly
URL (renders as link if present).
-
#icon ⇒ String?
readonly
Heroicon name.
-
#method ⇒ Symbol?
readonly
HTTP method for Turbo.
-
#size ⇒ Symbol
readonly
Size variant (:sm, :md, :lg).
-
#text ⇒ String?
readonly
Button text.
-
#type ⇒ Symbol
readonly
Button type (:button, :submit).
-
#variant ⇒ Symbol
readonly
Style variant (:primary, :secondary, :danger, :ghost).
Instance Method Summary collapse
-
#base_classes ⇒ String
private
Base CSS classes for all button variants.
-
#button_classes ⇒ String
private
Combined CSS classes for button.
-
#call ⇒ String
Renders the button or link.
-
#data_attributes ⇒ Hash
private
Data attributes for Turbo.
-
#initialize(text: nil, variant: :primary, size: :md, icon: nil, href: nil, method: nil, confirm: nil, disabled: false, type: :button) ⇒ ButtonComponent
constructor
A new instance of ButtonComponent.
-
#size_classes ⇒ String
private
CSS classes for button size.
-
#variant_classes ⇒ String
private
CSS classes for button variant.
Constructor Details
#initialize(text: nil, variant: :primary, size: :md, icon: nil, href: nil, method: nil, confirm: nil, disabled: false, type: :button) ⇒ ButtonComponent
Returns a new instance of ButtonComponent.
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'app/components/iron_admin/ui/button_component.rb', line 77 def initialize(text: nil, variant: :primary, size: :md, icon: nil, href: nil, method: nil, confirm: nil, disabled: false, type: :button) @text = text @variant = variant.to_sym @size = size.to_sym @icon = icon @href = href @method = method @confirm = confirm @disabled = disabled @type = type end |
Instance Attribute Details
#confirm ⇒ String? (readonly)
Returns Confirmation message.
43 44 45 |
# File 'app/components/iron_admin/ui/button_component.rb', line 43 def confirm @confirm end |
#disabled ⇒ Boolean (readonly)
Returns Whether button is disabled.
46 47 48 |
# File 'app/components/iron_admin/ui/button_component.rb', line 46 def disabled @disabled end |
#href ⇒ String? (readonly)
Returns URL (renders as link if present).
37 38 39 |
# File 'app/components/iron_admin/ui/button_component.rb', line 37 def href @href end |
#icon ⇒ String? (readonly)
Returns Heroicon name.
34 35 36 |
# File 'app/components/iron_admin/ui/button_component.rb', line 34 def icon @icon end |
#method ⇒ Symbol? (readonly)
Returns HTTP method for Turbo.
40 41 42 |
# File 'app/components/iron_admin/ui/button_component.rb', line 40 def method @method end |
#size ⇒ Symbol (readonly)
Returns Size variant (:sm, :md, :lg).
31 32 33 |
# File 'app/components/iron_admin/ui/button_component.rb', line 31 def size @size end |
#text ⇒ String? (readonly)
Returns Button text.
25 26 27 |
# File 'app/components/iron_admin/ui/button_component.rb', line 25 def text @text end |
#type ⇒ Symbol (readonly)
Returns Button type (:button, :submit).
49 50 51 |
# File 'app/components/iron_admin/ui/button_component.rb', line 49 def type @type end |
#variant ⇒ Symbol (readonly)
Returns Style variant (:primary, :secondary, :danger, :ghost).
28 29 30 |
# File 'app/components/iron_admin/ui/button_component.rb', line 28 def variant @variant end |
Instance Method Details
#base_classes ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Base CSS classes for all button variants.
105 106 107 108 |
# File 'app/components/iron_admin/ui/button_component.rb', line 105 def base_classes "inline-flex items-center justify-center gap-2 font-medium rounded-lg transition-colors duration-150 " \ "focus:outline-none focus:ring-2 focus:ring-offset-1 disabled:opacity-50 disabled:cursor-not-allowed" end |
#button_classes ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Combined CSS classes for button.
112 113 114 |
# File 'app/components/iron_admin/ui/button_component.rb', line 112 def "#{base_classes} #{variant_classes} #{size_classes}" end |
#call ⇒ String
Renders the button or link.
127 128 129 130 131 132 133 134 135 136 137 |
# File 'app/components/iron_admin/ui/button_component.rb', line 127 def call if href link_to(href, class: , data: data_attributes) do end else tag.(type: type, class: , disabled: disabled, data: data_attributes) do end end end |
#data_attributes ⇒ Hash
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns Data attributes for Turbo.
118 119 120 121 122 123 |
# File 'app/components/iron_admin/ui/button_component.rb', line 118 def data_attributes data = {} data[:turbo_method] = method if method data[:turbo_confirm] = confirm if confirm data end |
#size_classes ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns CSS classes for button size.
99 100 101 |
# File 'app/components/iron_admin/ui/button_component.rb', line 99 def size_classes SIZES[@size] || SIZES[:md] end |
#variant_classes ⇒ String
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns CSS classes for button variant.
92 93 94 95 |
# File 'app/components/iron_admin/ui/button_component.rb', line 92 def variant_classes variant_proc = VARIANTS[@variant] || VARIANTS[:primary] variant_proc.call end |