Skip to content

Commit e02bc42

Browse files
committed
fix(overlays): fire show/hide event on shown changed
1 parent 43409df commit e02bc42

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

packages/overlays/src/LocalOverlayController.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ async function __preloadPopper() {
77
}
88
export class LocalOverlayController {
99
constructor(params = {}) {
10+
this.__fakeExtendsEventTarget();
11+
1012
// TODO: Instead of in constructor, prefetch it or use a preloader-manager to load it during idle time
1113
this.constructor.popperModule = __preloadPopper();
1214
this.__mergePopperConfigs(params.popperConfig || {});
@@ -165,11 +167,19 @@ export class LocalOverlayController {
165167
if (this.trapsKeyboardFocus) this._setupTrapsKeyboardFocus();
166168
if (this.hidesOnOutsideClick) this._setupHidesOnOutsideClick();
167169
if (this.hidesOnEsc) this._setupHidesOnEsc();
170+
171+
if (this._prevShown === false) {
172+
this.dispatchEvent(new Event('show'));
173+
}
168174
} else {
169175
this._updateContent();
170176
this.invokerNode.setAttribute('aria-expanded', false);
171177
if (this.hidesOnOutsideClick) this._teardownHidesOnOutsideClick();
172178
if (this.hidesOnEsc) this._teardownHidesOnEsc();
179+
180+
if (this._prevShown === true) {
181+
this.dispatchEvent(new Event('hide'));
182+
}
173183
}
174184
this._prevShown = shown;
175185
this._prevData = data;
@@ -301,4 +311,13 @@ export class LocalOverlayController {
301311
...this.popperConfig,
302312
});
303313
}
314+
315+
// TODO: this method has to be removed when EventTarget polyfill is available on IE11
316+
// issue: https://gitlab.ing.net/TheGuideComponents/lion-element/issues/12
317+
__fakeExtendsEventTarget() {
318+
const delegate = document.createDocumentFragment();
319+
['addEventListener', 'dispatchEvent', 'removeEventListener'].forEach(funcName => {
320+
this[funcName] = (...args) => delegate[funcName](...args);
321+
});
322+
}
304323
}

packages/overlays/test/LocalOverlayController.test.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect, fixture, html, aTimeout, defineCE, unsafeStatic } from '@open-wc/testing';
2+
import sinon from 'sinon';
23
import Popper from 'popper.js/dist/popper.min.js';
34

45
import { keyUpOn } from '@polymer/iron-test-helpers/mock-interactions.js';
@@ -898,4 +899,27 @@ describe('LocalOverlayController', () => {
898899
expect(ctrl.isShown).to.equal(true);
899900
});
900901
});
902+
903+
describe('events', () => {
904+
it('fires "show" event once overlay becomes shown', async () => {
905+
const showSpy = sinon.spy();
906+
const ctrl = new LocalOverlayController();
907+
ctrl.addEventListener('show', showSpy);
908+
await ctrl.show();
909+
expect(showSpy.callCount).to.equal(1);
910+
await ctrl.show();
911+
expect(showSpy.callCount).to.equal(1);
912+
});
913+
914+
it('fires "hide" event once overlay becomes hidden', async () => {
915+
const hideSpy = sinon.spy();
916+
const ctrl = new LocalOverlayController();
917+
ctrl.addEventListener('hide', hideSpy);
918+
ctrl.hide();
919+
expect(hideSpy.callCount).to.equal(0);
920+
await ctrl.show();
921+
ctrl.hide();
922+
expect(hideSpy.callCount).to.equal(1);
923+
});
924+
});
901925
});

0 commit comments

Comments
 (0)