Skip to content

Commit

Permalink
Add configuration to treat content as a slot
Browse files Browse the repository at this point in the history
Like discussed many times before, this finally adds content as a slot to
ViewComponent so that we can begin separating the block provided when
rendering from content for performance and ergonomic reasons.

This adds two new methods:

* `content_is_a_slot!` - This makes the component _and its descendants_
  treat `content` as a slot, and the block provided to render calls _is
  always executed_.
* `do_not_use_content_as_a_slot!` - This keeps the existing behavior
  where `content` is lazily evaluated and is used to return content.

There's likely more implications from this change that need to be walked
through (like how slots are used, and easily setting content) before
this can be merged.
  • Loading branch information
BlakeWilliams committed Sep 29, 2024
1 parent 451543a commit 5abc33b
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 7 deletions.
24 changes: 24 additions & 0 deletions lib/view_component/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,13 @@ def render_in(view_context, &block)
@__vc_content_evaluated = false
@__vc_render_in_block = block

if self.class.send(:__vc_content_is_a_slot?)
@__vc_content_evaluated = true
if __vc_render_in_block_provided?
view_context.capture(self, &@__vc_render_in_block)
end
end

before_render

if render?
Expand Down Expand Up @@ -549,6 +556,10 @@ def render_template_for(variant = nil, format = nil)
child.instance_variable_set(:@__vc_ancestor_calls, vc_ancestor_calls)
end

if defined?(@__vc_content_is_a_slot)
child.instance_variable_set(:@__vc_content_is_a_slot, @__vc_content_is_a_slot)
end

super
end

Expand Down Expand Up @@ -687,6 +698,19 @@ def initialize_parameters
def provided_collection_parameter
@provided_collection_parameter ||= nil
end

def __vc_content_is_a_slot?
defined?(@__vc_content_is_a_slot) && @__vc_content_is_a_slot
end

def content_is_a_slot!
@__vc_content_is_a_slot = true
renders_one :content
end

def do_not_use_content_as_a_slot!
@__vc_content_is_a_slot = false
end
end

ActiveSupport.run_load_hooks(:view_component, self)
Expand Down
16 changes: 9 additions & 7 deletions lib/view_component/slotable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -298,8 +298,10 @@ def define_slot(slot_name, collection:, callable:)
end

def validate_plural_slot_name(slot_name)
if RESERVED_NAMES[:plural].include?(slot_name.to_sym)
raise ReservedPluralSlotNameError.new(name, slot_name)
if slot_name.to_sym == :contents && !__vc_content_is_a_slot?
if RESERVED_NAMES[:plural].include?(slot_name.to_sym)
raise ReservedPluralSlotNameError.new(name, slot_name)
end
end

raise_if_slot_name_uncountable(slot_name)
Expand All @@ -309,12 +311,12 @@ def validate_plural_slot_name(slot_name)
end

def validate_singular_slot_name(slot_name)
if slot_name.to_sym == :content
if slot_name.to_sym == :content && !__vc_content_is_a_slot?
raise ContentSlotNameError.new(name)
end

if RESERVED_NAMES[:singular].include?(slot_name.to_sym)
raise ReservedSingularSlotNameError.new(name, slot_name)
elsif !__vc_content_is_a_slot?
if RESERVED_NAMES[:singular].include?(slot_name.to_sym)
raise ReservedSingularSlotNameError.new(name, slot_name)
end
end

raise_if_slot_conflicts_with_call(slot_name)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<div>
<% if content? %>
<%= content %>
<% end %>
</div>
3 changes: 3 additions & 0 deletions test/sandbox/app/components/content_as_slot_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ContentAsSlotComponent < ViewComponent::Base
content_is_a_slot!
end
3 changes: 3 additions & 0 deletions test/sandbox/app/components/content_not_a_slot_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class ContentNotASlotComponent < ViewComponent::Base
do_not_use_content_as_a_slot!
end
58 changes: 58 additions & 0 deletions test/sandbox/test/slotable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -819,4 +819,62 @@ def test_overridden_slot_name_can_be_inherited
def test_slot_name_methods_are_not_shared_accross_components
assert_not_equal SlotsComponent.instance_method(:title).owner, SlotNameOverrideComponent::OtherComponent.instance_method(:title).owner
end

def test_content_as_a_slot_component
component = ContentAsSlotComponent.new
render_inline(component) do |c|
c.with_content do
"The truth is out there"
end
end

assert component.content?
assert_selector "div", text: "The truth is out there"
end

def test_content_as_a_slot_component_with_content
component = ContentAsSlotComponent.new
component.with_content do
"The truth is out there"
end
render_inline(component)

assert component.content?
assert_selector "div", text: "The truth is out there"
end

def test_content_as_a_slot_inheritance
new_component_class = Class.new(ContentAsSlotComponent)
assert new_component_class.send(:__vc_content_is_a_slot?)
end

def test_content_is_not_a_slot
new_component_class = Class.new(SlotsComponent) do
do_not_use_content_as_a_slot!
end
refute new_component_class.send(:__vc_content_is_a_slot?)

render_inline(SlotsComponent.new) do |component|
component.with_title do
"This is my title!"
end

component.with_subtitle do
"This is my subtitle!"
end

component.with_footer do
"This is the footer"
end
end

assert_text "No tabs provided"
assert_text "No items provided"
end

def test_content_is_not_a_slot_inheritance
refute ContentNotASlotComponent.send(:__vc_content_is_a_slot?)
new_component_class = Class.new(ContentNotASlotComponent)
refute new_component_class.send(:__vc_content_is_a_slot?)
end
end

0 comments on commit 5abc33b

Please sign in to comment.