From 80a62bf74266744f38c1f863d51ada040a6708e3 Mon Sep 17 00:00:00 2001 From: "Nichols, Kieran" Date: Wed, 6 Dec 2023 19:47:01 -0500 Subject: [PATCH] fix(state-layer): ensure all listeners are always remove from the previous target element regardless of attached state --- src/lib/state-layer/state-layer-foundation.ts | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/lib/state-layer/state-layer-foundation.ts b/src/lib/state-layer/state-layer-foundation.ts index 0bf574ef5..4a1e0254e 100644 --- a/src/lib/state-layer/state-layer-foundation.ts +++ b/src/lib/state-layer/state-layer-foundation.ts @@ -249,10 +249,10 @@ export class StateLayerFoundation implements IStateLayerFoundation { } public set targetElement(el: HTMLElement | null) { // Always remove the listeners from the previous target element - if (this._attached) { - this._removeListeners(); - } else { - // If unattached destroy the defer listener to recreate on the new target element + this._removeListeners(); + + // If unattached destroy the defer listener to recreate on the new target element + if (!this._attached) { this._adapter.destroy(); this._deferred = false; } @@ -273,9 +273,10 @@ export class StateLayerFoundation implements IStateLayerFoundation { this._target = value; if (this._adapter.isConnected) { - if (this._attached) { - this._removeListeners(); - } else { + // Always remove the listeners from the previous target element + this._removeListeners(); + + if (!this._attached) { this._adapter.destroy(); this._deferred = false; }