Child Snippet
Learn how to use the `child` snippet to render your own elements.
Usage
Many Bits UI components have a default HTML element that wraps their children
. For example, Accordion.Trigger
typically renders as:
While you can set standard button attributes, you might need more control for:
- Applying Svelte transitions or actions
- Using custom components
- Scoped CSS
This is where the child
snippet comes in.
Components supporting render delegation accept an optional child prop, which is a Svelte snippet. When used, the component passes its attributes to this snippet, allowing you to apply them to any element.
Let's take a look at an example using the Accordion.Trigger
component:
The props
object includes event handlers, ARIA attributes, and any other attributes passed to Accordion.Trigger
. Note that when using child
, other children outside this snippet are ignored.
Custom IDs & Attributes
To use custom IDs, event handlers, or other attributes with a custom element, you must pass them to the component first. This is crucial because:
- Many Bits UI internals rely on specific IDs
- Props are merged using a
mergeProps
function to handle cancelling internal handlers, etc.
Correct usage:
In this example, my-custom-id
, the click event handler, and my-custom-class are properly merged into the props
object, ensuring they work alongside Bits UI's internal logic.
Behind the scenes, components using the child prop typically implement logic similar to this:
Floating Content Components
Floating content components (tooltips, popovers, dropdowns, etc.) require special handling when used with the child
snippet due to their positioning requirements with Floating UI.
Implementation Details
When implementing floating content, you must:
- Include a wrapper element within the
child
snippet - Spread the
wrapperProps
prop to this wrapper element - Place your floating content inside this wrapper
Important Considerations
- The wrapper element must remain unstyled as its positioning is managed internally by Floating UI
- The
wrapperProps
contain computed positioning data essential for proper floating behavior - Modifying the wrapper element's styles or structure may break positioning calculations
Affected Components
The following components require a wrapper element:
Combobox.Content
DatePicker.Content
DateRangePicker.Content
DropdownMenu.Content
LinkPreview.Content
Menubar.Content
Popover.Content
Select.Content
Tooltip.Content