Skip to content

Commit a1b2680

Browse files
author
Dylan Huang
committed
add JSONTooltip
1 parent 2002efb commit a1b2680

1 file changed

Lines changed: 63 additions & 0 deletions

File tree

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import React from "react";
2+
import { Tooltip } from "react-tooltip";
3+
4+
interface JSONTooltipProps {
5+
children: React.ReactNode;
6+
data: any;
7+
position?: "top" | "bottom" | "left" | "right";
8+
className?: string;
9+
}
10+
11+
export const JSONTooltip: React.FC<JSONTooltipProps> = ({
12+
children,
13+
data,
14+
position = "top",
15+
className = "",
16+
}) => {
17+
const tooltipId = `json-tooltip-${Math.random().toString(36).substr(2, 9)}`;
18+
const formattedJSON = JSON.stringify(data, null, 2);
19+
20+
return (
21+
<>
22+
<div data-tooltip-id={tooltipId} className={`cursor-help ${className}`}>
23+
{children}
24+
</div>
25+
<Tooltip
26+
id={tooltipId}
27+
place={position}
28+
className="px-2 py-1 text-xs text-white bg-gray-800 rounded z-10 max-w-md"
29+
style={{
30+
fontSize: "0.75rem",
31+
lineHeight: "1rem",
32+
backgroundColor: "#1f2937",
33+
color: "white",
34+
borderRadius: "0.25rem",
35+
padding: "0.5rem",
36+
zIndex: 10,
37+
userSelect: "text",
38+
pointerEvents: "auto",
39+
}}
40+
delayShow={200}
41+
delayHide={300}
42+
clickable={true}
43+
noArrow={true}
44+
render={() => (
45+
<pre
46+
className="whitespace-pre-wrap text-left text-xs"
47+
style={{
48+
userSelect: "text",
49+
pointerEvents: "auto",
50+
cursor: "text",
51+
}}
52+
onMouseDown={(e) => e.stopPropagation()}
53+
onClick={(e) => e.stopPropagation()}
54+
onDoubleClick={(e) => e.stopPropagation()}
55+
onContextMenu={(e) => e.stopPropagation()}
56+
>
57+
{formattedJSON}
58+
</pre>
59+
)}
60+
/>
61+
</>
62+
);
63+
};

0 commit comments

Comments
 (0)