Sha256: 483c9c88d0827ca5a0af4db9c04a3a0a02e70bb561e6d4ab12d4d9718054bdd1

Contents?: true

Size: 799 Bytes

Versions: 5

Compression:

Stored size: 799 Bytes

Contents

module Lookbook
  class TagComponent < ViewComponent::Base
    include Lookbook::ComponentHelper

    def initialize(tag: :div, name: nil, cloak: false, **html_attrs)
      @tag = tag
      html_attrs[:class] = class_names(html_attrs[:class]) if html_attrs[:class]
      html_attrs[:data] ||= {}
      html_attrs[:data][:component] = name if name.present?
      html_attrs[:x_cloak] = true if cloak == true
      @html_attrs = convert_attrs(html_attrs)
    end

    def call
      @html_attrs[Rails.version.to_f < 7.0 ? :escape_attributes : :escape] = false
      tag.public_send(@tag, **@html_attrs) do
        content
      end
    end

    protected

    def convert_attrs(html_attrs)
      html_attrs.map do |name, value|
        [name.to_s.tr("_", "-"), value]
      end.to_h
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
lookbook-1.0.0.beta.4 app/components/lookbook/tag_component.rb
lookbook-1.0.0.beta.3 app/components/lookbook/tag_component.rb
lookbook-1.0.0.beta.2 app/components/lookbook/tag_component.rb
lookbook-1.0.0.beta.1 app/components/lookbook/tag_component.rb
lookbook-1.0.0.beta.0 app/components/lookbook/tag_component.rb