Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Fixed tooltip for Mobile Area Charts #1817

Merged
merged 11 commits into from
Nov 11, 2024
63 changes: 58 additions & 5 deletions app/charts/shared/interaction/tooltip-box.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,11 @@ import React, {
} from "react";
import ReactDOM from "react-dom";

import { Margins } from "@/charts/shared/use-size";
import { Margins, useSize } from "@/charts/shared/use-size";
import { useIsMobile } from "@/utils/use-is-mobile";
import { useResizeObserver } from "@/utils/use-resize-observer";

import { useChartBounds } from "../chart-dimensions";

const TRIANGLE_SIZE = 8;
const TOOLTIP_OFFSET = 4;
Expand Down Expand Up @@ -123,19 +127,32 @@ export const TooltipBox = ({
}: TooltipBoxProps) => {
const triangle = mkTriangle(placement);
const [pos, posRef] = usePosition();

const [tooltipRef, tooltipWidth] = useResizeObserver<HTMLDivElement>();

const isMobile = useIsMobile();
const { width, height } = useSize();
const { chartWidth } = useChartBounds(width, margins, height);

const tooltipX = isMobile
? toolTipXBoundary(x!, tooltipWidth, chartWidth)
noahonyejese marked this conversation as resolved.
Show resolved Hide resolved
: x!;
const triangleX = triangleXPos(x!, tooltipWidth, chartWidth);

return (
<>
<div ref={posRef} />
<Portal>
<Box
ref={tooltipRef}
data-testid="chart-tooltip"
style={{
zIndex: 1301,
position: "absolute",
left: x! + margins.left + pos.left,
left: tooltipX! + margins.left + pos.left,
top: mxYOffset(y!, placement) + margins.top + pos.top,
pointerEvents: "none",
transform: mkTranslation(placement),
pointerEvents: "none",
}}
>
<Box
Expand All @@ -158,9 +175,9 @@ export const TooltipBox = ({
height: 0,
borderStyle: "solid",
top: triangle.top,
right: triangle.right,
bottom: triangle.bottom,
left: triangle.left,
left: isMobile ? triangleX : triangle.left,
right: !isMobile ? triangle.right : undefined,
noahonyejese marked this conversation as resolved.
Show resolved Hide resolved
borderWidth: triangle.borderWidth,
borderTopColor: triangle.borderTopColor,
borderRightColor: triangle.borderRightColor,
Expand Down Expand Up @@ -217,6 +234,7 @@ const mkXTranslation = (xP: Xplacement, yP: Yplacement): Xtranslation => {
: `${TRIANGLE_SIZE + TOOLTIP_OFFSET}px`;
}
};

const mkYTranslation = (yP: Yplacement): YTranslation =>
yP === "top" ? "-100%" : yP === "middle" ? "-50%" : 0;

Expand Down Expand Up @@ -348,3 +366,38 @@ const mkTriangle = (p: TooltipPlacement) => {
};
}
};

const toolTipXBoundary = (
value: number,
noahonyejese marked this conversation as resolved.
Show resolved Hide resolved
tooltipWidth: number,
chartWidth: number
) => {
return Math.max(
tooltipWidth / 2 - TRIANGLE_SIZE,
Math.min(chartWidth - tooltipWidth / 2 + TRIANGLE_SIZE, value)
);
};

const triangleXPos = (
value: number,
noahonyejese marked this conversation as resolved.
Show resolved Hide resolved
tooltipWidth: number,
chartWidth: number
): number => {
const condition = chartWidth - tooltipWidth / 2 + TRIANGLE_SIZE < value;
noahonyejese marked this conversation as resolved.
Show resolved Hide resolved

if (condition) {
const overflow = value - (chartWidth - tooltipWidth / 2 + TRIANGLE_SIZE);
const maxOverflow = tooltipWidth / 2 - TRIANGLE_SIZE;

const proportion = Math.min(overflow / maxOverflow, 1);

return (
tooltipWidth / 2 + proportion * (tooltipWidth / 2) - TRIANGLE_SIZE * 2
);
} else {
return Math.min(
tooltipWidth / 2 - TRIANGLE_SIZE,
Math.min(chartWidth - tooltipWidth / 2 + TRIANGLE_SIZE, value)
);
}
};
Loading