Documentation Index
Fetch the complete documentation index at: https://mintlify.com/radix-ui/primitives/llms.txt
Use this file to discover all available pages before exploring further.
Overview
Slot merges its props onto its immediate child element. This is useful for composition patterns where you want to pass props through to a child component while maintaining the child’s original props and ref.Features
- Merges props with the child component
- Composes refs correctly
- Supports event handler composition
- Works with the
asChildpattern
Installation
Anatomy
API Reference
Slot
Merges its props onto its immediate child.The child element to merge props onto. Must be a single React element.
Slottable
Used inside components that use Slot to indicate which content should be rendered as the slotted element.Examples
Basic Usage
With Custom Component
Event Handler Composition
With Slottable
How It Works
Slot works by:- Taking a single child element
- Merging its own props with the child’s props
- Composing refs using
composeRefs - Composing event handlers so both parent and child handlers are called
- Cloning the child element with the merged props
The asChild Pattern
TheasChild pattern is commonly used with Slot to allow components to be polymorphic:
- Flexibility: Users can render any element while keeping your component’s functionality
- Accessibility: Users can choose the most semantically appropriate element
- Styling: The component’s styles are applied regardless of the underlying element
Common Use Cases
- Creating polymorphic components that can render as different elements
- Building composable primitives that pass props to children
- Implementing the
asChildprop pattern in component libraries - Merging event handlers and refs in composite components
Limitations
- Only works with a single child element
- Child must be a valid React element, not a string or number
- Does not work with React.Fragment as the direct child