svelte icon indicating copy to clipboard operation
svelte copied to clipboard

Obtaining slots as function or SvelteComponent

Open Eudritch opened this issue 2 years ago • 1 comments

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

Eudritch avatar Nov 30 '22 19:11 Eudritch

This is basically https://github.com/sveltejs/svelte/issues/7082, correct?

Prinzhorn avatar Dec 01 '22 12:12 Prinzhorn

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.

divdavem avatar Apr 03 '23 15:04 divdavem

It looks like this issue should be solved in svelte 5 by the new snippets feature that is way better than slots. Thank you!

divdavem avatar Nov 13 '23 12:11 divdavem

Closing since since snippets in Svelte 5 will allow this kind of composition, as they can be passed around like regular properties/variables.

dummdidumm avatar Feb 08 '24 11:02 dummdidumm