Skip to content

Commit

Permalink
fix(core-components-tooltip): change tooltip timer to ref (#207)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmitrsavk committed Jul 29, 2020
1 parent 42cedbf commit 139f0a7
Showing 1 changed file with 10 additions and 9 deletions.
19 changes: 10 additions & 9 deletions packages/tooltip/src/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import React, {
useEffect,
useCallback,
ReactNode,
useRef,
} from 'react';
import NodeResolver from 'react-node-resolver';
import cn from 'classnames';
Expand Down Expand Up @@ -120,7 +121,7 @@ export const Tooltip: FC<TooltipProps> = ({
const targetRef = React.useRef<RefElement>(null);
const contentRef = React.useRef<RefElement>(null);

let timerId = 0;
const timer = useRef(0);

const open = () => {
if (!visible) {
Expand Down Expand Up @@ -176,37 +177,37 @@ export const Tooltip: FC<TooltipProps> = ({
return () => {
document.body.removeEventListener('click', handleBodyClick);

clearTimeout(timerId);
clearTimeout(timer.current);
};
}, [close, timerId]);
}, [close]);

const handleTargetClick = () => {
toggle();
};

const handleMouseOver = () => {
clearTimeout(timerId);
clearTimeout(timer.current);

timerId = window.setTimeout(() => {
timer.current = window.setTimeout(() => {
open();
}, onOpenDelay);
};

const handleMouseOut = () => {
clearTimeout(timerId);
clearTimeout(timer.current);

timerId = window.setTimeout(() => {
timer.current = window.setTimeout(() => {
close();
}, onCloseDelay);
};

const handleTouchStart = (event: React.TouchEvent<HTMLElement>) => {
const eventTarget = event.target as Element;

clearTimeout(timerId);
clearTimeout(timer.current);

if (clickedOutside(eventTarget)) {
timerId = window.setTimeout(() => {
timer.current = window.setTimeout(() => {
close();
}, onCloseDelay);
} else {
Expand Down

0 comments on commit 139f0a7

Please sign in to comment.