diff --git a/src/portal.ts b/src/portal.ts index 7044f65..78c2b67 100644 --- a/src/portal.ts +++ b/src/portal.ts @@ -88,7 +88,7 @@ export class Portal { public detached() { this.isAttached = false; - this.view.detached(); + this.viewSlot!.detached(); } public unbind() { @@ -114,10 +114,8 @@ export class Portal { const target = this.getTarget(); const oldViewSlot = this.viewSlot; if (oldViewSlot) { - oldViewSlot.removeAt(0, false, false); - if (this.isAttached) { - view.detached(); - } + oldViewSlot.remove(view); + this.viewSlot = null; } if (this.isAttached) { const viewSlot = this.viewSlot = new ViewSlot(target!, true); diff --git a/test/unit/portal.spec.ts b/test/unit/portal.spec.ts index da72e0f..3ca0a5a 100644 --- a/test/unit/portal.spec.ts +++ b/test/unit/portal.spec.ts @@ -155,6 +155,20 @@ describe('Portal attribute', () => { expect(document.querySelector('.square3 form')).not.toBeFalsy(); }); + it('does not render when not attached', () => { + portal.bind(bindingContext, overrideContext); + + portal.targetChanged(); + + expect(document.querySelector('form')).toBe(null); + expect((portal as any).viewSlot).toBe(undefined); + + portal.attached(); + expect(document.querySelector('form')).not.toBeFalsy(); + + portal.detached(); + }); + it('throws in strict mode', () => { portal.strict = true; portal.target = '.square4';