forked from PinSpace-Org/GistPin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTooltip.tsx
More file actions
53 lines (45 loc) 路 1.36 KB
/
Copy pathTooltip.tsx
File metadata and controls
53 lines (45 loc) 路 1.36 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
'use client';
import { ReactNode, useRef, useState } from 'react';
type Placement = 'top' | 'bottom' | 'left' | 'right';
interface TooltipProps {
content: string;
children: ReactNode;
placement?: Placement;
delay?: number;
}
const placementClasses: Record<Placement, string> = {
top: 'bottom-full left-1/2 -translate-x-1/2 mb-2',
bottom: 'top-full left-1/2 -translate-x-1/2 mt-2',
left: 'right-full top-1/2 -translate-y-1/2 mr-2',
right: 'left-full top-1/2 -translate-y-1/2 ml-2',
};
export default function Tooltip({ content, children, placement = 'top', delay = 300 }: TooltipProps) {
const [visible, setVisible] = useState(false);
const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
function show() {
timer.current = setTimeout(() => setVisible(true), delay);
}
function hide() {
if (timer.current) clearTimeout(timer.current);
setVisible(false);
}
return (
<span
className="relative inline-flex"
onMouseEnter={show}
onMouseLeave={hide}
onFocus={show}
onBlur={hide}
>
{children}
{visible && (
<span
role="tooltip"
className={`pointer-events-none absolute z-50 whitespace-nowrap rounded bg-gray-900 px-2 py-1 text-xs text-white dark:bg-gray-700 ${placementClasses[placement]}`}
>
{content}
</span>
)}
</span>
);
}