Skip to content

Commit 6eb3ff3

Browse files
fix(tooltip): fix tooltip reopen when mouse leaves (#1279)
1 parent 1d093a6 commit 6eb3ff3

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

packages/elements/src/tooltip/__test__/tooltip.test.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,7 @@ describe('tooltip/Tooltip', function () {
265265
});
266266

267267
await elementUpdated(el);
268+
await nextFrame();
268269

269270
expect(tooltip.opened, 'Tooltip is not opened').to.be.true;
270271

@@ -275,6 +276,7 @@ describe('tooltip/Tooltip', function () {
275276
});
276277

277278
await elementUpdated(el);
279+
await nextFrame();
278280

279281
expect(tooltip.opened, 'Tooltip is not hidden').to.be.false;
280282
}).timeout(MouseMoveDelay * 2);

packages/elements/src/tooltip/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,20 +390,12 @@ class Tooltip extends BasicElement {
390390
* Hide tooltip
391391
* @returns {void}
392392
*/
393-
private hideTooltip(): void {
393+
private resetTooltip = (): void => {
394394
this.reset();
395395
this.matchTarget = null;
396396
this.matchTargetRect = null;
397397
this.positionTarget = null;
398398
this.setOpened(false);
399-
}
400-
401-
/**
402-
* Reset tooltip
403-
* @returns {void}
404-
*/
405-
private resetTooltip = (): void => {
406-
this.hideTooltip();
407399
};
408400

409401
/**
@@ -507,7 +499,7 @@ class Tooltip extends BasicElement {
507499
*/
508500
private onClick = (): void => {
509501
this.clicked = true;
510-
this.hideTooltip();
502+
this.resetTooltip();
511503
};
512504

513505
/**

packages/elements/src/tooltip/managers/tooltip-manager.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,17 @@ class TooltipManager {
4040
}
4141
}
4242

43+
/**
44+
* Cancel the pending task of throttled mousemove event listener.
45+
* This prevents the task from running out of order compared to the event dispatching sequence
46+
* due to its setTimeout usage.
47+
* Any event listener hiding the tooltip should call this method.
48+
* @returns {void}
49+
*/
50+
private cancelThrottler() {
51+
this.titleThrottler.task?.cancel();
52+
}
53+
4354
/**
4455
* @param event Mouse move event
4556
* @returns {void}
@@ -59,6 +70,7 @@ class TooltipManager {
5970
* @returns {void}
6071
*/
6172
private onClick = (event: MouseEvent): void => {
73+
this.cancelThrottler();
6274
this.registry.forEach(({ click }) => click(event));
6375
};
6476

@@ -67,6 +79,7 @@ class TooltipManager {
6779
* @returns {void}
6880
*/
6981
private onMouseOut = (event: MouseEvent): void => {
82+
this.cancelThrottler();
7083
this.registry.forEach(({ mouseout }) => mouseout(event));
7184
};
7285

@@ -75,6 +88,7 @@ class TooltipManager {
7588
* @returns {void}
7689
*/
7790
private onMouseLeave = (event: MouseEvent): void => {
91+
this.cancelThrottler();
7892
this.registry.forEach(({ mouseleave }) => mouseleave(event));
7993
};
8094

@@ -83,6 +97,7 @@ class TooltipManager {
8397
* @returns {void}
8498
*/
8599
private onWheel = (event: WheelEvent): void => {
100+
this.cancelThrottler();
86101
this.registry.forEach(({ wheel }) => wheel(event));
87102
};
88103

@@ -91,6 +106,7 @@ class TooltipManager {
91106
* @returns {void}
92107
*/
93108
private onKeyDown = (event: KeyboardEvent): void => {
109+
this.cancelThrottler();
94110
this.registry.forEach(({ keydown }) => keydown(event));
95111
};
96112

@@ -99,6 +115,7 @@ class TooltipManager {
99115
* @returns {void}
100116
*/
101117
private onBlur = (event: FocusEvent): void => {
118+
this.cancelThrottler();
102119
this.registry.forEach(({ blur }) => blur(event));
103120
};
104121

0 commit comments

Comments
 (0)