Sha256: 53928fac36b0b2253662297fdda610d94e6895265cdef855572fa114b7f0c0c4

Contents?: true

Size: 1.9 KB

Versions: 4

Compression:

Stored size: 1.9 KB

Contents

# frozen_string_literal: true

module ViewComponent
  class SlotV2
    attr_writer :_component_instance, :_content_block, :_content

    def initialize(parent)
      @parent = parent
    end

    # Used to render the slot content in the template
    #
    # There's currently 3 different values that may be set, that we can render.
    #
    # If the slot renderable is a component, the string class name of a
    # component, or a function that returns a component, we render that
    # component instance, returning the string.
    #
    # If the slot renderable is a function and returns a string, it is
    # set as `@_content` and is returned directly.
    #
    # If there is no slot renderable, we evaluate the block passed to
    # the slot and return it.
    def to_s
      view_context = @parent.send(:view_context)
      view_context.capture do
        if defined?(@_component_instance)
          # render_in is faster than `parent.render`
          @_component_instance.render_in(view_context, &@_content_block)
        elsif defined?(@_content)
          @_content
        elsif defined?(@_content_block)
          @_content_block.call
        end
      end
    end

    # Allow access to public component methods via the wrapper
    #
    # e.g.
    #
    # calling `header.name` (where `header` is a slot) will call `name`
    # on the `HeaderComponent` instance.
    #
    # Where the component may look like:
    #
    # class MyComponent < ViewComponent::Base
    #   has_one :header, HeaderComponent
    #
    #   class HeaderComponent < ViewComponent::Base
    #     def name
    #       @name
    #     end
    #   end
    # end
    #
    def method_missing(symbol, *args, &block)
      @_component_instance.public_send(symbol, *args, &block)
    end

    def respond_to_missing?(symbol, include_all = false)
      defined?(@_component_instance) && @_component_instance.respond_to?(symbol, include_all)
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
view_component-2.25.0 lib/view_component/slot_v2.rb
view_component-2.24.0 lib/view_component/slot_v2.rb
view_component-2.23.2 lib/view_component/slot_v2.rb
view_component-2.23.1 lib/view_component/slot_v2.rb