From 3151367f3abf49a476c98b72ce5ecee9ead3d622 Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:53:34 +0800 Subject: [PATCH] Import packages/plugin/src/client/portal.ts --- packages/plugin/src/client/portal.ts | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 packages/plugin/src/client/portal.ts diff --git a/packages/plugin/src/client/portal.ts b/packages/plugin/src/client/portal.ts new file mode 100644 index 0000000..c033a0c --- /dev/null +++ b/packages/plugin/src/client/portal.ts @@ -0,0 +1,30 @@ +import type { PortalRenderer } from './types.js' + +/** + * Escape shell ancestors that establish a fixed-position containing block. + * + * Host chrome around our action buttons can carry `transform`, `filter` or + * `contain`, which turns `position: fixed` backdrops into children of that box + * (issue #222): the mask then covers the wrapper instead of the viewport and the + * `onMouseDown` outside-click test compares against the wrong `currentTarget`. + * Floating layers therefore render through `ReactDOM.createPortal` into + * `document.body`. + * + * The fallback has to stay total, because a throw here takes down the whole + * action slot: the renderer may be missing (older host, no `react-dom`), the DOM + * may be absent (SSR, unit tests, prerender), `document.body` may not exist yet + * while the shell mounts, and `createPortal` itself rejects a detached or + * non-element container. In every case the layer degrades to inline rendering + * instead of vanishing or crashing the slot. + */ +export function renderBodyPortal(portal: PortalRenderer | undefined, children: unknown): unknown { + if (typeof portal !== 'function') return children + if (typeof document === 'undefined' || document === null) return children + const body = document.body + if (body === null || body === undefined) return children + try { + return portal(children, body) + } catch { + return children + } +}