-
Notifications
You must be signed in to change notification settings - Fork 429
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
Allow slots to be rendered by default #614
Comments
Does anyone else have any other strategies for this? I suppose we could create an abstraction that does the same thing that @manuelpuyol, but this seems like a common enough use-case that we could have some documentation surrounding it in the Slots docs. |
I think it's also possible to roll with |
@manuelpuyol what do you think of @boardfish's idea? |
I think @boardfish's idea is a good workaround, but I still feel like this should be native to slots to avoid duplication. Using |
Would something like that solve the issue? class MyComponent < ViewComponent::Base
renders_one :slot, "RequiredSlot"
renders_one :other_slot, "NotRequiredSlot"
def slot
super || render(RequiredSlot.new)
end
class RequiredSlot < ViewComponent::Base
end
class NotRequiredSlot < ViewComponent::Base
end
end |
@Spone just tried this with the latest version of ViewComponent |
@cj sorry I missed your reply. Which line causes the |
@Spone I received that same issue with 2.82.2
It was caused by the super call in the example above def slot
super || render(RequiredSlot.new)
end I was able to resolve the exception locally by removing the def slot
render(RequiredSlot.new)
end This resulted in the expected behavior of the component rendering inline. |
Hi! We are also running into this issue quite often. The fix suggested by @dirkkelly did not work for me since it wasn't possible to overwrite the required slot's defaults anymore. A good workaround though is calling def slot
get_slot(:slot) || render(RequiredSlot.new)
end but a better approach is the current workaround from @manuelpuyol but with using the def before_render
with_slot unless slot?
end From my point of view it would be very helpful if this could be a default behaviour on the |
Fixes ViewComponent#614 Add `required` option to `renders_one` to indicate that there is a default slot to be rendered, which automatically applies lambda options. This only works with lambda or Component slots (and not for passthrough slots). It can be used with polymorphic slots but exactly one specific option can be required, otherwise an error is shown.
* Add support for default slot values Closes #1861 #614 * Fix final line endings * add test case for overriding slot default value * lints * look up slot default method at compile time * fix indent * lints --------- Co-authored-by: GitHub Actions Bot <41898282+github-actions[bot]@users.noreply.github.com>
Closed by #2063 |
Feature request
It'd be helpful to allow some slots to be rendered even when they aren't explicitly called in the template.
e.g:
rb:
erb:
call:
This should render
slot
but notother_slot
Motivation
Some components always have to render a slot, so the burden goes to the user to make sure to always call the required slot in the call block. Since most of the times they will use the default arguments for the slot, the explicit call could be removed.
Current workaround
We can use
before_render
to reach this behavior:The text was updated successfully, but these errors were encountered: