diff --git a/site/content/tutorial/14-composition/04-optional-slots/app-a/App.svelte b/site/content/tutorial/14-composition/04-optional-slots/app-a/App.svelte old mode 100644 new mode 100755 index 620b602eca..80a4df59f8 --- a/site/content/tutorial/14-composition/04-optional-slots/app-a/App.svelte +++ b/site/content/tutorial/14-composition/04-optional-slots/app-a/App.svelte @@ -1,14 +1,57 @@ - - - P. Sherman - + + +

+ Projects +

+ + diff --git a/site/content/tutorial/14-composition/04-optional-slots/app-a/Comment.svelte b/site/content/tutorial/14-composition/04-optional-slots/app-a/Comment.svelte new file mode 100755 index 0000000000..8d15ffa882 --- /dev/null +++ b/site/content/tutorial/14-composition/04-optional-slots/app-a/Comment.svelte @@ -0,0 +1,56 @@ + + + + +
+
+ +
+

{name}

+ +
+
+
+ +
+
diff --git a/site/content/tutorial/14-composition/04-optional-slots/app-a/ContactCard.svelte b/site/content/tutorial/14-composition/04-optional-slots/app-a/ContactCard.svelte deleted file mode 100644 index 1acf290943..0000000000 --- a/site/content/tutorial/14-composition/04-optional-slots/app-a/ContactCard.svelte +++ /dev/null @@ -1,47 +0,0 @@ - - -
-

- - Unknown name - -

- -
- - Unknown address - -
- - -
\ No newline at end of file diff --git a/site/content/tutorial/14-composition/04-optional-slots/app-a/Project.svelte b/site/content/tutorial/14-composition/04-optional-slots/app-a/Project.svelte new file mode 100755 index 0000000000..38d19fc638 --- /dev/null +++ b/site/content/tutorial/14-composition/04-optional-slots/app-a/Project.svelte @@ -0,0 +1,62 @@ + + + + +
+
+

{title}

+

{tasksCompleted}/{totalTasks} tasks completed

+
+
+

Comments

+ +
+
diff --git a/site/content/tutorial/14-composition/04-optional-slots/app-b/App.svelte b/site/content/tutorial/14-composition/04-optional-slots/app-b/App.svelte old mode 100644 new mode 100755 index aa6d225905..80a4df59f8 --- a/site/content/tutorial/14-composition/04-optional-slots/app-b/App.svelte +++ b/site/content/tutorial/14-composition/04-optional-slots/app-b/App.svelte @@ -1,14 +1,57 @@ - - - P. Sherman - + + +

+ Projects +

+ + diff --git a/site/content/tutorial/14-composition/04-optional-slots/app-b/Comment.svelte b/site/content/tutorial/14-composition/04-optional-slots/app-b/Comment.svelte new file mode 100755 index 0000000000..8d15ffa882 --- /dev/null +++ b/site/content/tutorial/14-composition/04-optional-slots/app-b/Comment.svelte @@ -0,0 +1,56 @@ + + + + +
+
+ +
+

{name}

+ +
+
+
+ +
+
diff --git a/site/content/tutorial/14-composition/04-optional-slots/app-b/ContactCard.svelte b/site/content/tutorial/14-composition/04-optional-slots/app-b/ContactCard.svelte deleted file mode 100644 index 9bdb48953b..0000000000 --- a/site/content/tutorial/14-composition/04-optional-slots/app-b/ContactCard.svelte +++ /dev/null @@ -1,47 +0,0 @@ - - -
-

- - Unknown name - -

- - {#if $$slots.address} -
- -
- {/if} - - {#if $$slots.email} - - {/if} -
diff --git a/site/content/tutorial/14-composition/04-optional-slots/app-b/Project.svelte b/site/content/tutorial/14-composition/04-optional-slots/app-b/Project.svelte new file mode 100755 index 0000000000..87a1d0c3ac --- /dev/null +++ b/site/content/tutorial/14-composition/04-optional-slots/app-b/Project.svelte @@ -0,0 +1,64 @@ + + + + +
+
+

{title}

+

{tasksCompleted}/{totalTasks} tasks completed

+
+ {#if $$slots.comments} +
+

Comments

+ +
+ {/if} +
diff --git a/site/content/tutorial/14-composition/04-optional-slots/text.md b/site/content/tutorial/14-composition/04-optional-slots/text.md index 8d095bc211..5f77c89f11 100644 --- a/site/content/tutorial/14-composition/04-optional-slots/text.md +++ b/site/content/tutorial/14-composition/04-optional-slots/text.md @@ -1,25 +1,28 @@ --- -title: Optional slots +title: Checking slots --- -In the previous example, the contact card rendered fallback text if a named slot was left empty. But for some slots, perhaps you don't want to render anything at all. We can do this by checking the properties of the special `$$slots` variable. +In some cases, you may want to control parts of your component based on whether the parent passes in content for a certain slot. Perhaps you have a wrapper around that slot, and you don't want to render it if the slot is empty. Or perhaps you'd like to apply a class only if the slot is present. You can do this by checking the properties of the special `$$slots` variable. `$$slots` is an object whose keys are the names of the slots passed in by the parent component. If the parent leaves a slot empty, then `$$slots` will not have an entry for that slot. -In `ContactCard.svelte`, wrap the `address` and `email` slots in `if` blocks that check `$$slots`, and remove the fallbacks from each ``: +Notice that both instances of `` in this example render a container for comments and a notification dot, even though only one has comments. We want to use `$$slots` to make sure we only render these elements when the parent `` passes in content for the `comments` slot. + +In `Project.svelte`, update the `class:has-discussion` directive on the `
`: ```html -{#if $$slots.address} -
- -
-{/if} +
+``` + +Next, wrap the `comments` slot and its wrapping `
` in an `if` block that checks `$$slots`: -{#if $$slots.email} -