From 412270fa1a6752c75d459c430c2b9d8cd302e3c8 Mon Sep 17 00:00:00 2001 From: Thijs Louisse Date: Thu, 18 Mar 2021 11:35:37 +0100 Subject: [PATCH] fix(overlays): enhanced and documented closeOnOutsideClick Co-authored-by: Konstantinos Norgias --- .changeset/forty-taxis-lay.md | 5 + docs/docs/systems/overlays/configuration.md | 3 +- .../test/lion-input-datepicker.test.js | 5 +- packages/overlays/package.json | 1 + packages/overlays/src/OverlayController.js | 85 ++++++++++------ packages/overlays/test-helpers.js | 1 + packages/overlays/test-helpers/mimicClick.js | 21 ++++ .../overlays/test/OverlayController.test.js | 96 ++++++++++++------- .../select-rich/test/lion-select-rich.test.js | 6 +- 9 files changed, 158 insertions(+), 65 deletions(-) create mode 100644 .changeset/forty-taxis-lay.md create mode 100644 packages/overlays/test-helpers.js create mode 100644 packages/overlays/test-helpers/mimicClick.js diff --git a/.changeset/forty-taxis-lay.md b/.changeset/forty-taxis-lay.md new file mode 100644 index 000000000..878ac845c --- /dev/null +++ b/.changeset/forty-taxis-lay.md @@ -0,0 +1,5 @@ +--- +'@lion/overlays': patch +--- + +fix: only close an overlay if both mousedown and mousep events are outside (for hidesOnOutsideClick) diff --git a/docs/docs/systems/overlays/configuration.md b/docs/docs/systems/overlays/configuration.md index 9061a191d..db3db7776 100644 --- a/docs/docs/systems/overlays/configuration.md +++ b/docs/docs/systems/overlays/configuration.md @@ -167,7 +167,8 @@ export const hidesOnOutsideClick = () => {
- Hello! You can close this notification here: + + ')); const contentNode = /** @type {HTMLElement} */ (await fixture('
Content
')); @@ -548,22 +596,14 @@ describe('OverlayController', () => { contentNode, invokerNode, }); + const stopProp = (/** @type {Event} */ e) => e.stopPropagation(); const dom = await fixture( - /** - * @param {{ stopPropagation: () => any; }} e - */ `
-
- +
This element prevents our handlers from reaching the document click handler. - +
`, ); @@ -571,8 +611,9 @@ describe('OverlayController', () => { await ctrl.show(); expect(ctrl.isShown).to.equal(true); - /** @type {HTMLElement} */ - (dom.querySelector('third-party-noise')).click(); + const noiseEl = /** @type {HTMLElement} */ (dom.querySelector('#third-party-noise')); + + mimicClick(noiseEl); await aTimeout(0); expect(ctrl.isShown).to.equal(false); @@ -592,35 +633,26 @@ describe('OverlayController', () => { contentNode, invokerNode, }); + const stopProp = (/** @type {Event} */ e) => e.stopPropagation(); const dom = /** @type {HTMLElement} */ (await fixture(`
-
- +
This element prevents our handlers from reaching the document click handler. - +
`)); - /** @type {HTMLElement} */ - (dom.querySelector('third-party-noise')).addEventListener( - 'click', - (/** @type {Event} */ event) => { - event.stopPropagation(); - }, - true, - ); + const noiseEl = /** @type {HTMLElement} */ (dom.querySelector('#third-party-noise')); + + noiseEl.addEventListener('click', stopProp, true); + noiseEl.addEventListener('mousedown', stopProp, true); + noiseEl.addEventListener('mouseup', stopProp, true); await ctrl.show(); expect(ctrl.isShown).to.equal(true); - /** @type {HTMLElement} */ - (dom.querySelector('third-party-noise')).click(); + mimicClick(noiseEl); await aTimeout(0); expect(ctrl.isShown).to.equal(false); diff --git a/packages/select-rich/test/lion-select-rich.test.js b/packages/select-rich/test/lion-select-rich.test.js index 000e236f5..33c2c3fb0 100644 --- a/packages/select-rich/test/lion-select-rich.test.js +++ b/packages/select-rich/test/lion-select-rich.test.js @@ -2,6 +2,7 @@ import { LitElement } from '@lion/core'; import { renderLitAsNode } from '@lion/helpers'; import { OverlayController } from '@lion/overlays'; import { LionOption } from '@lion/listbox'; +import { mimicClick } from '@lion/overlays/test-helpers'; import { aTimeout, defineCE, @@ -253,9 +254,8 @@ describe('lion-select-rich', () => { )); expect(el.opened).to.be.true; - // a click on the button will trigger hide on outside click - // which we then need to sync back to "opened" - outerEl.click(); + + mimicClick(outerEl); await aTimeout(0); expect(el.opened).to.be.false; });