Compound Components with Context
Build multi-part components that share state through context.
Compound Components with Context is a free Sveltejs Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Compound Components
A compound component is one logical UI split into several sub-components that coordinate (Tabs + Tab + TabPanel).
Shared State via Context
The parent sets shared state in context; children read it via getContext.
<!-- Tabs.svelte -->
setContext("tabs", { active, setActive });Child Reads Context
Each Tab and TabPanel grabs the shared API.
<!-- Tab.svelte -->
const { active, setActive } = getContext("tabs");Loose Coupling
Children do not directly reference the parent; they communicate through context.
Composable API
Consumers can rearrange and customize the sub-components freely.
<Tabs>
<TabList>
<Tab id="a">A</Tab>
<Tab id="b">B</Tab>
</TabList>
<TabPanel id="a">Content A</TabPanel>
<TabPanel id="b">Content B</TabPanel>
</Tabs>Examples
Tabs, Accordion, Menu, Listbox, Steps.
Naming Convention
Use shared prefix or namespace exports for clarity.
import { Tabs, Tab, TabList, TabPanel } from "$lib/Tabs";Type-Safe Context
Define a symbol key + type for the shared API.
Errors When Misused
If a child cannot find the context, throw a friendly error to guide consumers.
Reusability
Compound components are highly reusable because consumers control composition.
Combine With Builders
Compound components often use the builder pattern internally for headless accessibility.
Quick Check
How do compound components share state?
Recap
Compound components split a UI into composable sub-components that coordinate via context.
Frequently asked questions
Is the “Compound Components with Context” lesson free?
Yes — the full text of “Compound Components with Context” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.
What will I learn in “Compound Components with Context”?
Build multi-part components that share state through context. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Sveltejs Academy?
No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Compound Components with Context” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Sveltejs Academy lesson?
Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Render Props with Snippets
- Higher-Order Components
- The Builder Pattern for Headless UI
- Compound Components with Context