forked from github/plane
feat: tooltip component added
This commit is contained in:
parent
9075f9441c
commit
30c59545f0
@ -1,39 +1,73 @@
|
|||||||
import React from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
|
|
||||||
type Props = {
|
export type Props = {
|
||||||
|
direction?: "top" | "right" | "bottom" | "left";
|
||||||
|
content: string | React.ReactNode;
|
||||||
|
margin?: string;
|
||||||
children: React.ReactNode;
|
children: React.ReactNode;
|
||||||
content: React.ReactNode;
|
customStyle?: string;
|
||||||
position?: "top" | "bottom" | "left" | "right";
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const Tooltip: React.FC<Props> = ({ children, content, position = "top" }) => (
|
const Tooltip: React.FC<Props> = ({
|
||||||
<div className="relative group">
|
content,
|
||||||
<div
|
direction = "top",
|
||||||
className={`fixed pointer-events-none transition-opacity opacity-0 group-hover:opacity-100 bg-black text-white px-3 py-1 rounded ${
|
children,
|
||||||
position === "right"
|
margin = "24px",
|
||||||
? "left-14"
|
customStyle,
|
||||||
: position === "left"
|
}) => {
|
||||||
? "right-14"
|
const [active, setActive] = useState(false);
|
||||||
: position === "top"
|
const [styleConfig, setStyleConfig] = useState("top-[calc(-100%-24px)]");
|
||||||
? "bottom-14"
|
let timeout: any;
|
||||||
: "top-14"
|
|
||||||
}`}
|
const showToolTip = () => {
|
||||||
>
|
timeout = setTimeout(() => {
|
||||||
<p className="truncate text-xs">{content}</p>
|
setActive(true);
|
||||||
<span
|
}, 300);
|
||||||
className={`absolute w-2 h-2 bg-black ${
|
};
|
||||||
position === "top"
|
|
||||||
? "top-full left-1/2 transform -translate-y-1/2 -translate-x-1/2 rotate-45"
|
const hideToolTip = () => {
|
||||||
: position === "bottom"
|
clearInterval(timeout);
|
||||||
? "bottom-full left-1/2 transform translate-y-1/2 -translate-x-1/2 rotate-45"
|
setActive(false);
|
||||||
: position === "left"
|
};
|
||||||
? "left-full top-1/2 transform translate-x-1/2 -translate-y-1/2 rotate-45"
|
|
||||||
: "right-full top-1/2 transform translate-x-1/2 -translate-y-1/2 rotate-45"
|
const tooltipStyles = {
|
||||||
}`}
|
top: `
|
||||||
/>
|
left-[50%] translate-x-[-50%] before:contents-[""] before:border-solid
|
||||||
</div>
|
before:border-transparent before:h-0 before:w-0 before:absolute before:pointer-events-none
|
||||||
|
before:border-[6px] before:left-[50%] before:ml-[calc(6px*-1)] before:top-full before:border-t-black`,
|
||||||
|
|
||||||
|
right: `
|
||||||
|
right-[-100%] top-[50%]
|
||||||
|
translate-x-0 translate-y-[-50%] `,
|
||||||
|
|
||||||
|
bottom: `
|
||||||
|
left-[50%] translate-x-[-50%] before:contents-[""] before:border-solid
|
||||||
|
before:border-transparent before:h-0 before:w-0 before:absolute before:pointer-events-none
|
||||||
|
before:border-[6px] before:left-[50%] before:ml-[calc(6px*-1)] before:bottom-full before:border-b-black`,
|
||||||
|
|
||||||
|
left: `
|
||||||
|
left-[-100%] top-[50%]
|
||||||
|
translate-x-0 translate-y-[-50%] `,
|
||||||
|
};
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const styleConfig = direction + "-[calc(-100%-" + margin + ")]";
|
||||||
|
setStyleConfig(styleConfig);
|
||||||
|
}, [margin, direction]);
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className="inline-block relative" onMouseEnter={showToolTip} onMouseLeave={hideToolTip}>
|
||||||
{children}
|
{children}
|
||||||
|
{active && (
|
||||||
|
<div
|
||||||
|
className={`absolute p-[6px] text-xs z-20 rounded leading-1 text-white bg-black text-center w-max max-w-[300px]
|
||||||
|
${tooltipStyles[direction]} ${customStyle ? customStyle : ""} ${styleConfig}`}
|
||||||
|
>
|
||||||
|
{content}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
|
};
|
||||||
|
|
||||||
export default Tooltip;
|
export default Tooltip;
|
||||||
|
Loading…
Reference in New Issue
Block a user