Class: Vident::RootComponent::UsingViewComponent

Inherits:
ViewComponent::Base
  • Object
show all
Includes:
Base
Defined in:
lib/vident/root_component/using_view_component.rb

Constant Summary collapse

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

Instance Method Summary collapse

Methods included from Base

#action, #action_data_attribute, #as_targets, #initialize, #named_classes, #target, #target_data_attribute, #with_actions, #with_controllers

Instance Method Details

#callObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/vident/root_component/using_view_component.rb', line 21

def call
  # Capture inner block content
  # It's important that we capture the block content before generating the tag options.
  # This is because the content could contain calls to `s.data_map`.
  # These calls add key-value pairs to the internal data_map, which can then be translated into
  # the correct `data-*` attrs by the tag options.
  generated = content

  # Generate outer tag options and render
  tag_type = @element_tag.presence || :div
  options = @html_options&.dup || {}
  data_attrs = tag_data_attributes
  options[:data] = options[:data].present? ? data_attrs.merge(options[:data]) : data_attrs
  options = options.merge(id: @id) if @id
  if SELF_CLOSING_TAGS.include?(tag_type)
    view_context.tag(tag_type, options)
  else
    view_context.(tag_type, generated, options)
  end
end

#target_tag(tag_name, targets, **options, &block) ⇒ Object



13
14
15
16
17
18
19
# File 'lib/vident/root_component/using_view_component.rb', line 13

def target_tag(tag_name, targets, **options, &block)
  parsed = parse_targets(Array.wrap(targets))
  options[:data] ||= {}
  options[:data].merge!(build_target_data_attributes(parsed))
  content = view_context.capture(&block) if block
  view_context.(tag_name, content, options)
end