Portal

Renders content in a different part of the DOM, outside its parent.

Overlays like dialogs, popovers, menus and tooltips are usually written inside the component that opens them, but they need to appear above the rest of the page. A parent with overflow: hidden, or one that creates a stacking context (through transform, filter, opacity, contain, isolation or perspective), can clip an overlay rendered inline or trap it under other content.

A portal moves the rendered nodes to another container, document.body by default, while keeping them in the same component tree. Context, state and events still flow as if the content were rendered in place, so the parts inside a portal keep talking to their root.

When to use it

Wrap the positioner (or backdrop and positioner) of an overlay in a portal when:

  • an ancestor clips its children with overflow
  • an ancestor creates a stacking context that z-index can't escape
  • the overlay should sit above the whole page, like a modal dialog

Inline rendering is fine when none of these apply, for example a popover inside a toolbar with no clipping.

Usage

import { Dialog } from '@ark-ui/react/dialog'
import { Portal } from '@ark-ui/react/portal'

export const Demo = () => (
  <Dialog.Root>
    <Dialog.Trigger>Open</Dialog.Trigger>
    <Portal>
      <Dialog.Backdrop />
      <Dialog.Positioner>
        <Dialog.Content>...</Dialog.Content>
      </Dialog.Positioner>
    </Portal>
  </Dialog.Root>
)

Portal is built on React's createPortal and adds:

  • Server rendering: on the server, and on the first client render while hydrating, children render in place, so the markup matches. They move into the container once the client takes over.
  • Shadow DOM: inside an EnvironmentProvider whose root node is in a shadow root, content renders into that shadow root instead of document.body, so it keeps the shadow root's styles.
  • Container: pass a ref as container to render somewhere other than the body.
  • Disabled: set disabled to render children in place.
const container = useRef<HTMLDivElement>(null)

<Portal container={container}>...</Portal>
<div ref={container} />

Styling

Portalled content leaves its parent, so styles that depend on the parent, like descendant selectors or inherited CSS variables, no longer apply. Style the parts directly, or set the variables on a shared ancestor such as :root.

API Reference

Portal

PropDefaultType
container
RefObject<HTMLElement | null>

disabled
boolean