-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTooltipContainer.js
More file actions
54 lines (41 loc) · 1.29 KB
/
TooltipContainer.js
File metadata and controls
54 lines (41 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"use client";
import { useEffect, useRef, useState } from "react";
import importedStyles from "./TooltipContainer.module.css";
export default function TooltipContainer(props) {
const children = props.children;
const style = props.style;
const styles = props.styles || importedStyles;
const tooltip = props.tooltip;
const timerRef = useRef();
const [isVisible, setIsVisible] = useState(false);
const [left, setLeft] = useState(0);
const [top, setTop] = useState(0);
function onMouseEnter(e) {
const boundingClientRect = e.target.getBoundingClientRect();
setLeft(boundingClientRect.left);
setTop(boundingClientRect.top);
clearTimeout(timerRef.current);
timerRef.current = setTimeout(() => {
setIsVisible(true);
}, 1000);
}
function onMouseLeave(e) {
clearTimeout(timerRef.current);
setIsVisible(false);
}
useEffect(() => {
return () => {
clearTimeout(timerRef.current);
};
}, []);
return (
<div className={styles.tooltip_container} onMouseEnter={onMouseEnter} onMouseLeave={onMouseLeave} style={style}>
{children}
{isVisible && tooltip && tooltip.length > 0 && (
<div className={styles.tooltip} onMouseLeave={onMouseLeave} style={{ left, top }}>
{tooltip}
</div>
)}
</div>
);
}