Class: BetterUi::General::Link::Component

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/better_ui/general/link/component.rb

Constant Summary collapse

"transition-colors duration-200 no-underline"
{
  default: "text-white hover:text-gray-300",        # Bianco per sfondi scuri
  white: "text-gray-900 hover:text-gray-700",      # Nero per sfondi chiari
  red: "text-red-500 hover:text-red-600",
  rose: "text-rose-500 hover:text-rose-600",
  orange: "text-orange-500 hover:text-orange-600",
  green: "text-green-500 hover:text-green-600",
  blue: "text-blue-500 hover:text-blue-600",
  yellow: "text-yellow-600 hover:text-yellow-700",
  violet: "text-violet-500 hover:text-violet-600"
}
{
  horizontal: "inline-flex items-center",
  vertical: "flex flex-col items-center"
}
{
  default: "",
  underline: "underline",
  bold: "font-bold",
  text: "no-underline"
}
{
  small: "text-sm",
  medium: "text-base",
  large: "text-lg"
}
{
  normal: "",
  active: "font-semibold",
  disabled: "opacity-50 cursor-not-allowed pointer-events-none"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label:, href: nil, theme: :white, orientation: :horizontal, style: :default, size: :medium, icon: nil, active: false, disabled: false, data: {}, method: nil, target: nil, **html_options) ⇒ Component

Returns a new instance of Component.

Parameters:

  • label (String)

    testo del link

  • href (String) (defaults to: nil)

    URL di destinazione (nil per semplice testo)

  • theme (Symbol) (defaults to: :white)

    tema del colore (:default, :white, etc.)

  • orientation (Symbol) (defaults to: :horizontal)

    orientamento (:horizontal, :vertical)

  • style (Symbol) (defaults to: :default)

    stile (:default, :underline, :bold, :text)

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

    dimensione (:small, :medium, :large)

  • icon (String) (defaults to: nil)

    icona opzionale

  • active (Boolean) (defaults to: false)

    stato attivo del link

  • disabled (Boolean) (defaults to: false)

    stato disabilitato del link

  • data (Hash) (defaults to: {})

    attributi data

  • method (Symbol) (defaults to: nil)

    metodo HTTP (per Turbo)

  • target (String) (defaults to: nil)

    target del link

  • html_options (Hash)

    opzioni HTML aggiuntive



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/components/better_ui/general/link/component.rb', line 64

def initialize(
  label:, 
  href: nil,
  theme: :white,
  orientation: :horizontal,
  style: :default,
  size: :medium,
  icon: nil,
  active: false,
  disabled: false,
  data: {},
  method: nil,
  target: nil,
  **html_options
)
  @label = label
  @href = href
  @theme = theme.to_sym
  @orientation = orientation.to_sym
  @style = style.to_sym
  @size = size.to_sym
  @icon = icon
  @active = active
  @disabled = disabled
  @data = data || {}
  @method = method
  @target = target
  @html_options = html_options
  
  validate_params
end

Instance Attribute Details

#activeObject (readonly)

Returns the value of attribute active.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def active
  @active
end

#dataObject (readonly)

Returns the value of attribute data.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def data
  @data
end

#disabledObject (readonly)

Returns the value of attribute disabled.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def disabled
  @disabled
end

#hrefObject (readonly)

Returns the value of attribute href.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def href
  @href
end

#iconObject (readonly)

Returns the value of attribute icon.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def icon
  @icon
end

#labelObject (readonly)

Returns the value of attribute label.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def label
  @label
end

#methodObject (readonly)

Returns the value of attribute method.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def method
  @method
end

#orientationObject (readonly)

Returns the value of attribute orientation.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def orientation
  @orientation
end

#sizeObject (readonly)

Returns the value of attribute size.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def size
  @size
end

#styleObject (readonly)

Returns the value of attribute style.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def style
  @style
end

#targetObject (readonly)

Returns the value of attribute target.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def target
  @target
end

#themeObject (readonly)

Returns the value of attribute theme.



5
6
7
# File 'app/components/better_ui/general/link/component.rb', line 5

def theme
  @theme
end

Instance Method Details

#active?Boolean

Determina se è un link attivo/corrente

Returns:

  • (Boolean)


97
98
99
# File 'app/components/better_ui/general/link/component.rb', line 97

def active?
  @active
end

#combined_classesObject

Combina tutte le classi CSS



112
113
114
115
116
117
118
119
120
121
122
# File 'app/components/better_ui/general/link/component.rb', line 112

def combined_classes
  [
    LINK_BASE_CLASSES,
    get_theme_class,
    get_orientation_class,
    get_style_class,
    get_size_class,
    get_state_class,
    @html_options[:class]
  ].compact.join(" ")
end

#disabled?Boolean

Determina se è disabilitato

Returns:

  • (Boolean)


102
103
104
# File 'app/components/better_ui/general/link/component.rb', line 102

def disabled?
  @disabled
end

#element_attributesObject

Restituisce gli attributi per il link/span



158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/components/better_ui/general/link/component.rb', line 158

def element_attributes
  attrs = @html_options.except(:class)
  attrs[:class] = combined_classes
  
  if link?
    # Attributi specifici per i link
    if @method.present?
      attrs[:data] = @data.merge(turbo_method: @method) 
    elsif @data.present?
      attrs[:data] = @data
    end
    
    attrs[:target] = @target if @target.present?
    attrs[:aria] ||= {}
    attrs[:aria][:current] = 'page' if active?
  else
    # Attributi per span (testo semplice o disabilitato)
    attrs[:aria] ||= {}
    attrs[:aria][:disabled] = true if disabled?
  end
  
  attrs
end

#icon_classesObject

Classi per l’icona con dimensionamento proporzionale



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'app/components/better_ui/general/link/component.rb', line 125

def icon_classes
  return "" unless @icon.present?
  
  # Definisce spacing e dimensioni icona basate su size
  base_spacing = case @orientation
  when :horizontal
    "mr-2"
  when :vertical
    "mb-1"
  else
    "mr-2"
  end
  
  icon_size = case @size
  when :small
    "w-4 h-4"
  when :medium
    "w-5 h-5"
  when :large
    "w-6 h-6"
  else
    "w-5 h-5"
  end
  
  "#{base_spacing} #{icon_size} inline-block"
end

#link?Boolean

Determina se è un link o solo testo

Returns:

  • (Boolean)


107
108
109
# File 'app/components/better_ui/general/link/component.rb', line 107

def link?
  @href.present? && !@disabled
end

#render_iconObject

Renderizza l’icona



188
189
190
191
192
193
194
195
196
# File 'app/components/better_ui/general/link/component.rb', line 188

def render_icon
  return nil unless show_icon?
  
  if @icon.is_a?(String)
    render BetterUi::General::IconComponent.new(name: @icon)
  else
    @icon # Assumiamo che sia già un componente renderizzato
  end
end

#show_icon?Boolean

Determina se mostrare l’icona

Returns:

  • (Boolean)


183
184
185
# File 'app/components/better_ui/general/link/component.rb', line 183

def show_icon?
  @icon.present?
end

#text_classesObject

Classi per il testo



153
154
155
# File 'app/components/better_ui/general/link/component.rb', line 153

def text_classes
  "inline-block"
end