Skip to main content
rule CX Codex

as-child — Claude Code rule: what it does, how to install

as-child is a Claude Code rule for merging a component’s props and handlers into a custom child without an extra DOM wrapper. It applies to composable React triggers.

Apache-2.0 ★ 141 updated 2026-11-17

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

What as-child does

The as-child rule defines a component pattern that places its props, behavior, and event handlers on a custom child instead of rendering the component’s default DOM element. This keeps a trigger’s behavior while leaving the chosen element in the DOM.

The implementation uses Slot from @radix-ui/react-slot. The child component has to forward received props with {...props}, and asChild accepts one child element. Manual React.cloneElement is not the prescribed implementation here.

The pattern covers Dialog.Trigger, Tooltip.Trigger, DropdownMenu.Trigger, and NavigationMenu.Link, including nested composition. A non-semantic child needs an appropriate role, tabIndex, and ARIA attribute.

What it helps with

  • remove nested buttons and extra DOM wrappers
  • forward trigger handlers to a custom element
  • keep HTML semantics when changing the rendered element
  • compose several trigger components around one child

Example prompts for the agent

  • Describe the asChild pattern for Dialog.Trigger with a single button child.
  • Check this component: the child must receive props through {...props}, and the merge should use Slot.
  • Show asChild in NavigationMenu.Link with a Link component while preserving accessibility.

How to install as-child

Manually: put the files in place

.claude/rules/as-child.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 as-child

How do I install as-child in Claude Code?

Place the rule file at .claude/rules/as-child.md.

What does as-child do?

It moves a component’s props, behavior, and event handlers onto a custom child element. The component can then render that element instead of adding its default DOM wrapper.

What does as-child require to work?

The implementation uses @radix-ui/react-slot. The child must spread received props with {...props}, and asChild must receive one child element.

Source text

as-child.md · Apache-2.0
---
title: asChild Pattern
impact: MEDIUM
impactDescription: Prop merging without wrapper elements
tags: as-child, slot, radix, composition, prop-merging
---

## asChild Pattern

The `asChild` prop allows components to merge their props, behaviors, and event handlers with a custom child element instead of rendering a default DOM element.

### When to Use

Implement `asChild` when:

- Building trigger components (Dialog.Trigger, Tooltip.Trigger)
- Creating composable components that wrap other elements
- Allowing users to customize rendered element while preserving functionality
- Integrating with existing design system components

### Implementation

```tsx
import { Slot } from "@radix-ui/react-slot";

interface TriggerProps extends React.ComponentPropsWithoutRef<"button"> {
  asChild?: boolean;
}

export const Trigger = React.forwardRef<HTMLButtonElement, TriggerProps>(
  ({ asChild = false, children, ...props }, ref) => {
    if (asChild) {
      return (
        <Slot ref={ref} {...props}>
          {children}
        </Slot>
      );
    }
    return (
      <button ref={ref} {...props}>
        {children}
      </button>
    );
  }
);
```

### How It Works

**Without asChild (nested elements):**

```tsx
<Dialog.Trigger>
  <button>Open Dialog</button>
</Dialog.Trigger>
// Renders: <button><button>Open Dialog</button></button>
```

**With asChild (props merge onto child):**

```tsx
<Dialog.Trigger asChild>
  <button>Open Dialog</button>
</Dialog.Trigger>
// Renders: <button data-state="closed">Open Dialog</button>
```

### Common Use Cases

**Custom Trigger Elements:**

```tsx
<AlertDialog.Trigger asChild>
  <a href="/delete">Delete Account</a>
</AlertDialog.Trigger>

<Tooltip.Trigger asChild>
  <IconButton icon={<InfoIcon />} />
</Tooltip.Trigger>

<DropdownMenu.Trigger asChild>
  <Button variant="outline" size="icon">
    <MoreVertical className="h-4 w-4" />
  </Button>
</DropdownMenu.Trigger>
```

**Semantic HTML:**

```tsx
<NavigationMenu.Link asChild>
  <Link href="/products" className="nav-link">
    Products
  </Link>
</NavigationMenu.Link>
```

**Component Composition:**

```tsx
<Dialog.Trigger asChild>
  <Tooltip.Trigger asChild>
    <button>Open dialog (with tooltip)</button>
  </Tooltip.Trigger>
</Dialog.Trigger>
```

### Best Practices

**1. Child Must Spread Props:**

**Incorrect:**

```tsx
// Won't receive trigger behavior
const BadButton = ({ children }) => <button>{children}</button>;
```

**Correct:**

```tsx
// Properly receives all props
const GoodButton = ({ children, ...props }) => (
  <button {...props}>{children}</button>
);
```

**2. Single Child Element Only:**

**Incorrect:**

```tsx
// Multiple children
<Trigger asChild>
  <button>One</button>
  <button>Two</button>
</Trigger>
```

**Correct:**

```tsx
// Single child
<Trigger asChild>
  <button>Single Button</button>
</Trigger>
```

**3. Maintain Accessibility:**

**Correct:**

```tsx
// Maintains button semantics
<Dialog.Trigger asChild>
  <button type="button">Open</button>
</Dialog.Trigger>
```

**Note (ensure proper ARIA):**

```tsx
// Ensure proper ARIA when using non-semantic elements
<Dialog.Trigger asChild>
  <div role="button" tabIndex={0} aria-label="Open dialog">
    Open
  </div>
</Dialog.Trigger>
```

### Common Pitfalls

**Don't manually clone elements:**

**Incorrect:**

```tsx
// Manual cloning misses edge cases
if (asChild) {
  return React.cloneElement(children, { ...props });
}
```

**Correct:**

```tsx
// Use Slot for proper merging
if (asChild) {
  return <Slot {...props}>{children}</Slot>;
}
```

### Integration

The `asChild` pattern is essential for:

- **Composability** - Enables flexible component composition
- **Customizable** - Allows users to control rendered elements
- **Transparent** - No hidden wrapper elements in DOM
- **Accessible** - Maintains semantic HTML structure

Reference: [@radix-ui/react-slot](https://www.radix-ui.com/primitives/docs/utilities/slot)

© maxtrigify · Apache-2.0 · maxtrigify/Otto

Similar IDE rules

All IDE rules for Frontend and UI →

From the same set18

The whole set →
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 documentation documentation is a Claude Code rule that standardizes UI component documentation. It enforces consistent sections for props, runnable examples, and accessibility guidelines. CX Codex +1 ★ 141repo stars state state is a Claude Code rule for controlled and uncontrolled React component state. It applies while designing component APIs and state props. CX Codex +1 ★ 141repo stars accessibility accessibility is a Claude Code rule that enforces accessible frontend patterns. It triggers when generating UI components, dialogs, and navigation menus. CX Codex +1 ★ 141repo stars polymorphism polymorphism is a Claude Code rule for React components that keep their styles and behavior across element types. The as prop selects the rendered element. CX Codex +1 ★ 141repo stars

Project sponsors

Companies that keep this analytics open