Skip to main content

Composition

View as Markdown

Use a component's behavior on your own element and compose multiple components on a single element.

Pass asChild="{true}" to merge the component's attributes into its first child element. The child takes the place of the element the part would render:

The <a> tag receives all necessary attributes (aria-describedby, event handlers, etc.) while keeping its link behavior.

Your own components offer asChild when you add {ui:asChild()} inside the opening bracket of the tag that should receive the merged attributes, next to {ui:attributes()}:

{ui:asChild()} registers the prop and marks the target tag, so no ui:prop is needed.

Your element has to stay accessible. A <div> in place of a <button> needs a role, a tabindex and the keyboard handlers the original element provided natively.

A collapsible trigger that also has a tooltip is one element that both components write an id onto. Pass both the same ID, so the ARIA attributes (aria-controls, aria-describedby) keep pointing at that element:

The ids prop accepts an object where keys are part names (trigger, content, etc.) and values are the IDs to use.