PortalController
Moves a node into document.body, or a container, and carries the origin theme with it.
Reach for it for a surface that must escape clipping or stacking contexts.
Import
import { PortalController, createPortalController } from '@ecopages/radiant-ui/controllers';Try it
Each press of the button attaches or destroys the controller. While attached the node is a child of body; destroyed, it is back among its siblings.
Parent: div
<div data-example="portal">
<div data-box>Portaled node</div>
<button type="button">Toggle portal</button>
<p data-status>Parent: div</p>
</div>import { PortalController } from '@ecopages/radiant-ui/controllers';
export function mount(root: HTMLElement): () => void {
const panel = root.querySelector<HTMLElement>('[data-box]')!;
const toggle = root.querySelector('button')!;
const status = root.querySelector('[data-status]')!;
const portal = new PortalController({ getNode: () => panel });
toggle.addEventListener('click', () => {
if (portal.isMounted) {
portal.destroy();
} else {
portal.attach();
}
status.textContent = `Parent: ${panel.parentElement!.tagName.toLowerCase()}`;
});
return () => portal.destroy();
}mount runs once for each example element on the page, and the function it returns undoes it; see how the examples run.
Config
| Option | Description |
|---|---|
getNode | The node to move. Resolved on every attach(); null leaves nothing mounted. |
getContainer | Where the node goes. Resolved on every attach(); document.body when omitted. |
getDirectionSource | The element whose direction the node takes, such as the anchor of a popover that sits elsewhere. Resolved on every attach(); the node's own place when omitted or null. |
Lifecycle
attach() leaves a comment where the node was and appends the node to the container. destroy() restores the original position among its siblings, skipping the move when something removed the comment in the meantime. A node without a parent is not mounted. Attaching again with the same node, or destroying twice, changes nothing. Attaching again after getNode returns another node puts the old one back and mounts the new one, and a null unmounts. isMounted tells which state it is in, and mountedNode is the node it holds out right now, or null. A node that leaves its ancestors loses what they set, so attach() also carries the dark class, the data-theme attribute and the inline --rui-* custom properties of its ancestors onto the node, and destroy() removes them again. It sets the color-scheme the node resolved in its old place inline when the container resolves another one, so a surface opened from a color-scheme: dark region of a light page keeps the dark theme colors. When an ancestor has a dir attribute, the node also gets the direction of its old place, or of getDirectionSource(), as dir="rtl" or dir="ltr", so a surface opened from a right-to-left section lays out right to left. The controller remembers where each portaled node came from, so DismissController can treat a node inside a portaled root as inside the surface the root came from. While a node is mounted it counts as inside whatever its origin was inside, which is how DismissController and FocusWithinController treat a portaled popup as part of its owner.
With a host, createPortalController(host, config) does the same on connect and disconnect; see bindController.
Accessibility: what the controller does and what you must provide
Moving a node to the end of document.body changes its place in the tab order and the accessibility tree: Tab from the trigger does not reach a portaled popup, which comes after everything else. Wire the trigger to the popup yourself (aria-controls, aria-expanded, aria-haspopup), and for a non-modal popup that holds interactive content either keep it in place with portal: false or move focus into it on open and back on close. A modal surface needs role="dialog", aria-modal="true" and a label.
Moving a node removes and re-inserts it, so the connectedCallback of any custom element inside it runs again. Keep state those elements need in the owner, not in their connect callbacks.
Protected hooks
None. Change behavior through the config callbacks.