Module: Lookbook::ComponentHelper

Included in:
BaseComponent, TagComponent
Defined in:
app/helpers/lookbook/component_helper.rb

Constant Summary collapse

COMPONENT_CLASSES =

cache for constantized references

{}

Instance Method Summary collapse

Instance Method Details

#build_tag_values(*args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/lookbook/component_helper.rb', line 39

def build_tag_values(*args)
  tag_values = []

  args.each do |tag_value|
    case tag_value
    when Hash
      tag_value.each do |key, val|
        tag_values << key.to_s if val && key.present?
      end
    when Array
      tag_values.concat build_tag_values(*tag_value)
    else
      tag_values << tag_value.to_s if tag_value.present?
    end
  end

  tag_values
end

#class_names(*args) ⇒ Object



33
34
35
36
37
# File 'app/helpers/lookbook/component_helper.rb', line 33

def class_names(*args)
  tokens = build_tag_values(*args).flat_map { |value| value.to_s.split(/\s+/) }.uniq

  safe_join(tokens, " ")
end

#code(language = :html, **attrs, &block) ⇒ Object



9
10
11
12
# File 'app/helpers/lookbook/component_helper.rb', line 9

def code(language = :html, **attrs, &block)
  attrs[:language] ||= language
  lookbook_render :code, **attrs, &block
end

#icon(name, **attrs) ⇒ Object



5
6
7
# File 'app/helpers/lookbook/component_helper.rb', line 5

def icon(name, **attrs)
  lookbook_render :icon, name: name, **attrs
end

#lookbook_render(ref, **attrs, &block) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'app/helpers/lookbook/component_helper.rb', line 18

def lookbook_render(ref, **attrs, &block)
  comp = if ref.is_a? ViewComponent::Base
    ref
  else
    klass = component_class(ref)
    attrs.key?(:content) ? klass.new(**attrs.except(:content)).with_content(attrs[:content]) : klass.new(**attrs)
  end
  if block && !attrs.key?(:content)
    public_send render_method_name, comp, &block
  else
    public_send render_method_name, comp
  end
end

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



14
15
16
# File 'app/helpers/lookbook/component_helper.rb', line 14

def lookbook_tag(tag = :div, **attrs, &block)
  lookbook_render :tag, tag: tag, **attrs, &block
end