svelte
svelte copied to clipboard
Obtaining slots as function or SvelteComponent
Describe the problem
I get frustrated when I have to use the slot block somewhere else on the page or try to dynamically create it. An overlay that runs every time the page loads, or a loading page that creates the slot elsewhere on the page in a layout, would be examples.
Describe the proposed solution
Instead of returning a Boolean, $$slots returns a function or a SvelteComponent, allowing you to pass a target argument from either return.
Alternatives considered
The ability to design unique svelte blocks would be useful for developing svelte libraries or project libraries.
Importance
i cannot use svelte without it
This is basically https://github.com/sveltejs/svelte/issues/7082, correct?
This is basically #7082, correct?
No, if the portal is implemented as <svelte:portal to={LOCATION}>CONTENT</svelte:portal>
, with LOCATION
being a DOM node, as it is suggested in #7082, I think getting a svelte component in $$slots
is a very different feature.
It is true that it is easy to do a portal component once we get a svelte component in $$slots
(by creating an instance with the constructor, passing the right target
). But calling the constructor of a svelte component is not the only way to use it.
If you can get a svelte component in $$slots
(instead of a boolean), you could for example put that component in the context (with setContext) and some component deeper in the hierarchy could use it directly inside its template wherever it wants (even multiple times), without having to deal with DOM nodes. This would not be (easily) possible with svelte:portal
.
I think it would be great to implement this feature, it would make slots much more powerful.
It looks like this issue should be solved in svelte 5 by the new snippets feature that is way better than slots. Thank you!
Closing since since snippets in Svelte 5 will allow this kind of composition, as they can be passed around like regular properties/variables.