Planning a Compound Component API
You'll learn: How to identify the shared state and interactions that child components need to coordinate in a compound component system.
The Planning Phase Matters
Before you write any code for a compound component, you need to map out what state and behavior your components will share. This planning step prevents awkward refactoring later and creates a cleaner, more intuitive API.
Identifying Shared State
Ask yourself: What information do multiple child components need to read or modify?
For a <Tabs> component, the shared state might be:
- Which tab is currently active
- Whether tabs can be disabled
- Keyboard navigation focus state
For an <Accordion> component:
- Which panels are expanded
- Whether multiple panels can be open simultaneously
- Animation timing preferences
Identifying Shared Interactions
Next: What actions need to coordinate across components?
In <Tabs>:
- Clicking a tab header should update the active index
- Switching tabs should hide one panel and show another
- Keyboard navigation should move between tab headers
In <Accordion>:
- Opening one panel might need to close others (exclusive mode)
- Clicking a trigger should toggle its associated panel
- External controls might expand/collapse all panels
The Analogy
Think of compound components like a band playing together. The shared state is the sheet music everyone can see, and the shared interactions are the conductor's signals that keep everyone synchronized. Planning means deciding what information goes on the sheet music and what signals the conductor needs.
Key Takeaway: Before building compound components, clearly identify what state needs to be shared (the "what") and what interactions need coordination (the "how") — this becomes your Context API contract.