Capítulo 5 de 43
The asChild prop lets any Radix part clone its child element instead of rendering its own default DOM element — this is how you swap the underlying tag or compose Radix's behavior onto your own React components.
asChild: when true, Radix skips rendering its default element and instead clones the single child, injecting the required props/behavior into it.asChild must spread all received props onto its underlying DOM node, or Radix's injected handlers/attributes are lost.asChild must forward its ref (React.forwardRef) since Radix sometimes needs the DOM node reference (e.g. to measure size).asChild can be chained — multiple primitives' triggers (e.g. Tooltip.Trigger + Dialog.Trigger) can all target the same underlying custom button.import * as React from "react";
import { Dialog, Tooltip } from "radix-ui";
const MyButton = React.forwardRef((props, forwardedRef) => (
<button {...props} ref={forwardedRef} />
));
export default () => (
<Dialog.Root>
<Tooltip.Root>
<Tooltip.Trigger asChild>
<Dialog.Trigger asChild>
<MyButton>Open dialog</MyButton>
</Dialog.Trigger>
</Tooltip.Trigger>
<Tooltip.Portal>…</Tooltip.Portal>
</Tooltip.Root>
<Dialog.Portal>...</Dialog.Portal>
</Dialog.Root>
);
asChild.Tooltip.Trigger's default button for a non-focusable div breaks keyboard/screen-reader accessibility — you own that responsibility once you use asChild.asChild when you want a Trigger/part to render as your own design-system component instead of Radix's plain default element.asChild composition.asChild, you inherit responsibility for keeping it accessible.asChild carry accessibility risk.asChild relies on.