Class: Lookbook::BaseComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
ClassNamesHelper, UiElementsHelper
Defined in:
app/components/lookbook/base_component.rb

Instance Method Summary collapse

Methods included from ClassNamesHelper

#build_tag_values, #class_names

Methods included from UiElementsHelper

#code, #icon, #lookbook_render, #lookbook_tag, #prose

Constructor Details

#initialize(alpine_data: [], **html_attrs) ⇒ BaseComponent

Returns a new instance of BaseComponent.



7
8
9
10
11
# File 'app/components/lookbook/base_component.rb', line 7

def initialize(alpine_data: [], **html_attrs)
  @alpine_data ||= alpine_data
  @html_attrs = html_attrs
  @html_attrs[:class] = {"#{@html_attrs[:class]}": true} if @html_attrs[:class].is_a? String
end

Instance Method Details

#component_nameObject



23
24
25
# File 'app/components/lookbook/base_component.rb', line 23

def component_name
  self.class.name.chomp("::Component").delete_prefix("Lookbook::").underscore.tr("/", "_").tr("_", "-")
end

#render_component_tag(tag = :div, **attrs, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
# File 'app/components/lookbook/base_component.rb', line 13

def render_component_tag(tag = :div, **attrs, &block)
  merged_classes = class_names(attrs[:class], @html_attrs[:class])
  merged_attrs = @html_attrs.except(:class).deep_merge(attrs.except(:class))

  lookbook_tag tag, name: component_name,
    **merged_attrs,
    "x-data": prepare_alpine_data(merged_attrs[:"x-data"]),
    class: merged_classes, &block
end