Is there a way to access children without rendering #14018
-
I'd like to have a Select component that is used like so: <Select bind:value>
<SelectItem value="one">Some <b>arbitrary</b> markup</SelectItem>
<SelectItem value="two"><SomeComponent /></SelectItem>
</Select> Select's children get rendered in a dropdown only when it is expanded, which means it doesn't render by default. The way I wanted to do this is for SelectItem to send its I understand that snippets are just functions but is there a way to somehow access SelectItem's children from Select, before SelectItems get rendered? My current workaround looks like this, obviously not ideal: // Select.svelte
<script>
let rendered = $state(false);
$effect(() => {
rendered = true;
});
</script>
{#if !rendered}
<div class="hidden">
{@render children?.()}
</div>
{/if} |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 3 replies
-
The answer is "no". You could restructure your code, so there is no {#snippet option3()}
<strong>Strong</strong> option 3
{/snippet}
<Select options={[
{ value: 1 },
{ value: 2, label: 'Option 2' },
{ value: 3, template: option3 },
]} /> A very rough sketch of this approach. |
Beta Was this translation helpful? Give feedback.
-
Not sure if I understand correctly, you need something like this? In this example |
Beta Was this translation helpful? Give feedback.
-
Based on other questions asked before, this can be done through snippets defined inside the list and passed in as children like so, which doesn't need an extra component Playground: children snippets inside Edit: this is incorrect, context is different:
Based on @zyxd's solution but only using props and without using context api. Wrapping the Playground: Wrap |
Beta Was this translation helpful? Give feedback.
-
No, because documentation says:
Example: https://svelte.dev/playground/2dcb7a1dd9ea48219b9c2afed0cde3ed?version=5.1.9 |
Beta Was this translation helpful? Give feedback.
Not sure if I understand correctly, you need something like this?
https://svelte.dev/playground/b141c402c05e4ba581a7e177a946278e?version=5.1.4
In this example
<SelectItem>
does not have a template - it is implemented in<Select>
. If you need to implement a template in<SelectItem>
you can give it the ability to decide whether to show or not<button>
by passing theopen
property from the parent.