Skip to main content
rule CX Codex

composition · maxtrigify — Claude Code rule: what it does, how to install

The composition rule for Claude Code guides the breakdown of complex UI components into small, focused parts. It serves when building component libraries or flexible interfaces.

Apache-2.0 ★ 141 updated 2026-11-17

Install into Claude Code · Manually: put the files in place All methods and platforms ↓
.claude/rules/composition.md
Open on GitHub

What composition does

This rule enforces a composition pattern instead of relying on numerous props. Components are split into logical units: a Root manages shared state via context, while Trigger and Content handle specific interactions and display tasks.

Implementing this pattern involves exporting components as a namespace, making the API clean and intuitive. It relies on standard React context to connect components, ensuring they remain independent yet functional together in various layouts.

What it helps with

  • breaking down monolithic components
  • structuring flexible UI elements
  • building maintainable component libraries
  • managing state distribution across sub-components

Example prompts for the agent

  • Add the composition rule to help refactor the current Accordion component.
  • Apply the composition pattern to my registration form.
  • Use the composition rule to separate concerns in the modal component.

How to install composition

Manually: put the files in place

.claude/rules/composition.md

The paths frontmatter field limits the files

Step by step, with commands for each platform: AGENTS.md vs CLAUDE.md: what they are and which one to use

Questions about composition

How to install composition in Claude Code?

Create a file at .claude/rules/composition.md and include the rule content.

What does composition do?

It establishes an architectural pattern to divide complex UI into smaller, collaborative components.

When should I use this rule?

Use it when a component has too many responsibilities or requires flexible layout customization.

Source text

composition.md · Apache-2.0
---
title: Component Composition
impact: HIGH
impactDescription: Foundation of component architecture
tags: composition, patterns, sub-components, context, root, trigger, content
---

## Component Composition

Composition is the foundation of building modern UI components. Instead of cramming all functionality into a single component with dozens of props, break components down into smaller, focused sub-components that work together.

**Incorrect (monolithic, hard to customize):**

```tsx
<Accordion data={data} />
```

**Correct (composable, each layer customizable):**

```tsx
<Accordion.Root open={open} setOpen={setOpen}>
  {data.map((item) => (
    <Accordion.Item key={item.title}>
      <Accordion.Trigger>{item.title}</Accordion.Trigger>
      <Accordion.Content>{item.content}</Accordion.Content>
    </Accordion.Item>
  ))}
</Accordion.Root>
```

### Building Composable Components

#### 1. Root Component

The main container that holds sub-components and manages shared state using Context:

```tsx
const AccordionContext = createContext<AccordionContextValue>({
  open: false,
  setOpen: () => {},
});

export const Root = ({
  children,
  open,
  setOpen,
  ...props
}: AccordionRootProps) => (
  <AccordionContext.Provider value={{ open, setOpen }}>
    <div {...props}>{children}</div>
  </AccordionContext.Provider>
);
```

#### 2. Item Component

A simple wrapper for each item:

```tsx
export const Item = (props: AccordionItemProps) => <div {...props} />;
```

#### 3. Trigger Component

Handles user interaction:

```tsx
export const Trigger = ({ asChild, ...props }: AccordionTriggerProps) => {
  const { open, setOpen } = useContext(AccordionContext);
  return (
    <button onClick={() => setOpen(!open)} aria-expanded={open} {...props} />
  );
};
```

#### 4. Content Component

Displays the main content:

```tsx
export const Content = ({ asChild, ...props }: AccordionContentProps) => {
  const { open } = useContext(AccordionContext);
  if (!open) return null;
  return <div {...props} />;
};
```

#### 5. Export Pattern

Export components as a namespace:

```tsx
export const Accordion = { Root, Item, Trigger, Content };

// Usage
<Accordion.Root open={open} setOpen={setOpen}>
  <Accordion.Item>
    <Accordion.Trigger>Title</Accordion.Trigger>
    <Accordion.Content>Content</Accordion.Content>
  </Accordion.Item>
</Accordion.Root>;
```

### Naming Conventions

- **Root** - Main container component
- **Trigger** - Element that initiates an action
- **Content** - Element containing the main content
- **Header/Body/Footer** - Structured content areas
- **Title/Description** - Informational components

### Best Practices

1. **Single Responsibility** - Each sub-component should have one clear purpose
2. **Context for State** - Use React Context to share state between sub-components
3. **Extend HTML Attributes** - Always extend native HTML element props
4. **Consistent Naming** - Follow established conventions
5. **Namespace Exports** - Export components as a namespace for clean API
6. **Composition Over Configuration** - Prefer multiple components over many props

### When to Use Composition

**Use composition when:**

- A component has multiple responsibilities
- Customization requires CSS overrides
- You need flexible layouts or structures
- Building a component library
- Components need to work together but remain independent

**Avoid composition for:**

- Simple, single-purpose components
- Components that don't need customization
- Over-engineering simple UI elements

© maxtrigify · Apache-2.0 · maxtrigify/Otto

Similar IDE rules

All IDE rules for Frontend and UI →

composition by other authors comes first.

composition Devin-AXIS Enforces component composition patterns: wraps list items in groups, requires modal titles for accessibility, and swaps custom divs for UI primitives. CX Codex +1 ★ 7,523repo stars vercel-composition-patterns vercel vercel-composition-patterns — a rule set that pushes your agent away from boolean props and toward compound components, shared context and state lifted into providers. CX Codex +1 464,000installs react-hooks-composition Claude MPM Team react-hooks-composition — a skill with patterns for composing React hooks around SWR, debounced search, and context providers. Use it when building data-fetching hooks or managing async UI states. CX Codex +1 206installs critique-composition Owl-Listener critique-composition critiques balance, negative space, visual rhythm, and gestalt grouping on rendered screens. Useful when layout hierarchy is solid but the spatial composition still feels off. CX Codex +1 2,500installs shot-composition iart-ai shot-composition plans how a frame is staged: grid, focal point, depth layers and camera moves. Useful when a scene needs layering and a layout that survives 16:9, 9:16 and 1:1. CX Codex +1 6,000installs photo-composition-critic curiositech The photo-composition-critic skill critiques a photo’s composition, color, and visual balance, then suggests concrete improvements. Use it to review images or compare crop options. CX Codex +1 674installs patterns-explicit-variants tech-leads-club patterns-explicit-variants — a rule that pushes one component per mode instead of a single component buried under boolean props. The call site then says what renders. CX Codex +1 ★ 7,191repo stars

From the same set18

The whole set →
npm npm is a Claude Code rule that guides the packaging of UI components into versioned releases. It enforces strict exports mapping, peer dependency boundaries, and build steps. CX Codex +1 ★ 141repo stars registry registry is a Claude Code rule for publishing UI components as source code. It defines registry metadata and a deployment flow. CX Codex +1 ★ 141repo stars marketplaces The marketplaces rule for Claude Code organizes component discovery and integration from external registries. It helps the agent evaluate code quality before adding dependencies. CX Codex +1 ★ 141repo stars design-tokens design-tokens is a Claude Code rule that replaces hardcoded colors with semantic tokens. Light and dark themes then change variable values instead of component classes. CX Codex +1 ★ 141repo stars overview overview is a Claude Code rule for designing accessible, composable UI components. It comes into play while shaping component APIs and shared patterns. CX Codex +1 ★ 141repo stars types The types rule for Claude Code standardizes component prop definitions in TypeScript. It provides a framework for building flexible, type-safe interfaces across React projects. CX Codex +1 ★ 141repo stars data-attributes data-attributes is a Claude Code rule for styling component states and targeting nested parts with data-state and data-slot. CX Codex +1 ★ 141repo stars principles The principles rule for Claude Code establishes six core standards for component design. It guides agents during component creation, refactoring, and evaluation. CX Codex +1 ★ 141repo stars

Project sponsors

Companies that keep this analytics open