The compound pattern addresses a common pain point: component APIs that grow to have dozens of props as every variant is added. By inverting control to the consumer (they choose the composition), the parent component stays lean and the API stays readable.
Trade-offs:
JSX.Element type inference for attached sub-components requires TypeScript declaration merging or explicit typing
Deeply nested compound components may need context bridging if the sub-component is used far from the parent
Over-using this pattern for simple cases adds unnecessary complexity
Common examples in the wild:
HTML <select> / <option> is the canonical compound component
Read the instructions and examples in this document.
Apply the patterns to your implementation, adapting to your specific context.
Verify your implementation against the details and edge cases listed above.
Harness Integration
Type: knowledge — this skill is a reference document, not a procedural workflow.
No tools or state — consumed as context by other skills and agents.
Success Criteria
The patterns described in this document are applied correctly in the implementation.
Edge cases and anti-patterns listed in this document are avoided.
1---2name: react-compound-pattern3description: React Compound Pattern4---5# React Compound Pattern67> Build multi-part components that share state implicitly via context89## When to Use1011- Building UI components with related sub-components (Select/Option, Tabs/Tab/TabPanel, Modal/Header/Body/Footer)12- You want consumers to control composition without prop-drilling13- The parent component needs to coordinate state shared across children14- You are replacing a heavily prop-loaded component with a more flexible API1516## Instructions17181. Create a parent component that owns shared state via `useState` or `useReducer`.192. Create a Context to hold the shared state and expose it.203. Attach child components as static properties of the parent (`Parent.Child = Child`).214. Child components read shared state from context — no explicit prop passing required.225. Export the parent as the public API; children are accessed via dot notation.2324```typescript25const FlyOutContext = createContext<{ open: boolean; toggle: () => void } | null>(null);2627function FlyOut({ children }: { children: React.ReactNode }) {28 const [open, setOpen] = useState(false);29 return (30 <FlyOutContext.Provider value={{ open, toggle: () => setOpen((o) => !o) }}>31 <div className="flyout">{children}</div>32 </FlyOutContext.Provider>33 );34}3536function Toggle() {37 const ctx = useContext(FlyOutContext)!;38 return <button onClick={ctx.toggle}>Toggle</button>;39}4041function List({ children }: { children: React.ReactNode }) {42 const ctx = useContext(FlyOutContext)!;43 return ctx.open ? <ul>{children}</ul> : null;44}4546FlyOut.Toggle = Toggle;47FlyOut.List = List;48```4950## Details5152The compound pattern addresses a common pain point: component APIs that grow to have dozens of props as every variant is added. By inverting control to the consumer (they choose the composition), the parent component stays lean and the API stays readable.5354**Trade-offs:**5556- JSX.Element type inference for attached sub-components requires TypeScript declaration merging or explicit typing57- Deeply nested compound components may need context bridging if the sub-component is used far from the parent58- Over-using this pattern for simple cases adds unnecessary complexity5960**Common examples in the wild:**6162- HTML `<select>` / `<option>` is the canonical compound component63- Radix UI primitives (Dialog.Root / Dialog.Trigger / Dialog.Content)64- Headless UI (Tab / Tab.Group / Tab.Panel)6566## Source6768https://patterns.dev/react/compound-pattern6970## Process71721. Read the instructions and examples in this document.732. Apply the patterns to your implementation, adapting to your specific context.743. Verify your implementation against the details and edge cases listed above.7576## Harness Integration7778- **Type:** knowledge — this skill is a reference document, not a procedural workflow.79- **No tools or state** — consumed as context by other skills and agents.8081## Success Criteria8283- The patterns described in this document are applied correctly in the implementation.84- Edge cases and anti-patterns listed in this document are avoided.
Run npx skillmds@latest add intense-visions/react-compound-pattern in your terminal (requires Node.js), paste this page's agent-chat prompt into Claude, Cursor, or any MCP-connected agent, or download the SKILL.md file and copy it into your agent's skills directory.
React Compound Pattern It is listed under Web & Frontend on SkillMD.
This skill has not completed SkillMD's automated safety review yet. SkillMD never runs a skill's scripts for you; review the SKILL.md before installing.
This skill is tagged as working with Claude Code, Claude.ai, OpenAI Codex. SKILL.md is an open format, so most agents that read a skills directory can load it too.
Yes. Installing skills from SkillMD is free, and the skill stays under its author's original license.
Intense-Visions (@intense-visions) published this skill. Their other Agent Skills are listed on their SkillMD profile.