Class: IronAdmin::Ui::ButtonComponent

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

Overview

Renders a styled button or link styled as a button.

Examples:

Primary button

render IronAdmin::Ui::ButtonComponent.new(text: "Save")

Danger button with confirmation

render IronAdmin::Ui::ButtonComponent.new(
  text: "Delete",
  variant: :danger,
  confirm: "Are you sure?"
)

Link styled as button

render IronAdmin::Ui::ButtonComponent.new(
  text: "View",
  href: user_path(@user),
  variant: :secondary
)

Constant Summary collapse

VARIANTS =

Variant style mappings (uses theme configuration).

Returns:

  • (Hash{Symbol => Proc})
{
  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.

Returns:

  • (Hash{Symbol => String})
{
  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

Instance Method Summary collapse

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.

Parameters:

  • text (String, nil) (defaults to: nil)

    Button text

  • variant (Symbol) (defaults to: :primary)

    Style variant (default: :primary)

  • size (Symbol) (defaults to: :md)

    Size variant (default: :md)

  • icon (String, nil) (defaults to: nil)

    Heroicon name

  • href (String, nil) (defaults to: nil)

    URL for link buttons

  • method (Symbol, nil) (defaults to: nil)

    HTTP method

  • confirm (String, nil) (defaults to: nil)

    Confirmation message

  • disabled (Boolean) (defaults to: false)

    Disabled state

  • type (Symbol) (defaults to: :button)

    Button type (default: :button)



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

#confirmString? (readonly)

Returns Confirmation message.

Returns:

  • (String, nil)

    Confirmation message



43
44
45
# File 'app/components/iron_admin/ui/button_component.rb', line 43

def confirm
  @confirm
end

#disabledBoolean (readonly)

Returns Whether button is disabled.

Returns:

  • (Boolean)

    Whether button is disabled



46
47
48
# File 'app/components/iron_admin/ui/button_component.rb', line 46

def disabled
  @disabled
end

#hrefString? (readonly)

Returns URL (renders as link if present).

Returns:

  • (String, nil)

    URL (renders as link if present)



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

def href
  @href
end

#iconString? (readonly)

Returns Heroicon name.

Returns:

  • (String, nil)

    Heroicon name



34
35
36
# File 'app/components/iron_admin/ui/button_component.rb', line 34

def icon
  @icon
end

#methodSymbol? (readonly)

Returns HTTP method for Turbo.

Returns:

  • (Symbol, nil)

    HTTP method for Turbo



40
41
42
# File 'app/components/iron_admin/ui/button_component.rb', line 40

def method
  @method
end

#sizeSymbol (readonly)

Returns Size variant (:sm, :md, :lg).

Returns:

  • (Symbol)

    Size variant (:sm, :md, :lg)



31
32
33
# File 'app/components/iron_admin/ui/button_component.rb', line 31

def size
  @size
end

#textString? (readonly)

Returns Button text.

Returns:

  • (String, nil)

    Button text



25
26
27
# File 'app/components/iron_admin/ui/button_component.rb', line 25

def text
  @text
end

#typeSymbol (readonly)

Returns Button type (:button, :submit).

Returns:

  • (Symbol)

    Button type (:button, :submit)



49
50
51
# File 'app/components/iron_admin/ui/button_component.rb', line 49

def type
  @type
end

#variantSymbol (readonly)

Returns Style variant (:primary, :secondary, :danger, :ghost).

Returns:

  • (Symbol)

    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_classesString

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.

Returns:

  • (String)

    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_classesString

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.

Returns:

  • (String)

    Combined CSS classes for button



112
113
114
# File 'app/components/iron_admin/ui/button_component.rb', line 112

def button_classes
  "#{base_classes} #{variant_classes} #{size_classes}"
end

#callString

Renders the button or link.

Returns:

  • (String)

    HTML content



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: button_classes, data: data_attributes) do
      button_content
    end
  else
    tag.button(type: type, class: button_classes, disabled: disabled, data: data_attributes) do
      button_content
    end
  end
end

#data_attributesHash

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.

Returns:

  • (Hash)

    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_classesString

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.

Returns:

  • (String)

    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_classesString

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.

Returns:

  • (String)

    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