Overview
TheSlot component merges its props with its child element, enabling prop forwarding and composition patterns. It’s useful for creating flexible, composable components that don’t add extra DOM nodes.
Import
Component Parts
Slot.Root
Merges props into the child element
Slot.Slottable
Marks content as the target for slotting
Slot.Root
Props
React.ReactElement
required
A single React element to merge props into. Must be a valid React element.
React.Ref<HTMLElement>
Ref to be composed with the child’s ref.
Slot.Slottable
Props
React.ReactNode
required
The content to be slotted. Must be a single React element.
Usage Examples
Basic Prop Merging
Ref Composition
Event Handler Merging
Using Slottable
Creating Polymorphic Components
Complex Composition
Style Merging
With forwardRef
Notes
- The component only accepts a single child element (enforced via
Children.only) - Props are merged using a custom
mergePropsutility that handles event handlers and styles intelligently - Refs are composed so both parent and child refs work correctly
- When the child is a Fragment, ref composition is skipped
Slot.Slottablehelps preserve additional wrapper content while slotting the main child- Class names, styles, and event handlers are intelligently merged, not replaced
- Based on Radix UI’s Slot implementation