breakout71/src/tooltip.ts

94 lines
2.5 KiB
TypeScript
Raw Normal View History

2025-04-24 14:57:08 +02:00
import { isOptionOn } from "./options";
2025-04-01 13:35:33 +02:00
2025-04-29 10:31:56 +02:00
const tooltip = document.getElementById("tooltip") as HTMLDivElement;
2025-04-01 13:35:33 +02:00
export function setupTooltips() {
2025-04-24 14:57:08 +02:00
if (isOptionOn("mobile-mode")) {
setupMobileTooltips(tooltip);
} else {
setupDesktopTooltips(tooltip);
}
2025-04-23 17:25:37 +02:00
}
2025-04-25 10:10:09 +02:00
export function hideAnyTooltip() {
tooltip.style.display = "none";
}
2025-04-23 17:25:37 +02:00
function setupMobileTooltips(tooltip: HTMLDivElement) {
2025-04-26 22:40:32 +02:00
tooltip.className = "mobile";
2025-04-24 14:57:08 +02:00
function openTooltip(e: Event) {
2025-04-26 22:40:32 +02:00
hideAnyTooltip();
2025-04-24 14:57:08 +02:00
const hovering = e.target as HTMLElement;
2025-05-03 16:42:03 +02:00
const tooltipContent =
hovering?.getAttribute("data-help-content")?.trim() || "";
if (!tooltipContent) {
2025-04-24 14:57:08 +02:00
return;
2025-04-23 17:25:37 +02:00
}
2025-04-24 14:57:08 +02:00
e.stopPropagation();
e.preventDefault();
2025-05-03 16:42:03 +02:00
tooltip.innerHTML = tooltipContent;
2025-04-24 14:57:08 +02:00
tooltip.style.display = "";
2025-04-26 22:40:32 +02:00
const { top } = hovering.getBoundingClientRect();
2025-04-26 17:27:26 +02:00
tooltip.style.transform = `translate(0,${top}px) translate(0,-100%)`;
2025-04-24 14:57:08 +02:00
}
2025-04-29 10:31:56 +02:00
document.body.addEventListener("click", openTooltip, true);
2025-04-26 17:27:26 +02:00
document.addEventListener("scroll", hideAnyTooltip);
2025-04-23 17:25:37 +02:00
}
function setupDesktopTooltips(tooltip: HTMLDivElement) {
2025-04-26 22:40:32 +02:00
tooltip.className = "desktop";
2025-04-24 14:57:08 +02:00
function updateTooltipPosition(e: { clientX: number; clientY: number }) {
2025-04-07 15:25:58 +02:00
tooltip.style.transform = `translate(${e.clientX}px,${e.clientY}px) translate(${e.clientX > window.innerWidth / 2 ? "-100%" : "0"},${e.clientY > (window.innerHeight * 2) / 3 ? "-100%" : "20px"})`;
2025-04-24 14:57:08 +02:00
}
2025-04-01 13:35:33 +02:00
2025-04-24 14:57:08 +02:00
function closeToolTip() {
2025-04-25 10:10:09 +02:00
hideAnyTooltip();
2025-04-02 10:42:01 +02:00
hovering = null;
2025-04-24 14:57:08 +02:00
}
let hovering: HTMLElement | null = null;
document.body.addEventListener(
"mouseenter",
(e: MouseEvent) => {
let parent: HTMLElement | null = e.target as HTMLElement;
while (parent && !parent.hasAttribute("data-tooltip")) {
parent = parent.parentElement;
}
if (parent?.getAttribute("data-tooltip")?.trim()) {
hovering = parent as HTMLElement;
tooltip.innerHTML = hovering.getAttribute("data-tooltip") || "";
tooltip.style.display = "";
updateTooltipPosition(e);
} else {
closeToolTip();
}
},
true,
);
setInterval(() => {
if (hovering) {
if (!document.body.contains(hovering)) {
closeToolTip();
}
}
}, 200);
document.body.addEventListener(
"mousemove",
(e) => {
if (!tooltip.style.display) {
updateTooltipPosition(e);
}
},
true,
);
document.body.addEventListener(
"mouseleave",
(e) => {
closeToolTip();
},
true,
);
2025-04-01 13:35:33 +02:00
}