Tabs with compound components

Context & Patterns — Intermediate React challenge.

Build a Tabs component using the 'compound components' pattern — Tabs, Tabs.List, Tabs.Tab, Tabs.Panels, and Tabs.Panel work together, sharing active-tab state implicitly through Context instead of the consumer wiring up index/state props by hand.

Requirements

Signature

function Tabs({ defaultIndex?: number; children: React.ReactNode }): JSX.Element
Tabs.List, Tabs.Tab({ index }), Tabs.Panels, Tabs.Panel({ index })

Example

<Tabs defaultIndex={0}>
  <Tabs.List>
    <Tabs.Tab index={0}>Profile</Tabs.Tab>
    <Tabs.Tab index={1}>Settings</Tabs.Tab>
  </Tabs.List>
  <Tabs.Panels>
    <Tabs.Panel index={0}>Profile content</Tabs.Panel>
    <Tabs.Panel index={1}>Settings content</Tabs.Panel>
  </Tabs.Panels>
</Tabs>
// clicking the "Settings" tab swaps the visible panel

Constraints

🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code 🏷️ Get 20% OFF on Yearly & Lifetime plans — use code
Loading workspace…