Skip to content

Commit db9b3d3

Browse files
authored
fix: do not reopen popover when clicking target (#9979)
1 parent 325e4a0 commit db9b3d3

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

packages/popover/src/vaadin-popover-overlay.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,20 @@ class PopoverOverlay extends PopoverOverlayMixin(
8686
_shouldAddGlobalListeners() {
8787
return true;
8888
}
89+
90+
/**
91+
* Override method from `OverlayMixin` to prevent closing when clicking on target.
92+
* Clicking the target will already close the popover when using the click trigger.
93+
*
94+
* @override
95+
* @protected
96+
*/
97+
_shouldCloseOnOutsideClick(event) {
98+
if (event.composedPath().includes(this.positionTarget)) {
99+
return false;
100+
}
101+
return super._shouldCloseOnOutsideClick(event);
102+
}
89103
}
90104

91105
defineCustomElement(PopoverOverlay);

packages/popover/test/basic.test.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from '@vaadin/chai-plugins';
2+
import { resetMouse, sendMouseToElement } from '@vaadin/test-runner-commands';
23
import { esc, fixtureSync, nextRender, nextUpdate, oneEvent, outsideClick } from '@vaadin/testing-helpers';
34
import sinon from 'sinon';
45
import '../src/vaadin-popover.js';
@@ -251,6 +252,10 @@ describe('popover', () => {
251252
await nextUpdate(popover);
252253
});
253254

255+
afterEach(async () => {
256+
await resetMouse();
257+
});
258+
254259
it('should open overlay on target click by default', async () => {
255260
target.click();
256261
await oneEvent(overlay, 'vaadin-overlay-open');
@@ -261,7 +266,10 @@ describe('popover', () => {
261266
target.click();
262267
await oneEvent(overlay, 'vaadin-overlay-open');
263268

264-
target.click();
269+
// Use browser command here to test for possible side effects between the outside click listener and the
270+
// target opened toggle behavior. Using browser commands for opening the popover doesn't work consistently as
271+
// the opened event might fire before the click promise resolves.
272+
await sendMouseToElement({ type: 'click', element: target });
265273
await nextRender();
266274
expect(overlay.opened).to.be.false;
267275
});

0 commit comments

Comments
 (0)