From 545d40b2119fc8dc740a4b8f095d02d44080a706 Mon Sep 17 00:00:00 2001 From: Samuel Degueldre Date: Wed, 19 Oct 2022 13:31:09 +0200 Subject: [PATCH] [FIX] portal: correctly mount portal content in target created by mount Previously, blah blah blah --- src/runtime/portal.ts | 20 ++++++++------------ tests/misc/portal.test.ts | 2 +- 2 files changed, 9 insertions(+), 13 deletions(-) diff --git a/src/runtime/portal.ts b/src/runtime/portal.ts index f85f3d62..cc544de2 100644 --- a/src/runtime/portal.ts +++ b/src/runtime/portal.ts @@ -21,20 +21,16 @@ class VPortal extends VText implements Partial> { this.target = document.querySelector(this.selector) as any; if (this.target) { this.content!.mount(this.target!, null); - } else { - this.content!.mount(parent, anchor); } } beforeRemove() { - this.content!.beforeRemove(); - } - remove() { - if (this.content) { - super.remove(); + // this.target not being null means content is mounted + if (this.target) { + this.content!.beforeRemove(); this.content!.remove(); - this.content = null; } + this.content = null; } patch(other: VPortal) { @@ -73,9 +69,9 @@ export class Portal extends Component { onMounted(() => { const portal: VPortal = node.bdom; if (!portal.target) { - const target: HTMLElement = document.querySelector(this.props.target); - if (target) { - portal.content!.moveBeforeVNode(target, null); + portal.target = document.querySelector(this.props.target); + if (portal.target) { + portal.content!.mount(portal.target, null); } else { throw new OwlError("invalid portal target"); } @@ -84,7 +80,7 @@ export class Portal extends Component { onWillUnmount(() => { const portal: VPortal = node.bdom; - portal.remove(); + portal.beforeRemove(); }); } } diff --git a/tests/misc/portal.test.ts b/tests/misc/portal.test.ts index 33fdbe45..b5c230b4 100644 --- a/tests/misc/portal.test.ts +++ b/tests/misc/portal.test.ts @@ -892,7 +892,7 @@ describe("Portal", () => { } await mount(Parent, fixture); expect(fixture.innerHTML).toBe( - 'child
portal' + 'child
portal
' ); });