Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add configuration to treat content as a slot #2108

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading