📖 Scenario: You are building a reusable Card component in Svelte that can display custom content inside it. Sometimes, the user of the component might not provide any content. To handle this, you want to show a default message inside the card when no content is passed.
🎯 Goal: Create a Svelte Card component that uses a <slot> with fallback content. This fallback content should display the text "No content provided" when no content is passed to the slot.
📋 What You'll Learn
Create a Svelte component named Card.svelte with a <slot> element.
Add fallback content inside the <slot> that says No content provided.
Use the Card component in an App.svelte file twice: once with custom content and once without any content.
Ensure the fallback content appears only when no slot content is passed.
💡 Why This Matters
🌍 Real World
Slot fallback content is useful when building reusable UI components that can show default messages or placeholders if no custom content is provided by the user.
💼 Career
Understanding slots and fallback content is important for frontend developers working with Svelte or other component-based frameworks to create flexible and user-friendly components.
Progress0 / 4 steps
1
Create the Card component with a slot
Create a Svelte component file named Card.svelte. Inside it, write a <div> with class card and place an empty <slot> element inside it.
Svelte
Hint
Use the <slot> tag inside the div with class card.
2
Add fallback content inside the slot
Inside the Card.svelte component, add fallback content inside the <slot> element that says No content provided. This text should appear if no content is passed to the slot.
Svelte
Hint
Place the fallback text between the opening and closing <slot> tags.
3
Use the Card component with custom content
Create an App.svelte file. Import the Card component. Use the <Card> component once with the content <p>Hello, Svelte!</p> inside it.
Svelte
Hint
Use import Card from './Card.svelte' and place <p>Hello, Svelte!</p> inside the <Card> tags.
4
Use the Card component without content to show fallback
In the same App.svelte file, add another <Card> component without any content inside it. This should display the fallback text No content provided inside the card.
Svelte
Hint
Add a second <Card> component with no children to show fallback content.
Practice
(1/5)
1. What does fallback content in a Svelte slot do?
easy
A. It disables the slot from receiving content.
B. It hides the slot completely when empty.
C. It duplicates the slot content automatically.
D. It shows default content if no slot content is provided.
Solution
Step 1: Understand slot usage in Svelte
Slots allow external content to be inserted into a component.
Step 2: Role of fallback content
If no content is passed to the slot, fallback content is displayed instead.
Final Answer:
It shows default content if no slot content is provided. -> Option D
Quick Check:
Fallback content = default display when slot empty [OK]
Hint: Fallback content appears only if slot is empty [OK]
Common Mistakes:
Thinking fallback hides the slot
Assuming fallback duplicates content
Believing fallback disables the slot
2. Which of the following is the correct syntax to provide fallback content inside a Svelte slot?
easy
A. Fallback text here
B.
C. {Fallback text here}
D. Fallback text here
Solution
Step 1: Recall Svelte slot syntax
Fallback content is placed between the opening and closing <slot> tags.
Step 2: Identify correct syntax
Only <slot>Fallback text here</slot> correctly defines fallback content.
Final Answer:
<slot>Fallback text here</slot> -> Option A
Quick Check:
Fallback inside slot tags = Fallback text here [OK]
The <slot> tag is missing the closing slash on the closing tag (</slot>).
Step 2: Validate HTML correctness
Proper closing tags are required; missing slash causes syntax error.
Final Answer:
Missing closing tag slash on <slot> element -> Option B
Quick Check:
Proper closing tags = Missing closing tag slash on <slot> element [OK]
Hint: Always close slot tags properly with [OK]
Common Mistakes:
Ignoring missing slash on closing tag
Thinking fallback must be wrapped in div
Believing extra content after slot is invalid
5. You want a Svelte component to show a default message if no slot content is passed, but also include a button that only appears when slot content is provided. How can you achieve this?
hard
A.
Click me
B. Default message
{#if !$$slots.default}
Click me
{/if}
C. Default message
{#if $$slots.default}
Click me
{/if}
D. Default message
Click me
Solution
Step 1: Use fallback content inside <slot>
Placing 'Default message' inside <slot> ensures it shows if no content is passed.
Step 2: Conditionally show button with $$slots.default
Using {#if $$slots.default} checks if slot content exists, showing button only then.
Final Answer:
<slot>Default message</slot>
{#if $$slots.default}
<button>Click me</button>
{/if} -> Option C