Skip to content

Commit 2d47dd0

Browse files
fix(tooltip): fix tooltip reopen when mouse leaves (#1276)
1 parent acef46c commit 2d47dd0

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
@@ -258,6 +258,7 @@ describe('tooltip/Tooltip', function () {
258258
});
259259

260260
await elementUpdated(el);
261+
await nextFrame();
261262

262263
expect(tooltip.opened, 'Tooltip is not opened').to.be.true;
263264

@@ -268,6 +269,7 @@ describe('tooltip/Tooltip', function () {
268269
});
269270

270271
await elementUpdated(el);
272+
await nextFrame();
271273

272274
expect(tooltip.opened, 'Tooltip is not hidden').to.be.false;
273275
}).timeout(MouseMoveDelay * 5);

packages/elements/src/tooltip/index.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -382,20 +382,12 @@ class Tooltip extends BasicElement {
382382
* Hide tooltip
383383
* @returns {void}
384384
*/
385-
private hideTooltip(): void {
385+
private resetTooltip = (): void => {
386386
this.reset();
387387
this.matchTarget = null;
388388
this.matchTargetRect = null;
389389
this.positionTarget = null;
390390
this.setOpened(false);
391-
}
392-
393-
/**
394-
* Reset tooltip
395-
* @returns {void}
396-
*/
397-
private resetTooltip = (): void => {
398-
this.hideTooltip();
399391
};
400392

401393
/**
@@ -499,7 +491,7 @@ class Tooltip extends BasicElement {
499491
*/
500492
private onClick = (): void => {
501493
this.clicked = true;
502-
this.hideTooltip();
494+
this.resetTooltip();
503495
};
504496

505497
/**

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

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

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

@@ -66,6 +78,7 @@ class TooltipManager {
6678
* @returns {void}
6779
*/
6880
private onMouseOut = (event: MouseEvent): void => {
81+
this.cancelThrottler();
6982
this.registry.forEach(({ mouseout }) => mouseout(event));
7083
};
7184

@@ -74,6 +87,7 @@ class TooltipManager {
7487
* @returns {void}
7588
*/
7689
private onMouseLeave = (event: MouseEvent): void => {
90+
this.cancelThrottler();
7791
this.registry.forEach(({ mouseleave }) => mouseleave(event));
7892
};
7993

@@ -82,6 +96,7 @@ class TooltipManager {
8296
* @returns {void}
8397
*/
8498
private onWheel = (event: WheelEvent): void => {
99+
this.cancelThrottler();
85100
this.registry.forEach(({ wheel }) => wheel(event));
86101
};
87102

@@ -90,6 +105,7 @@ class TooltipManager {
90105
* @returns {void}
91106
*/
92107
private onKeyDown = (event: KeyboardEvent): void => {
108+
this.cancelThrottler();
93109
this.registry.forEach(({ keydown }) => keydown(event));
94110
};
95111

@@ -98,6 +114,7 @@ class TooltipManager {
98114
* @returns {void}
99115
*/
100116
private onBlur = (event: FocusEvent): void => {
117+
this.cancelThrottler();
101118
this.registry.forEach(({ blur }) => blur(event));
102119
};
103120

0 commit comments

Comments
 (0)