Class: Coco::Tag

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
AlpineHelper, TagHelper
Defined in:
app/components/coco/tag.rb

Constant Summary collapse

SELF_CLOSING_TAGS =
i[area base br col embed hr img input link meta param source track wbr].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from AlpineHelper

#x_attrs, #x_data

Methods included from TagHelper

#prefix_attr_keys, #random_id, #style_str

Constructor Details

#initialize(tag_name = :div, strip: false, root: false, **tag_attrs) ⇒ Tag

Returns a new instance of Tag.



10
11
12
13
14
15
# File 'app/components/coco/tag.rb', line 10

def initialize(tag_name = :div, strip: false, root: false, **tag_attrs)
  @tag_name = tag_name
  @strip = strip
  @tag_attrs = tag_attrs
  @root = root
end

Instance Attribute Details

#rootObject (readonly)

Returns the value of attribute root.



8
9
10
# File 'app/components/coco/tag.rb', line 8

def root
  @root
end

#stripObject (readonly)

Returns the value of attribute strip.



8
9
10
# File 'app/components/coco/tag.rb', line 8

def strip
  @strip
end

#tag_nameObject (readonly)

Returns the value of attribute tag_name.



8
9
10
# File 'app/components/coco/tag.rb', line 8

def tag_name
  @tag_name
end

Instance Method Details

#callObject



21
22
23
24
25
26
27
# File 'app/components/coco/tag.rb', line 21

def call
  if SELF_CLOSING_TAGS.include?(@tag_name)
    tag(tag_name, tag_attrs)
  else
    (tag_name, content, tag_attrs)
  end
end

#contentObject



17
18
19
# File 'app/components/coco/tag.rb', line 17

def content
  strip ? super.strip_heredoc.strip : super
end

#tag_attrsObject



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/components/coco/tag.rb', line 29

def tag_attrs
  attrs = @tag_attrs.except(:x, :test_data)

  alpine_attrs = @tag_attrs.fetch(:x, {})
  if alpine_attrs.any?
    attrs.merge!(x_attrs(alpine_attrs, root: root))
  end

  if dev?
    test_attrs = prefix_attr_keys(@tag_attrs.fetch(:test_data, {}), "test")
    attrs.merge!(test_attrs)
  end

  attrs
end