Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close popover with hover trigger on target mousedown #8216

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions packages/popover/src/vaadin-popover.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,6 +453,7 @@ class Popover extends PopoverPositionMixin(
this.__onTargetClick = this.__onTargetClick.bind(this);
this.__onTargetFocusIn = this.__onTargetFocusIn.bind(this);
this.__onTargetFocusOut = this.__onTargetFocusOut.bind(this);
this.__onTargetMouseDown = this.__onTargetMouseDown.bind(this);
this.__onTargetMouseEnter = this.__onTargetMouseEnter.bind(this);
this.__onTargetMouseLeave = this.__onTargetMouseLeave.bind(this);

Expand Down Expand Up @@ -550,6 +551,7 @@ class Popover extends PopoverPositionMixin(
target.addEventListener('click', this.__onTargetClick);
target.addEventListener('mouseenter', this.__onTargetMouseEnter);
target.addEventListener('mouseleave', this.__onTargetMouseLeave);
target.addEventListener('mousedown', this.__onTargetMouseDown);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't solve the issue if the dialog is opened with keyboard.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, I didn't consider this case. Maybe we should close it on click from keyboard even if the popover doesn't have click trigger.

target.addEventListener('focusin', this.__onTargetFocusIn);
target.addEventListener('focusout', this.__onTargetFocusOut);
}
Expand All @@ -563,6 +565,7 @@ class Popover extends PopoverPositionMixin(
target.removeEventListener('click', this.__onTargetClick);
target.removeEventListener('mouseenter', this.__onTargetMouseEnter);
target.removeEventListener('mouseleave', this.__onTargetMouseLeave);
target.removeEventListener('mousedown', this.__onTargetMouseDown);
target.removeEventListener('focusin', this.__onTargetFocusIn);
target.removeEventListener('focusout', this.__onTargetFocusOut);
}
Expand Down Expand Up @@ -805,6 +808,17 @@ class Popover extends PopoverPositionMixin(
this.__handleMouseLeave();
}

/** @private */
__onTargetMouseDown() {
if (this._overlayElement.opened && !isLastOverlay(this._overlayElement)) {
return;
}

if (this.__hasTrigger('hover') && !this.__hasTrigger('click')) {
this._openedStateController.close(true);
}
}

/** @private */
__onOverlayFocusIn() {
this.__focusInside = true;
Expand Down
24 changes: 24 additions & 0 deletions packages/popover/test/trigger.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ describe('trigger', () => {
expect(overlay.opened).to.be.true;
});

it('should close on target mousedown', async () => {
mouseenter(target);
await nextRender();

mousedown(target);
await nextUpdate(popover);
expect(overlay.opened).to.be.false;
});

it('should not open on target mouseenter when detached', async () => {
popover.remove();
mouseenter(target);
Expand Down Expand Up @@ -417,6 +426,21 @@ describe('trigger', () => {
});
});

describe('hover and click', () => {
beforeEach(async () => {
popover.trigger = ['hover', 'click'];
await nextUpdate(popover);
});

it('should not close on target mousedown', async () => {
mouseenter(target);
await nextRender();
mousedown(target);
await nextUpdate(popover);
expect(overlay.opened).to.be.true;
});
});

describe('focus and click', () => {
beforeEach(async () => {
popover.trigger = ['click', 'focus'];
Expand Down