š Scenario: You are building a reusable card component for a website. The card should have separate areas for a header, main content, and footer. You want to use Svelte's named slots to allow flexible content insertion in these areas.
šÆ Goal: Create a Svelte Card component that uses named slots for header, content, and footer. Then use this component in an App.svelte file, filling each slot with different content.
š What You'll Learn
Create a Card.svelte component with named slots: header, content, and footer.
Use the <slot name="..."> syntax inside the Card.svelte component.
In App.svelte, use the Card component and provide content for each named slot.
Ensure the default slot is not used; only named slots are used.
Use semantic HTML elements inside the card for accessibility.
š” Why This Matters
š Real World
Named slots let you build flexible UI components where different parts can be filled with custom content. This is common in dashboards, cards, modals, and layout components.
š¼ Career
Understanding named slots is important for frontend developers working with Svelte or similar frameworks. It helps create reusable, accessible, and maintainable components.
Progress0 / 4 steps
1
Create the Card component with named slots
Create a Svelte component file named Card.svelte. Inside it, write HTML with three sections: a <header> containing a named slot called header, a <main> containing a named slot called content, and a <footer> containing a named slot called footer. Use the syntax <slot name="header"></slot> for each slot.
Svelte
Hint
Use <slot name="header"></slot> inside <header> and similarly for content and footer.
2
Set up the App component to use Card
Create a file named App.svelte. Import the Card component using import Card from './Card.svelte';. Add the <Card> component in the markup. Do not add any slot content yet.
Svelte
Hint
Use import Card from './Card.svelte'; and add <Card></Card> in the markup.
3
Add content to each named slot in App
Inside the <Card> component in App.svelte, add three elements with the slot attribute set to header, content, and footer respectively. For example, add a <h1 slot="header">Welcome</h1> for the header slot. Add a paragraph with slot="content" and a small footer text with slot="footer".
Svelte
Hint
Use elements with slot="header", slot="content", and slot="footer" inside <Card>.
4
Add accessibility and semantic improvements
In Card.svelte, add aria-label="Card component" to the <main> element for accessibility. Also, add a class card to the outermost element (wrap the existing content in a <section class="card"> element).
Svelte
Hint
Wrap content in <section class="card"> and add aria-label="Card component" to <main>.
Practice
(1/5)
1. What is the main purpose of named slots in Svelte components?
easy
A. To allow placing content into specific parts of a component
B. To define component styles globally
C. To handle component state changes
D. To import external JavaScript files
Solution
Step 1: Understand what slots do
Slots let you insert content inside a component from outside.
Step 2: Identify the role of named slots
Named slots let you put content into specific places inside the component, not just one default spot.
Final Answer:
To allow placing content into specific parts of a component -> Option A
Quick Check:
Named slots = specific content placement [OK]
Hint: Named slots = content in specific component areas [OK]
Common Mistakes:
Confusing slots with styling or state
Thinking slots import files
Assuming slots handle events
2. Which of the following is the correct syntax to define a named slot called header inside a Svelte component?
easy
A.
B.
C.
D.
Solution
Step 1: Recall slot syntax in Svelte
Svelte uses the <slot> tag with a name attribute to define named slots.
Step 2: Match correct attribute
The attribute to name a slot is name, not id or custom tags.
Assuming MyComponent defines named slots header and footer.
medium
A. Slots must be direct children of the component, so this is invalid
B. Multiple elements use the same slot name 'header', causing only the last to render
C. Slot names must be unique and cannot be reused in the same component usage
D. There is no error; all content will render in the header slot
Solution
Step 1: Check multiple slot usage
When multiple elements use the same slot name, only the last one is rendered in that slot.
Step 2: Understand slot content replacement
The first header content is replaced by the second because slots accept one content block per name.
Final Answer:
Multiple elements use the same slot name 'header', causing only the last to render -> Option B
Quick Check:
Duplicate slot names = last content wins [OK]
Hint: Only one content block per named slot is rendered [OK]
Common Mistakes:
Thinking all duplicates render together
Believing slot names must be unique globally
Assuming nested slots behave differently
5. You want to create a Svelte component Card with named slots for header, content, and footer. You also want to provide default content for each slot if none is passed. Which code snippet correctly implements this?
hard
A. Default Header
Default Content
Default Footer
B. Default Header
Default Content
Default Footer
C.
D. Default Header
Default Content
Default Footer
Solution
Step 1: Use correct named slot syntax with default content
Named slots require name attribute and default content inside the slot tags.
Step 2: Verify all slots have names and defaults
Default Header
Default Content
Default Footer correctly names all slots and provides default text inside each slot tag.