Skip to content

Commit a74db59

Browse files
authored
fix: prevent tooltip click and mousedown from bubbling to target (#10268)
1 parent a6fbc28 commit a74db59

File tree

3 files changed

+39
-7
lines changed

3 files changed

+39
-7
lines changed

packages/tooltip/src/vaadin-tooltip-mixin.js

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -553,17 +553,11 @@ export const TooltipMixin = (superClass) =>
553553
}
554554

555555
/** @private */
556-
__onMouseDown(event) {
556+
__onMouseDown() {
557557
if (this.manual) {
558558
return;
559559
}
560560

561-
// Do not close on bubbling mousedown events when
562-
// the tooltip is slotted into the target element
563-
if (event.composedPath().includes(this)) {
564-
return;
565-
}
566-
567561
this._stateController.close(true);
568562
}
569563

@@ -617,6 +611,20 @@ export const TooltipMixin = (superClass) =>
617611
}
618612
}
619613

614+
/** @protected */
615+
__onOverlayMouseDown(event) {
616+
// Prevent mousedown listeners from being called when
617+
// the tooltip is slotted into the target element
618+
event.stopPropagation();
619+
}
620+
621+
/** @protected */
622+
__onOverlayClick(event) {
623+
// Prevent click listeners from being called when
624+
// the tooltip is slotted into the target element
625+
event.stopPropagation();
626+
}
627+
620628
/** @private */
621629
__handleMouseLeave() {
622630
if (this.manual) {

packages/tooltip/src/vaadin-tooltip.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,8 @@ class Tooltip extends TooltipMixin(ThemePropertyMixin(ElementMixin(PolylitMixin(
8585
?no-vertical-overlap="${this.__computeNoVerticalOverlap(effectivePosition)}"
8686
.horizontalAlign="${this.__computeHorizontalAlign(effectivePosition)}"
8787
.verticalAlign="${this.__computeVerticalAlign(effectivePosition)}"
88+
@click="${this.__onOverlayClick}"
89+
@mousedown="${this.__onOverlayMouseDown}"
8890
@mouseenter="${this.__onOverlayMouseEnter}"
8991
@mouseleave="${this.__onOverlayMouseLeave}"
9092
modeless

test/integration/component-tooltip.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { expect } from '@vaadin/chai-plugins';
22
import { resetMouse, sendKeys, sendMouseToElement } from '@vaadin/test-runner-commands';
33
import { fixtureSync, mousedown, nextRender, tabKeyDown } from '@vaadin/testing-helpers';
4+
import sinon from 'sinon';
45
import './not-animated-styles.js';
56
import { AccordionPanel } from '@vaadin/accordion/src/vaadin-accordion-panel.js';
67
import { Button } from '@vaadin/button/src/vaadin-button.js';
@@ -162,6 +163,27 @@ before(() => {
162163
expect(tooltipOverlay.opened).to.be.true;
163164
});
164165

166+
it('should not fire target click listeners on overlay click', () => {
167+
const spy = sinon.spy();
168+
tooltip.target.addEventListener('click', spy);
169+
170+
mouseenter(tooltip.target);
171+
tooltipOverlay.click();
172+
173+
expect(spy.called).to.be.false;
174+
});
175+
176+
it('should not fire target mousedown listeners on overlay mousedown', () => {
177+
const spy = sinon.spy();
178+
tooltip.target.addEventListener('mousedown', spy);
179+
180+
mouseenter(tooltip.target);
181+
const content = tooltip.querySelector('[slot="overlay"]');
182+
mousedown(content);
183+
184+
expect(spy.called).to.be.false;
185+
});
186+
165187
it('should set has-tooltip attribute on the element', () => {
166188
expect(element.hasAttribute('has-tooltip')).to.be.true;
167189
});

0 commit comments

Comments
 (0)