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 a {#render} block #13509

Open
adiguba opened this issue Oct 5, 2024 · 1 comment
Open

Add a {#render} block #13509

adiguba opened this issue Oct 5, 2024 · 1 comment

Comments

@adiguba
Copy link
Contributor

adiguba commented Oct 5, 2024

Describe the problem

Svelte 4 had a simple syntax to define the slot default code :

<slot>
    <!-- Default code if slot is missing -->
</slot>

Svelte 5 doesn't really have an equivalent for that, and this has to be done manually :

Either via an if/else :

{#if children}
    {@render children()}
{:else}
    <!-- Default code if children is missing -->
{:if}

Or via a second snippet :

{#snippet default_children()}
    <!-- Default code if children is missing -->
{/snippet}
{@render (children??default_children)()}

Describe the proposed solution

It would be nice to have a {#render} block that would mimic the <slot/> behavior :

{#render children()}
    <!-- Default code if children is missing -->
{/render}]

I think that this change may only impact the compiler, as the result code could simply use $.snippet() with a condition :

$.snippet(node, () => $$props.children ?? ($$anchor) => {
	$.next();
	var text_1 = $.text("DEFAULT CHILDREN");
	$.append($$anchor, text_1);
});

If the idea is accepted, I think I could try to implement it...

Importance

nice to have

@brunnerh
Copy link
Member

brunnerh commented Oct 5, 2024

I never found the slot syntax to be very intuitive.
Why would fallback content be the hierarchical child?
It looks like some content is being set but it's a replacement that might be there or not.

This might be more terse, but I am not sure it is actually better.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants