When to Use
- Build polymorphic components with
asChildpatterns - Merge event handlers and props across component boundaries
- Create flexible wrapper components that don’t add DOM nodes
- Implement headless UI patterns
- Compose ref forwarding across nested components
Basic Usage
Component API
Slot.Root
Merges all props into the first valid React element child.React.ReactNode
required
Single React element to merge props into. Multiple children or non-elements will return null
React.Ref<HTMLElement>
Ref to forward. Will be composed with child’s ref if it exists
HTMLAttributes
Any props to merge into the child element (className, onClick, style, etc.)
Slot.Slottable
Marks content that should be preserved when using asChild pattern.React.ReactNode
required
Content to preserve from the child element when slotting
Examples
Polymorphic Button Component
Merging Event Handlers
Complex Icon Button
Tooltip Trigger
Ref Forwarding
Dialog Trigger Pattern
Comparison to Native Patterns
Without Slot (Wrapper Element)
With Slot (Flexible Composition)
Common Use Cases
Design System Components
Navigation Items
Accessible Disclosure
The Slot component automatically merges refs using
composeRefs and props using mergeProps, ensuring event handlers and other props are properly combined.Implementation Details
- Props are merged using
mergePropsutility (className concatenation, event handler composition) - Refs are composed using
composeRefsto support multiple ref assignments Slot.Slottablemarks content to preserve from the child when using asChild- Fragments are supported but their ref will not be forwarded