Skip to content

Commit

Permalink
feat: Tooltip - Performance - avoid calling clearTimeout on unmount i…
Browse files Browse the repository at this point in the history
…f timeout id is 0 (#3163)

Co-authored-by: Carlos Nicacio <[email protected]>
  • Loading branch information
carloseustaquio and Carlos Nicacio authored Oct 3, 2024
1 parent 79c6a88 commit db6267a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .yarn/versions/ae1744f4.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
releases:
"@radix-ui/react-tooltip": patch

declined:
- primitives
11 changes: 10 additions & 1 deletion packages/react/tooltip/src/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -190,12 +190,14 @@ const Tooltip: React.FC<TooltipProps> = (props: ScopedProps<TooltipProps>) => {

const handleOpen = React.useCallback(() => {
window.clearTimeout(openTimerRef.current);
openTimerRef.current = 0;
wasOpenDelayedRef.current = false;
setOpen(true);
}, [setOpen]);

const handleClose = React.useCallback(() => {
window.clearTimeout(openTimerRef.current);
openTimerRef.current = 0;
setOpen(false);
}, [setOpen]);

Expand All @@ -204,11 +206,17 @@ const Tooltip: React.FC<TooltipProps> = (props: ScopedProps<TooltipProps>) => {
openTimerRef.current = window.setTimeout(() => {
wasOpenDelayedRef.current = true;
setOpen(true);
openTimerRef.current = 0;
}, delayDuration);
}, [delayDuration, setOpen]);

React.useEffect(() => {
return () => window.clearTimeout(openTimerRef.current);
return () => {
if (openTimerRef.current) {
window.clearTimeout(openTimerRef.current);
openTimerRef.current = 0;
}
};
}, []);

return (
Expand All @@ -230,6 +238,7 @@ const Tooltip: React.FC<TooltipProps> = (props: ScopedProps<TooltipProps>) => {
} else {
// Clear the timer in case the pointer leaves the trigger before the tooltip is opened.
window.clearTimeout(openTimerRef.current);
openTimerRef.current = 0;
}
}, [handleClose, disableHoverableContent])}
onOpen={handleOpen}
Expand Down

0 comments on commit db6267a

Please sign in to comment.