---
name: component-composition
description: Component composition patterns with nesting, slots, compound components, and render props. Use when creating reusable components, component APIs, or complex component hierarchies.
allowed-tools: Read, Write, Edit, Glob, Grep
user-invocable: true
---
# Component Composition
Advanced composition patterns for scalable components.
## Agent Workflow (MANDATORY)
Before implementation:
1. **fuse-ai-pilot:explore-codebase** - Check existing composition patterns
2. **fuse-ai-pilot:research-expert** - React 19 composition patterns
After: Run **fuse-ai-pilot:sniper** for validation.
## Pattern Overview
| Pattern | Use Case | Complexity |
|---------|----------|------------|
| Children | Simple containers | Low |
| Slots | Named regions | Medium |
| Compound | Related sub-components | Medium |
| Render Props | Custom rendering | High |
| Context | Shared state | High |
## 1. Children Pattern (Basic)
```tsx
function Card({ children, className }: { children: React.ReactNode }) {
return (
{children}
);
}
// Usage
Title
Content
```
## 2. Slots Pattern
```tsx
interface CardProps {
header?: React.ReactNode;
footer?: React.ReactNode;
children: React.ReactNode;
}
function Card({ header, footer, children }: CardProps) {
return (
{header && (
{header}
)}
{children}
{footer && (
{footer}
)}
);
}
// Usage
Settings}
footer={}
>
Card content here
```
## 3. Compound Components
```tsx
const CardContext = createContext<{ variant: string }>({ variant: "default" });
function Card({ children, variant = "default" }) {
return (
{children}
);
}
Card.Header = function CardHeader({ children }) {
return {children}
;
};
Card.Body = function CardBody({ children }) {
return {children}
;
};
Card.Footer = function CardFooter({ children }) {
const { variant } = useContext(CardContext);
return (
{children}
);
};
// Usage
Title
Content
```
## 4. Render Props
```tsx
interface ListProps {
items: T[];
renderItem: (item: T, index: number) => React.ReactNode;
renderEmpty?: () => React.ReactNode;
}
function List({ items, renderItem, renderEmpty }: ListProps) {
if (items.length === 0) {
return renderEmpty?.() ?? No items
;
}
return (
{items.map((item, i) => (
- {renderItem(item, i)}
))}
);
}
// Usage
}
renderEmpty={() => }
/>
```
## 5. As Prop (Polymorphic)
```tsx
type ButtonProps = {
as?: T;
children: React.ReactNode;
} & React.ComponentPropsWithoutRef;
function Button({
as,
children,
...props
}: ButtonProps) {
const Component = as || "button";
return (
{children}
);
}
// Usage
```
## 6. Forwarded Refs
```tsx
const Input = forwardRef((props, ref) => {
return (
);
});
Input.displayName = "Input";
// Usage with ref
const inputRef = useRef(null);
```
## Composition Guidelines
| DO | DON'T |
|----|-------|
| Use children for simple nesting | Over-engineer simple components |
| Use slots for named regions | Use too many slots (max 3-4) |
| Use compound for related parts | Create deep nesting (max 2 levels) |
| Forward refs for form elements | Forget displayName |
## Validation
```
[ ] Appropriate pattern for complexity
[ ] TypeScript props properly typed
[ ] displayName set on forwardRef
[ ] Context used sparingly
[ ] Max 2-3 composition levels
[ ] Documented API in JSDoc
```
## References
- `../../references/design-patterns.md` - Component patterns
- `../../references/component-examples.md` - Production examples