Composition
A guide to composing Sveltery Base components with your own Svelte components.
Composing custom Svelte components
Use the render prop to compose a Sveltery Base part with your own Svelte components.
In Sveltery Base, render is a snippet. It receives the props to spread onto your element, the part's state, and the part's children.
For example, most triggers render a <button> by default.
The code snippet below shows how to use a custom button instead.
The custom component must spread all the received props on its underlying DOM node. Svelte has no refs to forward. The part reads its element through an attachment inside props, so the spread is what connects the part to the element.
Composing multiple components
In situations where you need to compose multiple Sveltery Base components with custom Svelte components, render snippets can be nested as deeply as necessary.
Working with Tooltip is a common example.
Changing the default rendered element
You can also use the render prop to override the rendered element of the component.
For example, <Accordion.Header> renders an <h3> element by default.
The code snippet below shows how to render it as an <h2> element so that it fits the heading hierarchy of the page.
Each Sveltery Base component renders the most appropriate element by default, and in most cases, rendering a different element is recommended only on a case-by-case basis.
Render function
Base UI's render prop takes either a React element or a function. Svelte has no element objects to clone, so a Sveltery Base render snippet always works like Base UI's render function.
Using a snippet gives you complete control over spreading props and also allows you to render different content based on the component's state.