-
-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathtooltip-node.tsx
96 lines (79 loc) · 2.51 KB
/
tooltip-node.tsx
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import React, { createContext, useContext, useState, useCallback } from 'react';
import {
type NodeProps,
type NodeToolbarProps,
NodeToolbar,
} from '@xyflow/react';
import { BaseNode } from './base-node';
/* TOOLTIP CONTEXT ---------------------------------------------------------- */
const TooltipContext = createContext(false);
/* TOOLTIP NODE ------------------------------------------------------------- */
export interface TooltipNodeProps extends Partial<NodeProps> {
children?: React.ReactNode;
}
/**
* A component that wraps a node and provides tooltip visibility context.
*/
export const TooltipNode = React.forwardRef<HTMLDivElement, TooltipNodeProps>(
({ selected, children }, ref) => {
const [isTooltipVisible, setTooltipVisible] = useState(false);
const showTooltip = useCallback(() => setTooltipVisible(true), []);
const hideTooltip = useCallback(() => setTooltipVisible(false), []);
return (
<TooltipContext.Provider value={isTooltipVisible}>
<BaseNode
ref={ref}
onMouseEnter={showTooltip}
onMouseLeave={hideTooltip}
onFocus={showTooltip}
onBlur={hideTooltip}
tabIndex={0}
>
{children}
</BaseNode>
</TooltipContext.Provider>
);
},
);
TooltipNode.displayName = 'TooltipNode';
/* TOOLTIP CONTENT ---------------------------------------------------------- */
export interface TooltipContentProps extends NodeToolbarProps {}
/**
* A component that displays the tooltip content based on visibility context.
*/
export const TooltipContent = React.forwardRef<
HTMLDivElement,
TooltipContentProps
>(({ position, children }, ref) => {
const isTooltipVisible = useContext(TooltipContext);
return (
<div ref={ref}>
<NodeToolbar
isVisible={isTooltipVisible}
className="rounded-sm bg-primary p-2 text-primary-foreground"
tabIndex={1}
position={position}
>
{children}
</NodeToolbar>
</div>
);
});
TooltipContent.displayName = 'TooltipContent';
/* TOOLTIP TRIGGER ---------------------------------------------------------- */
export interface TooltipTriggerProps
extends React.HTMLAttributes<HTMLParagraphElement> {}
/**
* A component that triggers the tooltip visibility.
*/
export const TooltipTrigger = React.forwardRef<
HTMLParagraphElement,
TooltipTriggerProps
>(({ children, ...props }, ref) => {
return (
<div ref={ref} {...props}>
{children}
</div>
);
});
TooltipTrigger.displayName = 'TooltipTrigger';