Sub topic 10
Composing components and using asChild
This chapter assumes you can read the Button and cn code from earlier chapters. It also assumes you know that forwardRef passes a ref to a DOM element, and that you know how props spread with {...props}.
The problem: one element, two owners
Radix primitives render their own element. Dialog.Trigger renders a <button>. Your styled Button also renders a <button>. If you nest one inside the other, you get a button inside a button. That is invalid HTML and confuses screen readers.
You want one element that has the trigger's behavior and the button's styles. The asChild prop does this.
What asChild does
When asChild is true, a Radix component stops rendering its default element. It passes its props to its single child instead. Radix does this with a component called Slot.
Slot merges the following onto the child:
- Props such as
aria-*attributes,data-state, andtype. classNamevalues, which are joined.- Event handlers, which are chained. Both the child's
onClickand the parent'sonClickrun. - The
ref, which is composed so both owners receive the element.
Your shadcn/ui Button uses the same idea. const Comp = asChild ? Slot : "button" lets the caller choose the rendered element.
Example: a styled dialog trigger
Wrap your Button in Dialog.Trigger asChild. The trigger's accessibility attributes and click handler land on the button, and the button keeps its variant styles.
import * as Dialog from "@radix-ui/react-dialog"
import { Button } from "@/components/ui/button"
export function DeleteDialog() {
return (
<Dialog.Root>
<Dialog.Trigger asChild>
<Button variant="outline">Delete item</Button>
</Dialog.Trigger>
<Dialog.Portal>
<Dialog.Overlay className="fixed inset-0 bg-black/50" />
<Dialog.Content className="fixed left-1/2 top-1/2 -translate-x-1/2 -translate-y-1/2 rounded-md bg-white p-6">
<Dialog.Title>Delete this item?</Dialog.Title>
<Dialog.Description>This action cannot be undone.</Dialog.Description>
<Dialog.Close asChild>
<Button size="sm">Cancel</Button>
</Dialog.Close>
</Dialog.Content>
</Dialog.Portal>
</Dialog.Root>
)
}Without asChild, Dialog.Trigger would render its own <button> and wrap your Button in it. With asChild, the DOM contains one <button> that opens the dialog and has the outline styles.
Example: a link that looks like a button
A navigation target should be an <a>, not a <button>. Use asChild on your own Button so the anchor gets the button styles.
import { Button } from "@/components/ui/button"
export function DocsLink() {
return (
<Button asChild variant="outline">
<a href="/docs">Read the docs</a>
</Button>
)
}The output is a plain <a href="/docs"> with the buttonVariants classes. Screen readers announce it as a link, which is correct.
Rules and common errors
asChild fails quietly when the child does not cooperate. Check these points first.
| Symptom | Cause | Fix |
|---|---|---|
| Handlers or focus do not work | The child component does not spread `...props` | Spread props onto the DOM element |
| Dialog focus returns to the wrong place | The child does not forward its ref | Wrap it in `React.forwardRef` |
| Runtime error about children | `Slot` needs exactly one React element | Pass a single element, not text or a fragment |
| Link looks disabled but still works | `disabled` has no effect on `<a>` | Handle it with `aria-disabled` and styles |
Composing in general
asChild is one form of composition. Radix parts such as Dialog.Root, Trigger, and Content are small pieces you arrange yourself. shadcn/ui wraps them into named parts such as DialogTrigger and DialogContent. Because you own those files, you can add asChild support, extra parts, or default classes in one place.
Exercise
You write <Dialog.Trigger><Button>Open</Button></Dialog.Trigger> and the page has a button inside a button. What change fixes it, and what must Button do for the fix to work?
Show answer
Add asChild to Dialog.Trigger. Slot then merges the trigger's props, handlers, and ref onto Button instead of rendering a second <button>. For this to work, Button must forward its ref and spread the remaining props onto its DOM element.