Skip to content

Conditional parent #11486

Answered by brunnerh
bartcuijpers asked this question in Q&A
May 6, 2024 · 1 comments · 1 reply
Discussion options

You must be logged in to vote

In Svelte 5 you can use a snippet:

{#each data as { wrapper, content }}
	{#snippet contentSnippet()}
		<div class="content">{content}</div>
	{/snippet}
	{#if wrapper}
		<div class="wrapper">{@render contentSnippet()}</div>
	{:else}
		{@render contentSnippet()}
	{/if}
{/each}

REPL

In Svelte 4, you probably have to either duplicate the content or extract it to a separate component.

You can also implement a generic conditional wrapper component.

<ConditionalWrapper condition={wrapper}>
  <div class="content">{content}</div>
</ConditionalWrapper>
<!-- ConditionalWrapper.svelte -->
<script>
  export let condition;
  export let tagName = 'div';
</script>
{#if condition}
  <svelte:element this={t…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@bartcuijpers
Comment options

Answer selected by bartcuijpers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants