2020-06-19 11:36:49 +01:00
|
|
|
import "./Tooltip.scss";
|
|
|
|
|
2021-05-25 13:52:04 +02:00
|
|
|
import React, { useEffect } from "react";
|
|
|
|
|
2022-02-03 20:34:59 +05:30
|
|
|
export const getTooltipDiv = () => {
|
2021-05-25 13:52:04 +02:00
|
|
|
const existingDiv = document.querySelector<HTMLDivElement>(
|
|
|
|
".excalidraw-tooltip",
|
|
|
|
);
|
|
|
|
if (existingDiv) {
|
|
|
|
return existingDiv;
|
|
|
|
}
|
|
|
|
const div = document.createElement("div");
|
|
|
|
document.body.appendChild(div);
|
|
|
|
div.classList.add("excalidraw-tooltip");
|
|
|
|
return div;
|
|
|
|
};
|
|
|
|
|
2022-02-03 20:34:59 +05:30
|
|
|
export const updateTooltipPosition = (
|
2021-05-25 13:52:04 +02:00
|
|
|
tooltip: HTMLDivElement,
|
2022-02-03 20:34:59 +05:30
|
|
|
item: {
|
|
|
|
left: number;
|
|
|
|
top: number;
|
|
|
|
width: number;
|
|
|
|
height: number;
|
|
|
|
},
|
|
|
|
position: "bottom" | "top" = "bottom",
|
2021-05-25 13:52:04 +02:00
|
|
|
) => {
|
2022-02-03 20:34:59 +05:30
|
|
|
const tooltipRect = tooltip.getBoundingClientRect();
|
2021-05-25 13:52:04 +02:00
|
|
|
|
|
|
|
const viewportWidth = window.innerWidth;
|
|
|
|
const viewportHeight = window.innerHeight;
|
|
|
|
|
|
|
|
const margin = 5;
|
|
|
|
|
2022-02-03 20:34:59 +05:30
|
|
|
let left = item.left + item.width / 2 - tooltipRect.width / 2;
|
|
|
|
if (left < 0) {
|
|
|
|
left = margin;
|
|
|
|
} else if (left + tooltipRect.width >= viewportWidth) {
|
|
|
|
left = viewportWidth - tooltipRect.width - margin;
|
|
|
|
}
|
2021-05-25 13:52:04 +02:00
|
|
|
|
2022-02-03 20:34:59 +05:30
|
|
|
let top: number;
|
|
|
|
|
|
|
|
if (position === "bottom") {
|
|
|
|
top = item.top + item.height + margin;
|
|
|
|
if (top + tooltipRect.height >= viewportHeight) {
|
|
|
|
top = item.top - tooltipRect.height - margin;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
top = item.top - tooltipRect.height - margin;
|
|
|
|
if (top < 0) {
|
|
|
|
top = item.top + item.height + margin;
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 13:52:04 +02:00
|
|
|
|
|
|
|
Object.assign(tooltip.style, {
|
2022-02-03 20:34:59 +05:30
|
|
|
top: `${top}px`,
|
|
|
|
left: `${left}px`,
|
2021-05-25 13:52:04 +02:00
|
|
|
});
|
|
|
|
};
|
2020-06-19 11:36:49 +01:00
|
|
|
|
2022-02-03 20:34:59 +05:30
|
|
|
const updateTooltip = (
|
|
|
|
item: HTMLDivElement,
|
|
|
|
tooltip: HTMLDivElement,
|
|
|
|
label: string,
|
|
|
|
long: boolean,
|
|
|
|
) => {
|
|
|
|
tooltip.classList.add("excalidraw-tooltip--visible");
|
|
|
|
tooltip.style.minWidth = long ? "50ch" : "10ch";
|
|
|
|
tooltip.style.maxWidth = long ? "50ch" : "15ch";
|
|
|
|
|
|
|
|
tooltip.textContent = label;
|
|
|
|
|
|
|
|
const itemRect = item.getBoundingClientRect();
|
|
|
|
updateTooltipPosition(tooltip, itemRect);
|
|
|
|
};
|
|
|
|
|
2020-06-19 11:36:49 +01:00
|
|
|
type TooltipProps = {
|
|
|
|
children: React.ReactNode;
|
|
|
|
label: string;
|
2020-12-14 18:54:54 +05:30
|
|
|
long?: boolean;
|
2021-11-24 17:16:18 +01:00
|
|
|
style?: React.CSSProperties;
|
2020-06-19 11:36:49 +01:00
|
|
|
};
|
|
|
|
|
2021-11-24 17:16:18 +01:00
|
|
|
export const Tooltip = ({
|
|
|
|
children,
|
|
|
|
label,
|
|
|
|
long = false,
|
|
|
|
style,
|
|
|
|
}: TooltipProps) => {
|
2021-05-25 13:52:04 +02:00
|
|
|
useEffect(() => {
|
|
|
|
return () =>
|
|
|
|
getTooltipDiv().classList.remove("excalidraw-tooltip--visible");
|
|
|
|
}, []);
|
|
|
|
return (
|
|
|
|
<div
|
2021-07-15 18:48:03 +02:00
|
|
|
className="excalidraw-tooltip-wrapper"
|
2021-05-25 13:52:04 +02:00
|
|
|
onPointerEnter={(event) =>
|
|
|
|
updateTooltip(
|
|
|
|
event.currentTarget as HTMLDivElement,
|
|
|
|
getTooltipDiv(),
|
|
|
|
label,
|
|
|
|
long,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
onPointerLeave={() =>
|
|
|
|
getTooltipDiv().classList.remove("excalidraw-tooltip--visible")
|
2020-12-14 18:54:54 +05:30
|
|
|
}
|
2021-11-24 17:16:18 +01:00
|
|
|
style={style}
|
2020-12-14 18:54:54 +05:30
|
|
|
>
|
2021-05-25 13:52:04 +02:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|