Skip to content

Svelte 5: Avoiding {#snippet} boilerplate with the ability to pass parameters #10678

Description

@katriellucas

Describe the problem

As of now, if we want to pass a Snippet as props to a component, it has to be without parameters, this is due to the Snippet props expecting a snippet function to be passed. if we want parameters on our Snippet function, it needs be nested inside the component as a child.

This process can get boring and boilerplatey very quickly, specially with more complex cases were different kinds of Snippets are used inside components.

Describe the proposed solution

Solution 1

It would be nice if Svelte let us pass parameters directly as props, using the example above, maybe:

// Option 1
<Chip label="Test" media={icon('blue')} />

or

// Option 2
<Chip label="Test" media={() => icon('blue')} />

or even

// Option 3
<Chip label="Test" media={(() => icon('blue'))()} />

Personally, Option 2 feels more natural as this is how we already pass functions on Svelte 5 using events such as onclick.

Solution 2

Some kind of new sintax for such cases might be interesting to think about.

<Chip label="Blue">
  {@render icon('blue') on media} // where Chip must have a "media" prop, show an error if doesn't
</Chip>

Solution 3

Do nothing. There is an argument on using {#each} loops but it feels somewhat overkill for 3 or 4 components.

Importance

would make my life easier

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions