Skip to content

$$slots is empty object if using customElement: true #6165

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

Closed
imsamtar opened this issue Apr 4, 2021 · 10 comments
Closed

$$slots is empty object if using customElement: true #6165

imsamtar opened this issue Apr 4, 2021 · 10 comments

Comments

@imsamtar
Copy link

imsamtar commented Apr 4, 2021

When customElement option is set to true $$slots is empty object

here is my use case

<svelte:options tag="my-button" />

<script>
   onMount(() => {
      console.log($$slots);
   });
</script>

{#if $$slots.lefticon}
   <span>
	<slot name="lefticon" />
   </span>
{/if}

<span>
   <slot />
</span>

{#if $$slots.icon}
   <span>
	<slot name="icon" />
   </span>
{/if}
@jstrother
Copy link

How are you using it in the parent component? My understanding is that $$slots will only have anything in it IF the parent component passes any through.

@imsamtar
Copy link
Author

imsamtar commented Apr 7, 2021

@jstrother

I am using it like this

<my-button>
   <span slot="lefticon">L</span>
   Hello World!
   <span slot="icon">R</span>
</my-button>

@jstrother
Copy link

It looks to me like you're doing everything correctly. I remembered seeing something about custom elements causing issues and saw this issue #6164 that seems to be related. There are a few links there that could help out.

@imsamtar

This comment has been minimized.

@YamiOdymel
Copy link

Are these two (#6059 with this) the same issue? I'm currently facing the same issue and it's more like the $$slots variable is not reactive when customElement was set as true.

@imsamtar imsamtar reopened this Apr 23, 2021
@tanhauhau
Copy link
Member

$$slots is not supposed to be reactive.

as per the original issue title, this is a duplicate of #5594, and should have been fixed in v3.29.5

@YamiOdymel
Copy link

YamiOdymel commented Apr 24, 2021

image

Guess I was wrong, but the issue doesn't get fixed.

I'm using the same example code from the issue you mentioned above with "svelte": "^3.37.0"

<svelte:options tag="my-wc"/>

<script>
$: console.log($$slots);
</script>

<slot name="foo"></slot>
<my-wc>
    <span slot="foo">Foobar</span>
</my-wc>

@YamiOdymel
Copy link

Problem solved by adding defer to <script> tag, to get $$slots working it requires the DOM to be loaded first.

@tytusplanck-8451
Copy link

$$slots is not supposed to be reactive.

as per the original issue title, this is a duplicate of #5594, and should have been fixed in v3.29.5

@tanhauhau just out of curiosity, what's the reasoning behind $$slots not being reactive? It would help me out personally when I need to hide/show content based on something being in the slots or not.

i.e. if the implementer doesn't provide something in the slot, showing some empty state message in my UI.

@login2030
Copy link

$$slots is still an empty object. Even if there is a defer in the script tag =(

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

6 participants