Skip to content

Commit 59caf18

Browse files
fix: do not reopen popover when clicking target (#9979) (#10398)
Co-authored-by: Sascha Ißbrücker <sissbruecker@vaadin.com>
1 parent b593e99 commit 59caf18

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
@@ -134,6 +134,20 @@ class PopoverOverlay extends PopoverOverlayMixin(DirMixin(ThemableMixin(PolylitM
134134
_shouldAddGlobalListeners() {
135135
return true;
136136
}
137+
138+
/**
139+
* Override method from `OverlayMixin` to prevent closing when clicking on target.
140+
* Clicking the target will already close the popover when using the click trigger.
141+
*
142+
* @override
143+
* @protected
144+
*/
145+
_shouldCloseOnOutsideClick(event) {
146+
if (event.composedPath().includes(this.positionTarget)) {
147+
return false;
148+
}
149+
return super._shouldCloseOnOutsideClick(event);
150+
}
137151
}
138152

139153
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';
@@ -246,6 +247,10 @@ describe('popover', () => {
246247
await nextUpdate(popover);
247248
});
248249

250+
afterEach(async () => {
251+
await resetMouse();
252+
});
253+
249254
it('should open overlay on target click by default', async () => {
250255
target.click();
251256
await oneEvent(overlay, 'vaadin-overlay-open');
@@ -256,7 +261,10 @@ describe('popover', () => {
256261
target.click();
257262
await oneEvent(overlay, 'vaadin-overlay-open');
258263

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

0 commit comments

Comments
 (0)