Skip to content

Svelte 5: Support passing of snippets or components? #9774

Open
@brunnerh

Description

@brunnerh

Describe the problem

This came up in Discord:
It might be useful for library components to accept either a snippet or a regular component as input.

<!-- Snippet allows for adding additional content besides component/s -->
<List {items}>
	{#snippet item(data)}
		<ListItem {...data} />
	{/snippet}
</List>

<!-- Passing just a component -->
<List {items} item={ListItem} />

Describe the proposed solution

There would have to be a way to either:

  • Render a snippet or component regardless of what it is, e.g. make @render accept components.
    The first argument (in case Svelte 5: Variadic snippets #9672 is implemented) to the called function would then be considered the props in case of a component.
  • Add built-in functions to determine if something is a component/snippet. E.g.
    {#if isSnippet(item)}
      {@render item(args)}
    {:else if isComponent(item)}
      <svelte:component this={item} {...args} />
    {/if}

Alternatives considered

Always require the use of snippets, which can just use the desired component internally.
Potentially add a conversion utility from component to snippet:

<List {items} item={asSnippet(ListItem)} />

Importance

nice to have

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions