Skip to content
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

Astro.slots.has() wrongly returns true #9278

Closed
1 task
silveltman opened this issue Dec 3, 2023 · 4 comments
Closed
1 task

Astro.slots.has() wrongly returns true #9278

silveltman opened this issue Dec 3, 2023 · 4 comments
Labels
needs discussion Issue needs to be discussed

Comments

@silveltman
Copy link

Astro Info

Astro                    v3.6.3
Node                     v18.17.1
System                   macOS (arm64)
Package Manager          yarn
Output                   static
Adapter                  none
Integrations             @astrojs/svelte
                         @astrojs/sitemap
                         astro-robots-txt
                         bookshop
                         unocss

If this issue only occurs in one browser, which browser is a problem?

/

Describe the Bug

Astro.slots.has('default') return true, even though the slot is empty.

my usecase:

<List {...list}>
      <Socials {socials} />
</List>

after running Astro.slots.render('default') I found out that it returns 2 empty spaces. When changing my component to the following it does work as expected:

<List {...list}> <Socials {socials} /></List>

What's the expected result?

I expect Astro to not count it as a slot if it's just empty spaces. Otherwise we would need to write very uggly code, especially when having a more nested structure. This quickly looks like this:

<Section {...section}><Segment {...segment}><List {...list}><Socials {socials} /></List></Segment></Section>

Not only is it way too hard to read, prettier actually wont allow it with the astro plugin.

Link to Minimal Reproducible Example

na

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Dec 3, 2023
@florian-lefebvre
Copy link
Member

@natemoo-re do you think it's caused by withastro/compiler#893 ?

@matthewp
Copy link
Contributor

matthewp commented Dec 4, 2023

Pretty sure this is intentional, we just check that the slot exists, it hasn't rendered until you call render(). You can call render() and check for non-whitespace to do the same thing.

@matthewp matthewp added needs discussion Issue needs to be discussed and removed needs triage Issue needs to be triaged labels Dec 4, 2023
@matthewp
Copy link
Contributor

matthewp commented Dec 4, 2023

Adding the need discussion label but I'm pretty sure this is just a known limitation of how slots are rendered.

@matthewp
Copy link
Contributor

matthewp commented Dec 6, 2023

Talked about this with some others on the core team and this is indeed expected behavior, has() just says that a slot was passed. Use render() to check on its value.

@matthewp matthewp closed this as completed Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs discussion Issue needs to be discussed
Projects
None yet
Development

No branches or pull requests

3 participants