Skip to content

Commit 8e6c79c

Browse files
fix: debounce immediate
1 parent cbd7669 commit 8e6c79c

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

src/components/Tooltip/Tooltip.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,8 @@ const Tooltip = ({
284284

285285
// debounce handler to prevent call twice when
286286
// mouse enter and focus events being triggered toggether
287-
const debouncedHandleShowTooltip = debounce(handleShowTooltip, 50)
288-
const debouncedHandleHideTooltip = debounce(handleHideTooltip, 50)
287+
const debouncedHandleShowTooltip = debounce(handleShowTooltip, 50, true)
288+
const debouncedHandleHideTooltip = debounce(handleHideTooltip, 50, true)
289289

290290
useEffect(() => {
291291
const elementRefs = new Set(anchorRefs)

src/utils/debounce.ts

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,21 @@ const debounce = (func: (...args: any[]) => void, wait?: number, immediate?: tru
1616
}
1717
}
1818

19-
if (timeout) {
20-
clearTimeout(timeout)
19+
if (immediate && !timeout) {
20+
/**
21+
* there's not need to clear the timeout
22+
* since we expect it to resolve and set `timeout = null`
23+
*/
24+
func.apply(this, args)
25+
timeout = setTimeout(later, wait)
2126
}
2227

23-
timeout = setTimeout(later, wait)
28+
if (!immediate) {
29+
if (timeout) {
30+
clearTimeout(timeout)
31+
}
32+
timeout = setTimeout(later, wait)
33+
}
2434
}
2535
}
2636

0 commit comments

Comments
 (0)