Class: Vident::RootComponent::UsingPhlexHTML

Inherits:
Phlex::HTML
  • Object
show all
Includes:
Base, UsingBetterHTML
Defined in:
lib/vident/root_component/using_phlex_html.rb

Constant Summary collapse

VALID_TAGS =
Set[
  *(Phlex::HTML::VoidElements::REGISTERED_ELEMENTS.keys + Phlex::HTML::StandardElements::REGISTERED_ELEMENTS.keys)
].freeze

Instance Method Summary collapse

Methods included from UsingBetterHTML

#as_targets, #with_actions, #with_controllers

Methods included from Base

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

Instance Method Details

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

Create a tag for a target with a block containing content



19
20
21
22
23
24
# File 'lib/vident/root_component/using_phlex_html.rb', line 19

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

#template(&block) ⇒ Object

Build a tag with the attributes determined by this components properties and stimulus data attributes.

Raises:

  • (ArgumentError)


28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/vident/root_component/using_phlex_html.rb', line 28

def template(&block)
  # Generate tag options and render
  tag_type = @element_tag.presence&.to_sym || :div
  raise ArgumentError, "Unsupported HTML tag name #{tag_type}" unless VALID_TAGS.include?(tag_type)
  options = @html_options&.dup || {}
  data_attrs = tag_data_attributes
  data_attrs = options[:data].present? ? data_attrs.merge(options[:data]) : data_attrs
  options = options.merge(id: @id) if @id
  options.except!(:data)
  options.merge!(data_attrs.transform_keys { |k| "data-#{k}" })
  generate_tag(tag_type, **options, &block)
end