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

docs: clarify let: directives #8495

Closed
wants to merge 4 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions site/content/docs/03-template-syntax.md
Original file line number Diff line number Diff line change
Expand Up @@ -1600,22 +1600,30 @@ Note that explicitly passing in an empty named slot will add that slot's name to

---

Slots can be rendered zero or more times and can pass values *back* to the parent using props. The parent exposes the values to the slot template using the `let:` directive.
Slots can be rendered zero or more times.

The `let:` directive gives a slot template access to variables defined in the component that they are provided to.
Copy link
Member

Choose a reason for hiding this comment

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

I find this sentence much harder to read/parse than the previous one. Can we find some kind of middle ground between what was there before and what's proposed?

Copy link
Member

@ghostdevv ghostdevv May 11, 2023

Choose a reason for hiding this comment

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

Suggested change
The `let:` directive gives a slot template access to variables defined in the component that they are provided to.
The `let:` directive gives a slot template access to variables passed down by the parent component.

The usual shorthand rules apply — `let:item` is equivalent to `let:item={item}`, and `<slot {item}>` is equivalent to `<slot item={item}>`.

```sv
<!-- FancyList.svelte -->
<!-- This component passes data to its child slot using the `itemData` prop -->
<ul>
{#each items as item}
<li class="fancy">
<slot prop={item}></slot>
<slot itemData={item}></slot>
</li>
{/each}
</ul>

<!-- App.svelte -->
<FancyList {items} let:prop={thing}>
<!-- `itemData` can now be used in the content you pass into the slot using `let:itemData` -->
<FancyList {items} let:itemData>
<div>{itemData.text}</div>
</FancyList>

<!-- You can also rename it using the `let:itemData={thing}` syntax. -->
<FancyList {items} let:itemData={thing}>
<div>{thing.text}</div>
</FancyList>
```
Expand Down