architecture-compound-components — Claude Code rule: purpose and setup
The architecture-compound-components rule describes splitting complex UI components into pieces that share state through context. It fits components that have accumulated render props and conditional options.
MIT ★ 7,191 updated 2026-10-24
.claude/rules/architecture-compound-components.mdWhat architecture-compound-components does
The rule separates a complex component into a Provider, a frame, and smaller pieces such as an input, submit button, or footer. Each piece reads shared state and actions from context instead of receiving them through props. Consumers choose which pieces to compose, without hidden conditionals.
A parent Provider supplies state, actions, and metadata. The same component structure can then be used with different dependencies. The parts need a Provider supplying the shared values.
The example uses a Composer built with React and TypeScript: its input updates state through an action, while its submit button calls the submit action. To install the rule in Claude Code, add .claude/rules/architecture-compound-components.md to the project.
What it helps with
- split a monolithic component into composable pieces
- share component state through context
- build an interface from selected pieces without hidden conditions
- reuse a component structure with different dependencies
Example prompts for the agent
- Refactor the Composer into smaller components that share state through context.
- Replace the component's render callbacks and boolean options with a Provider, Frame, Input, and Footer.
- Show how to pass state and actions through a Provider and compose a form from selected pieces.
How to install architecture-compound-components
Manually: put the files in place
.claude/rules/architecture-compound-components.mdThe paths frontmatter field limits the files
AGENTS.md
Codex reads AGENTS.md: paste the rule text there
Manually: put the files in place
.cursor/rules/architecture-compound-components.mdcFrontmatter: description, globs, alwaysApply
Manually: put the files in place
.github/instructions/architecture-compound-components.instructions.mdThe applyTo field sets which files it applies to
Manually: put the files in place
.devin/rules/architecture-compound-components.mdManually: put the files in place
.clinerules/architecture-compound-components.mdStep by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use
Questions about architecture-compound-components
How do I install architecture-compound-components in Claude Code?
Add the rule at .claude/rules/architecture-compound-components.md.
What does architecture-compound-components do?
It recommends splitting complex components into pieces that access shared state through context. Consumers choose which pieces make up the interface.
How do the component pieces get state and actions?
A parent Provider supplies state, actions, and metadata through shared context. The pieces access that context rather than receiving those values as props.
Source text
architecture-compound-components.md · MIT
---
title: Use Compound Components
impact: HIGH
impactDescription: enables flexible composition without prop drilling
tags: composition, compound-components, architecture
---
## Use Compound Components
Structure complex components as compound components with a shared context. Each
subcomponent accesses shared state via context, not props. Consumers compose the
pieces they need.
**Incorrect (monolithic component with render props):**
```tsx
function Composer({
renderHeader,
renderFooter,
renderActions,
showAttachments,
showFormatting,
showEmojis,
}: Props) {
return (
<form>
{renderHeader?.()}
<Input />
{showAttachments && <Attachments />}
{renderFooter ? (
renderFooter()
) : (
<Footer>
{showFormatting && <Formatting />}
{showEmojis && <Emojis />}
{renderActions?.()}
</Footer>
)}
</form>
)
}
```
**Correct (compound components with shared context):**
```tsx
const ComposerContext = createContext<ComposerContextValue | null>(null)
function ComposerProvider({ children, state, actions, meta }: ProviderProps) {
return (
<ComposerContext value={{ state, actions, meta }}>
{children}
</ComposerContext>
)
}
function ComposerFrame({ children }: { children: React.ReactNode }) {
return <form>{children}</form>
}
function ComposerInput() {
const {
state,
actions: { update },
meta: { inputRef },
} = use(ComposerContext)
return (
<TextInput
ref={inputRef}
value={state.input}
onChangeText={(text) => update((s) => ({ ...s, input: text }))}
/>
)
}
function ComposerSubmit() {
const {
actions: { submit },
} = use(ComposerContext)
return <Button onPress={submit}>Send</Button>
}
// Export as compound component
const Composer = {
Provider: ComposerProvider,
Frame: ComposerFrame,
Input: ComposerInput,
Submit: ComposerSubmit,
Header: ComposerHeader,
Footer: ComposerFooter,
Attachments: ComposerAttachments,
Formatting: ComposerFormatting,
Emojis: ComposerEmojis,
}
```
**Usage:**
```tsx
<Composer.Provider state={state} actions={actions} meta={meta}>
<Composer.Frame>
<Composer.Header />
<Composer.Input />
<Composer.Footer>
<Composer.Formatting />
<Composer.Submit />
</Composer.Footer>
</Composer.Frame>
</Composer.Provider>
```
Consumers explicitly compose exactly what they need. No hidden conditionals. And the state, actions and meta are dependency-injected by a parent provider, allowing multiple usages of the same component structure.
© tech-leads-club · MIT · tech-leads-club/agent-skills