Crustack

@crustack/portal

Generate a Portal component with defaults.

Usage

Generate a Portal component that renders content into specified DOM elements. Use the defaultRoots option to define target elements for rendering. When no target is found, content falls back to rendering in the document body.

import {  } from '@crustack/portal'

export const {  } = ({
  // Try to render in a <dialog> element if any, otherwise in #root, or fallback to the body
  : ['dialog', '#root'],
})

Wrap your component in the Portal.


function () {
  return (
    <>
      <>This content will render in the first available default root</>
    </>
  )
}

Use a different root element for this portal:


function () {
  return (
    < ="#root-element">
      <>This content will render in the first available default root</>
    </>
  )
}

API Reference

👉 definePortal

Create a custom Portal component.

DefinePortalOptions

PropTypeDefault
defaultRoots?
Root[]
-

👉 Portal

The component returned by definePortal. It renders its children into the specified root.

PortalProps

PropTypeDefault
root?
Root
document.body
children
ReactNode
-

On this page