Overview
TheTeleport component (also known as Portal) renders its children into a different part of the DOM tree while maintaining the React component hierarchy. This is useful for modals, tooltips, dropdowns, and other overlay components.
Import
Props
React.ReactNode
required
The content to render in the portal.
string | HTMLElement | React.RefObject<HTMLElement> | ValidHtmlTags | null
required
The destination for the portal. Can be:
- A CSS selector string (e.g., “#modal-root”)
- An HTML element
- A React ref object
- An HTML tag name (e.g., “body”)
nullto disable the portal
InsertPosition
Where to insert the content relative to the destination element. Options:
"beforebegin"- Before the element"afterbegin"- Inside, before first child"beforeend"- Inside, after last child"afterend"- After the element
Type Definitions
Usage Examples
Basic Portal to Body
Portal to Specific Element
Using a Ref
With Insert Position
Conditional Portal
Multiple Portals
Tooltip with Portal
Modal Stack
Insert at Specific Position
Client-Side Only
Notes
- The component uses
ClientGateinternally, making it safe for server-side rendering - When
insertPositionis used, a temporary wrapper div is created and then unwrapped - The wrapper div has
display: contentsto avoid affecting layout - Content is portaled using React’s
createPortalAPI - The portal maintains React context and event bubbling through the component tree
- When
toisnull, the portal is disabled and nothing is rendered - CSS selector strings are resolved using
document.querySelector - The component cleans up the temporary wrapper on unmount