From cc15594e69206e1439c985a9e2946f8efb06a398 Mon Sep 17 00:00:00 2001 From: Hunter Johnston Date: Thu, 11 Jul 2024 17:53:17 -0400 Subject: [PATCH] update delegation docs --- sites/docs/content/delegation.md | 70 ++++++++++++++------------------ 1 file changed, 31 insertions(+), 39 deletions(-) diff --git a/sites/docs/content/delegation.md b/sites/docs/content/delegation.md index 5fca92f98..33dae6fbc 100644 --- a/sites/docs/content/delegation.md +++ b/sites/docs/content/delegation.md @@ -1,69 +1,61 @@ --- title: Render Delegation -description: Learn how to use the asChild prop to render your own elements. +description: Learn how to use the `child` snippet to render your own elements. --- ## Usage -For certain components, we set a default underlying HTML element to wrap the `` with. As a high level example, the `AccordionTrigger`, looks something like this: +For certain components, we set a default underlying HTML element to wrap the `children` with. As a high level example, the `Accordion.Trigger`, looks something like this: ```svelte ``` -While we do allow you to set any attribute that you normally could on a button, let's say you're one of those `
` people and want to use a `div` or some other element instead of the button we provide out of the box. That's when render delegation comes into play. +While we do allow you to set any attribute that you normally could on a button, let's say you want to apply a [Svelte Transition](https://svelte.dev/docs#transition) or [Svelte Action](https://svelte.dev/docs#use_action) to the button, or use a custom component you've made, that's when render delegation comes into play. -Each of the components that support render delegation have an optional prop called `asChild`. When set to `true`, the component will pass the `builder` as a slot prop, which you can then apply to the element of your choosing. +Each of the components that support render delegation accept an optional prop called `child`, which is a [Snippet](https://svelte.dev). When used, the component will pass the attributes as a snippet prop, which you can then apply to the element of your choosing. Note, if you use `child` any other children that aren't within that `child` snippet will be ignored. -Let's take a look at an example using the `AccordionTrigger` component: +Let's take a look at an example using the `Accordion.Trigger` component: ```svelte - -
Open accordion item
-
+ + {#snippet child({ props })} +
Open accordion item
+ {/snippet} +
``` -We're passing the Melt UI builder we would normally apply to the ` {/if} ``` - -## Use with components - -Unfortunately, wanting to delegate rendering to a component isn't as simple as an element, since we can't apply actions directly to components. While still possible, there's some work that has to be done to each component you intend to use render delegation with. - -At the time of writing this, we're still working on refined type / helpers functions on the Melt UI side for this exact purpose, but for the time being, we expose a few helper functions and types to make this easier. - -Let's create a custom ` -``` - -We're using the `builderActions` and `getAttrs` helpers to apply the actions and attributes to the button. We're also using the `Builder` type to type the `builders` prop we'd receive from whatever component we're using this with. We use an array here to cover the case where we may want to apply multiple builders to the button. The helper functions handle applying all the actions and attributes to the button for us. - -If you do plan to pass multiple builders, the order in which you pass them matters. Certain actions/attributes may override others, so be sure to test your implementation to ensure it's working as expected.