Sha256: 3767e6e033af5930882b0c7f1db3132e173d4a58ac3d27836a9257a84b838344

Contents?: true

Size: 1.29 KB

Versions: 3

Compression:

Stored size: 1.29 KB

Contents

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

    def initialize(alpine_data: [], **html_attrs)
      @alpine_data ||= alpine_data
      @html_attrs = html_attrs
      @html_attrs[:class] = {"#{@html_attrs[:class]}": true} if @html_attrs[:class].is_a? String
    end

    def render_component_tag(tag = :div, **attrs, &block)
      merged_classes = class_names(attrs[:class], @html_attrs[:class])
      merge_attrs = @html_attrs.except(:class).deep_merge(attrs.except(:class))
      render Lookbook::TagComponent.new(tag: tag,
        name: component_name,
        **merge_attrs,
        x_data: prepare_alpine_data(attrs[:x_data]),
        class: merged_classes), &block
    end

    def component_name
      self.class.name.chomp("::Component").delete_prefix("Lookbook::").underscore.tr("/", "_").tr("_", "-")
    end

    protected

    attr_reader :alpine_data

    def alpine_component
      nil
    end

    def prepare_alpine_data(x_data = nil)
      alpine_component_name = x_data || @html_attrs&.dig(:x_data) || alpine_component
      if alpine_component_name.present?
        args = Array.wrap(alpine_data)
        args.any? ? "#{alpine_component_name}(#{safe_join(args).gsub("&quot;", "'").tr("\"", "'")})" : alpine_component_name
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
lookbook-1.0.0.beta.2 app/components/lookbook/component.rb
lookbook-1.0.0.beta.1 app/components/lookbook/component.rb
lookbook-1.0.0.beta.0 app/components/lookbook/component.rb