Skip to content

Commit 48bf477

Browse files
committed
prevent infering event processes, only use latest one
1 parent cc3e52d commit 48bf477

1 file changed

Lines changed: 6 additions & 2 deletions

File tree

src/components/Tooltip/Tooltip.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ export const Tooltip = ({
6464
const placeholderRef = React.useRef(null);
6565
const eventMemory = React.useRef<null | "afterhover" | "afterfocus">(null);
6666
const searchId = React.useRef<null | string>(null);
67+
const swapDelay = React.useRef<null | NodeJS.Timeout>(null);
6768
const swapDelayTime = swapPlaceholderDelay;
6869
const [placeholder, setPlaceholder] = React.useState<boolean>(
6970
!otherTooltipProps.disabled &&
@@ -83,7 +84,10 @@ export const Tooltip = ({
8384
React.useEffect(() => {
8485
if (placeholderRef.current !== null) {
8586
const swap = (ev: MouseEvent | globalThis.FocusEvent) => {
86-
const swapDelay = setTimeout(() => {
87+
if (swapDelay.current) {
88+
clearTimeout(swapDelay.current);
89+
}
90+
swapDelay.current = setTimeout(() => {
8791
// we delay the swap to prevent unwanted effects
8892
// (e.g. forced mouseover after the swap but the cursor is already somewhere else)
8993
eventMemory.current = ev.type === "focusin" ? "afterfocus" : "afterhover";
@@ -99,7 +103,7 @@ export const Tooltip = ({
99103
) {
100104
eventMemory.current = null;
101105
}
102-
clearTimeout(swapDelay);
106+
clearTimeout(swapDelay.current as NodeJS.Timeout);
103107
});
104108
}
105109
};

0 commit comments

Comments
 (0)