From 09a547c3d4974474e458644f5fdf91d63835471a Mon Sep 17 00:00:00 2001 From: Gouvernathor <44340603+Gouvernathor@users.noreply.github.com> Date: Thu, 29 Oct 2026 21:59:48 +0100 Subject: [PATCH] feat(cdk/portal): allow detached DOM nodes to be used in DomPortal --- src/cdk/portal/dom-portal-outlet.ts | 27 +++++++++++++-------------- 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/cdk/portal/dom-portal-outlet.ts b/src/cdk/portal/dom-portal-outlet.ts index 880f1eea5a35..64c6bb0de22f 100644 --- a/src/cdk/portal/dom-portal-outlet.ts +++ b/src/cdk/portal/dom-portal-outlet.ts @@ -141,24 +141,23 @@ export class DomPortalOutlet extends BasePortalOutlet { */ override attachDomPortal = (portal: DomPortal) => { const element = portal.element; - if (!element.parentNode && (typeof ngDevMode === 'undefined' || ngDevMode)) { - throw Error('DOM portal content must be attached to a parent node.'); + if (element.parentNode) { + // Anchor used to save the element's previous position so + // that we can restore it when the portal is detached. + const anchorNode = this.outletElement.ownerDocument.createComment('dom-portal'); + + element.parentNode!.insertBefore(anchorNode, element); + + super.setDisposeFn(() => { + // We can't use `replaceWith` here because IE doesn't support it. + if (anchorNode.parentNode) { + anchorNode.parentNode.replaceChild(element, anchorNode); + } + }); } - // Anchor used to save the element's previous position so - // that we can restore it when the portal is detached. - const anchorNode = this.outletElement.ownerDocument.createComment('dom-portal'); - - element.parentNode!.insertBefore(anchorNode, element); this.outletElement.appendChild(element); this._attachedPortal = portal; - - super.setDisposeFn(() => { - // We can't use `replaceWith` here because IE doesn't support it. - if (anchorNode.parentNode) { - anchorNode.parentNode.replaceChild(element, anchorNode); - } - }); }; /**