Skip to content

Add documentation for $$slots #5277

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

Merged
merged 6 commits into from
Oct 29, 2020

Conversation

CMessinides
Copy link
Contributor

@CMessinides CMessinides commented Aug 15, 2020

Fixes #5266

Changes

  • Adds a tutorial chapter on optional rendering with $$slots.
  • Notes $$slots under the named slot section of the API documentation.

I pulled my explanation of $$slots from the original PR (#4602) -- @tanhauhau thank you for the write up, and if I've misunderstood anything about the implementation, please don't hesitate to let me know. :)

Caveats

The tutorial chapter isn't running correctly yet -- the REPL halts on the following error:

$$slots is an illegal variable name (ContactCard.svelte:36:6)

I could get it working in the REPL using the local version, so I'm assuming this issue will be revolved when the latest version of the REPL is published?

@tanhauhau
Copy link
Member

Thank you @CMessinides, it looks better now 👍

@@ -1289,6 +1289,27 @@ Named slots allow consumers to target specific areas. They can also have fallbac
</div>
```

---

`$$slots` is an object whose keys are the names of the slots passed into the component by the parent. If the parent leaves a named slot empty, `$$slots` will not have a key for that slot. This allows components to render a slot (and other elements, like wrappers for styling) only if the parent provides children for it.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't quite true. If a slot is explicitly passed with no content, $$slots.foo will still be present. This is basically what #5312 is asking for, and I'm not sure how that's going to be handled yet.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point. I updated to make the difference between omitted slots and empty slots more clear. My language feels a little strained in this sentence:

If the parent does not pass in a slot with a particular name, that name will not be a present in $$slots.

I was trying to avoid saying "If the parent does not pass in a named slot," which sounds ambiguous to my ear -- could be misinterpreted as "If the parent does not pass in any named slots." But I'm not sure if "a slot with a particular name" is the best way to describe what we mean, either. Feedback appreciated.

I also added a note under the $$slots code sample to hammer home the behavior for explicitly empty slots, which may or may not be necessary -- feedback also appreciated on that.

Copy link
Member

@Conduitry Conduitry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See inline comment. We should be careful about distinguishing between not passing a slot and passing a slot with no content.

@CMessinides
Copy link
Contributor Author

@Conduitry Thank you for your review and feedback. Please see my edits and my reply to your inline comment, and let me know if these changes line up with what you were thinking.

@Conduitry
Copy link
Member

Sorry about the long delay! This looks awesome, thank you. I made a couple of small tweaks, am merging now, and will redeploy the site shortly.

@Conduitry Conduitry merged commit 169fd84 into sveltejs:master Oct 29, 2020
@CMessinides
Copy link
Contributor Author

No worries about the delay! Glad I could help :)

taylorzane pushed a commit to taylorzane/svelte that referenced this pull request Dec 17, 2020
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

Successfully merging this pull request may close these issues.

Document $$slots
4 participants