Skip to content

Commit

Permalink
fix(overlays): rework setup and teardown logic of OverlayMixin
Browse files Browse the repository at this point in the history
  • Loading branch information
tlouisse committed Jan 7, 2025
1 parent fef94cd commit f4af9e6
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 32 deletions.
2 changes: 1 addition & 1 deletion .changeset/smooth-boats-argue.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
'@lion/ui': patch
---

overlayController teardown cleanup (removes logs in test files)
[overlays]: overlayController teardown cleanup (removes logs in test files)
5 changes: 5 additions & 0 deletions .changeset/tricky-suns-change.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ui': patch
---

[overlays]: allow Subclassers to teardown at the right moment
5 changes: 5 additions & 0 deletions .changeset/tricky-suns-changerz.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@lion/ui': minor
---

[overlays]: rework setup and teardown logic of OverlayMixin (allow to reconnect offline dom nodes; allow moving dom nodes in performant way)
2 changes: 2 additions & 0 deletions packages/ui/components/combobox/test/lion-combobox.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,8 @@ describe('lion-combobox', () => {
</lion-combobox>
`)
);
await el.updateComplete;

const { _inputNode } = getComboboxMembers(el);

_inputNode.dispatchEvent(new KeyboardEvent('keyup', { key: 'Escape' }));
Expand Down
21 changes: 10 additions & 11 deletions packages/ui/components/overlays/src/OverlayController.js
Original file line number Diff line number Diff line change
Expand Up @@ -1003,16 +1003,16 @@ export class OverlayController extends EventTarget {
* @param {{ phase: OverlayPhase }} config
*/
_handleVisibilityTriggers({ phase }) {
if (typeof this.visibilityTriggerFunction !== 'function') return;

if (phase === 'init') {
this.__visibilityTriggerHandler = this.visibilityTriggerFunction({
phase,
controller: this,
});
}
if (this.__visibilityTriggerHandler[phase]) {
this.__visibilityTriggerHandler[phase]();
if (typeof this.visibilityTriggerFunction === 'function') {
if (phase === 'init') {
this.__visibilityTriggerHandler = this.visibilityTriggerFunction({
phase,
controller: this,
});
}
if (this.__visibilityTriggerHandler[phase]) {
this.__visibilityTriggerHandler[phase]();
}
}
}

Expand Down Expand Up @@ -1192,7 +1192,6 @@ export class OverlayController extends EventTarget {
event.composedPath().includes(this.contentNode) ||
deepContains(this.contentNode, /** @type {HTMLElement|ShadowRoot} */ (event.target));
if (hasPressedInside) return;

this.hide();
};

Expand Down
34 changes: 20 additions & 14 deletions packages/ui/components/overlays/src/OverlayMixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ export const OverlayMixinImplementation = superclass =>
};
}

#hasSetup = false;

constructor() {
super();
/**
Expand Down Expand Up @@ -173,22 +175,23 @@ export const OverlayMixinImplementation = superclass =>
}
}

/**
* @param {import('lit-element').PropertyValues } changedProperties
*/
firstUpdated(changedProperties) {
super.firstUpdated(changedProperties);
connectedCallback() {
super.connectedCallback();

this._setupOverlayCtrl();
this.updateComplete.then(() => {
if (this.#hasSetup) return;
this._setupOverlayCtrl();
this.#hasSetup = true;
});
}

async disconnectedCallback() {
super.disconnectedCallback();

if (!this._overlayCtrl) return;
if (await this.#isMovingInDom()) return;

this._teardownOverlayCtrl();
if (await this._isPermanentlyDisconnected()) {
this._teardownOverlayCtrl();
this.#hasSetup = false;
}
}

/**
Expand Down Expand Up @@ -238,6 +241,8 @@ export const OverlayMixinImplementation = superclass =>

/** @protected */
_setupOverlayCtrl() {
if (this._overlayCtrl) return;

/** @type {OverlayController} */
this._overlayCtrl = this._defineOverlay({
contentNode: this._overlayContentNode,
Expand All @@ -253,11 +258,12 @@ export const OverlayMixinImplementation = superclass =>

/** @protected */
_teardownOverlayCtrl() {
if (!this._overlayCtrl) return;

this._teardownOpenCloseListeners();
this.__teardownSyncFromOverlayController();

/** @type {OverlayController} */
(this._overlayCtrl).teardown();
/** @type {OverlayController} */ (this._overlayCtrl).teardown();
}

/**
Expand Down Expand Up @@ -396,9 +402,9 @@ export const OverlayMixinImplementation = superclass =>
* Before we decide to teardown, let's wait to see if we were not just moving nodes around.
* @return {Promise<boolean>}
*/
async #isMovingInDom() {
async _isPermanentlyDisconnected() {
await this.updateComplete;
return this.isConnected;
return !this.isConnected;
}
};
export const OverlayMixin = dedupeMixin(OverlayMixinImplementation);
138 changes: 133 additions & 5 deletions packages/ui/components/overlays/test-suites/OverlayMixin.suite.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
import { expect, fixture, html, nextFrame, aTimeout } from '@open-wc/testing';

import sinon from 'sinon';
import {
unsafeStatic,
nextFrame,
aTimeout,
defineCE,
fixture,
expect,
html,
} from '@open-wc/testing';
import { overlays as overlaysManager, OverlayController } from '@lion/ui/overlays.js';

import '@lion/ui/define/lion-dialog.js';
import { browserDetection } from '@lion/ui/core.js';
import { cache } from 'lit/directives/cache.js';
import '@lion/ui/define/lion-dialog.js';
import { LitElement } from 'lit';
import sinon from 'sinon';

/**
* @typedef {import('../types/OverlayConfig.js').OverlayConfig} OverlayConfig
Expand Down Expand Up @@ -331,6 +339,125 @@ export function runOverlayMixinSuite({ tagString, tag, suffix = '' }) {
}).to.not.throw;
stub.restore();
});

it('does not teardown when moving dom nodes', async () => {
const el = /** @type {OverlayEl} */ (
await fixture(html`
<${tag}>
<div slot="content">content</div>
<button slot="invoker">invoker button</button>
</${tag}>
`)
);
// @ts-expect-error [allow-protected] in tests
const teardownSpy = sinon.spy(el, '_teardownOverlayCtrl');
// @ts-expect-error [allow-protected] in tests
const spyDisconnected = sinon.spy(el, '_isPermanentlyDisconnected');

// move around in dom
const newParent = document.createElement('div');
document.body.appendChild(newParent);
newParent.appendChild(el);
await aTimeout(0);

expect(spyDisconnected.callCount).to.equal(1);
expect(teardownSpy.callCount).to.equal(0);

// simulate a permanent disconnect
newParent.removeChild(el);
await aTimeout(0);

expect(teardownSpy.callCount).to.equal(1);
});

it('allows to disconnect and reconnect later', async () => {
const el = /** @type {OverlayEl} */ (
await fixture(html`
<${tag}>
<div slot="content">content</div>
<button slot="invoker">invoker button</button>
</${tag}>
`)
);
// @ts-expect-error [allow-protected] in tests
const teardownSpy = sinon.spy(el, '_teardownOverlayCtrl');
// @ts-expect-error [allow-protected] in tests
const spyDisconnected = sinon.spy(el, '_isPermanentlyDisconnected');
// @ts-expect-error [allow-protected] in tests
const setupSpy = sinon.spy(el, '_setupOverlayCtrl');

// 'permanently' disconnect (although we will reconnect later)
const offlineParent = document.createElement('div');
offlineParent.appendChild(el);
await aTimeout(0);

expect(spyDisconnected.callCount).to.equal(1);
expect(teardownSpy.callCount).to.equal(1);

// reconnect
document.body.appendChild(offlineParent);
await aTimeout(0);

expect(setupSpy.callCount).to.equal(1);
});

it('works with cache directive when reconnected', async () => {
class CachingContext extends LitElement {
static properties = {
switched: { type: Boolean },
};

constructor() {
super();

this.switched = false;
}

get overlayEl() {
return this.shadowRoot?.querySelector('#myOverlay');
}

render() {
return html`${cache(
this.switched
? html`something else`
: html`<${tag} id="myOverlay">
<div slot="content">content b</div>
<button slot="invoker">invoker button b</button>
</${tag}>`,
)}`;
}
}
const cachingTagName = defineCE(CachingContext);
const cachingTag = unsafeStatic(cachingTagName);

const el = /** @type {CachingContext} */ (
await fixture(html`
<${cachingTag}></${cachingTag}>
`)
);
// @ts-expect-error [allow-protected] in tests
const teardownSpy = sinon.spy(el.overlayEl, '_teardownOverlayCtrl');
// @ts-expect-error [allow-protected] in tests
const spyDisconnected = sinon.spy(el.overlayEl, '_isPermanentlyDisconnected');
// @ts-expect-error [allow-protected] in tests
const setupSpy = sinon.spy(el.overlayEl, '_setupOverlayCtrl');

el.switched = true;
// render the new content
await el.updateComplete;
// wait for the teardown to complete
await el.updateComplete;

expect(spyDisconnected.callCount).to.equal(1);
expect(teardownSpy.callCount).to.equal(1);
expect(setupSpy.callCount).to.equal(0);

el.switched = false;
await el.updateComplete;

expect(setupSpy.callCount).to.equal(1);
});
});

describe(`OverlayMixin${suffix} nested`, () => {
Expand Down Expand Up @@ -388,6 +515,7 @@ export function runOverlayMixinSuite({ tagString, tag, suffix = '' }) {
const moveTarget = /** @type {OverlayEl} */ (await fixture('<div id="target"></div>'));
moveTarget.appendChild(el);
await el.updateComplete;

expect(getGlobalOverlayCtrls().length).to.equal(1);
}
});
Expand Down
5 changes: 4 additions & 1 deletion packages/ui/components/select-rich/src/LionSelectRich.js
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,8 @@ export class LionSelectRich extends SlotMixin(ScopedElementsMixin(OverlayMixin(L
*/
_teardownOverlayCtrl() {
super._teardownOverlayCtrl();
if (!this._overlayCtrl) return;

this._overlayCtrl.removeEventListener('show', this.__overlayOnShow);
this._overlayCtrl.removeEventListener('before-show', this.__overlayBeforeShow);
this._overlayCtrl.removeEventListener('hide', this.__overlayOnHide);
Expand All @@ -434,11 +436,12 @@ export class LionSelectRich extends SlotMixin(ScopedElementsMixin(OverlayMixin(L
* @protected
*/
async _alignInvokerWidth() {
await this.updateComplete;

if (!this._overlayCtrl?.content) {
return;
}

await this.updateComplete;
const initContentDisplay = this._overlayCtrl.content.style.display;
const initContentMinWidth = this._overlayCtrl.contentWrapperNode.style.minWidth;
const initContentWidth = this._overlayCtrl.contentWrapperNode.style.width;
Expand Down

0 comments on commit f4af9e6

Please sign in to comment.