Skip to main content

Overview

The Teleport 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”)
  • null to 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

Insert at Specific Position

Client-Side Only

Notes

  • The component uses ClientGate internally, making it safe for server-side rendering
  • When insertPosition is used, a temporary wrapper div is created and then unwrapped
  • The wrapper div has display: contents to avoid affecting layout
  • Content is portaled using React’s createPortal API
  • The portal maintains React context and event bubbling through the component tree
  • When to is null, 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