Sha256: 793fbfd4ef3f266cd539d47113536f2c918372eac6d470178984253efcc2d5bc

Contents?: true

Size: 1.75 KB

Versions: 4

Compression:

Stored size: 1.75 KB

Contents

# frozen_string_literal: true

require 'view_component'

class Proscenium::ViewComponent < ViewComponent::Base
  extend ActiveSupport::Autoload
  include Proscenium::CssModule

  autoload :TagBuilder
  autoload :ReactComponent

  # Side loads the class, and its super classes that respond to `.path`. Assign the `abstract_class`
  # class variable to any abstract class, and it will not be side loaded. Additionally, if the class
  # responds to `side_load`, then that method is called.
  module Sideload
    def before_render
      klass = self.class

      if !klass.abstract_class && respond_to?(:side_load, true)
        side_load
        klass = klass.superclass
      end

      while !klass.abstract_class && klass.respond_to?(:path) && klass.path
        Proscenium::SideLoad.append klass.path
        klass = klass.superclass
      end

      super
    end
  end

  class << self
    attr_accessor :path, :abstract_class

    def inherited(child)
      child.path = if caller_locations(1, 1).first.label == 'inherited'
                     Pathname.new caller_locations(2, 1).first.path
                   else
                     Pathname.new caller_locations(1, 1).first.path
                   end

      child.prepend Sideload if Rails.application.config.proscenium.side_load

      super
    end
  end

  # @override Auto compilation of class names to css modules.
  def render_in(...)
    cssm.compile_class_names(super(...))
  end

  private

  # Overrides ActionView::Helpers::TagHelper::TagBuilder, allowing us to intercept the
  # `css_module` option from the HTML options argument of the `tag` and `content_tag` helpers, and
  # prepend it to the HTML `class` attribute.
  def tag_builder
    @tag_builder ||= Proscenium::ViewComponent::TagBuilder.new(self)
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
proscenium-0.10.0-x86_64-linux lib/proscenium/view_component.rb
proscenium-0.10.0-aarch64-linux lib/proscenium/view_component.rb
proscenium-0.10.0-arm64-darwin lib/proscenium/view_component.rb
proscenium-0.10.0-x86_64-darwin lib/proscenium/view_component.rb