Flexible Component Order
What you'll learn: How to let users arrange compound children in any sequence while keeping shared state working correctly.
The Core Idea
When you build compound components like <Tabs> with <TabList> and <TabPanels>, consumers might reasonably want to put panels above tabs, or mix in other elements between them. Flexible component order means your compound component doesn't break when children are reordered—the shared state (like "which tab is active") still flows correctly to all parts.
This differs from rigid compound components that expect children in a specific sequence.
Why It Matters
Imagine a <Tabs> component that assumes <TabList> always comes before <TabPanels>. If a developer wants the panels on top for a mobile layout, they shouldn't have to rewrite your entire component. Flexible order respects their design needs.
The secret? Use context to share state, not position-based assumptions. When <Tabs> provides activeIndex via context, it doesn't matter whether <TabList> or <TabPanels> renders first—both read from the same source of truth.
The Pattern
// Consumers can now do this:
<Tabs>
<TabPanels>
<TabPanel>Content A</TabPanel>
<TabPanel>Content B</TabPanel>
</TabPanels>
{/* Tabs below panels! */}
<TabList>
<Tab>Tab A</Tab>
<Tab>Tab B</Tab>
</TabList>
</Tabs>
Because activeIndex lives in context (not in prop drilling), both <TabList> and <TabPanels> sync perfectly regardless of their DOM order.
Key Takeaway: Use context for shared state in compound components so consumers can compose children in any order without breaking functionality—design flexibility comes from location-agnostic state sharing.