fix: keep the two-step on chart points on touch

A point and a calendar cell are small targets; one tap that opened the
day opened days by accident. On a touch screen the first tap shows the
value again and the tooltip's calm 44 px row opens the day, on every
day-linked chart and calendar alike. A date in a list still opens in one
tap; a click on a fine pointer still opens directly. The tooltip keeps
not gliding into place.
This commit is contained in:
Marc-André Bombeck committed 2026-10-08 16:04:54 +02:00
1 parent fee6088e08
commit 8bcb3e6301
25 files changed
+213 -79

No files matched your search

+5
View File
@@ -2404,6 +2404,11 @@ export function HealthChart({
: dateLabel
}
rows={rows}
action={chartDays.tooltipAction(
hoverPoint
? dayLinkSource?.indexOf(hoverPoint)
: undefined,
)}
/>
);
}}
@@ -644,8 +644,18 @@ export function MedicationComplianceChart({
delta,
},
];
const hoverPoint = payload[0]?.payload;
return (
<RichChartTooltip active label={dateLabel} rows={rows} />
<RichChartTooltip
active
label={dateLabel}
rows={rows}
action={chartDays.tooltipAction(
hoverPoint
? chartData.indexOf(hoverPoint)
: undefined,
)}
/>
);
}}
/>
+10 -1
View File
@@ -1015,7 +1015,16 @@ export function MoodChart({
}
}
if (rows.length === 0) return null;
return <RichChartTooltip active label={dateLabel} rows={rows} />;
return (
<RichChartTooltip
active
label={dateLabel}
rows={rows}
action={chartDays.tooltipAction(
hoverPoint ? dayLinkSource?.indexOf(hoverPoint) : undefined,
)}
/>
);
}}
/>
<Line
@@ -155,6 +155,7 @@ export function NutrientDailyBarChart({
active
label={fmt.dateShortSmartCalendar(point.day)}
rows={rows}
action={chartDays.tooltipAction(points.indexOf(point))}
/>
);
}}
@@ -239,6 +239,7 @@ export function CustomMetricChart({
active
label={fmt.dateShortSmart(new Date(point.timestamp))}
rows={rows}
action={chartDays.tooltipAction(points.indexOf(point))}
/>
);
}}
+18 -2
View File
@@ -24,7 +24,7 @@
* it surfaces a "log your morning temperature" hint rather than a flat dot.
*/
import { useMemo } from "react";
import { useMemo, type ReactNode } from "react";
import { Thermometer } from "lucide-react";
import {
CartesianGrid,
@@ -288,7 +288,20 @@ export function BbtChart({
strokeOpacity: 0.3,
strokeDasharray: "3 3",
}}
content={<BbtTooltip />}
content={(props) => {
const payload = props.payload as unknown as
{ payload: BbtPoint }[] | undefined;
const point = payload?.[0]?.payload;
return (
<BbtTooltip
active={props.active}
payload={payload}
action={chartDays.tooltipAction(
point ? points.indexOf(point) : undefined,
)}
/>
);
}}
/>
{ovulationMs != null ? (
<ReferenceLine
@@ -374,6 +387,8 @@ function PhaseDot(props: { cx?: number; cy?: number; payload?: BbtPoint }) {
export function BbtTooltip(props: {
active?: boolean;
payload?: { payload: BbtPoint }[];
/** v1.42 — the tooltip's way to the day on a touch screen. */
action?: ReactNode;
}) {
const { t } = useTranslations();
const fmt = useFormatters();
@@ -407,6 +422,7 @@ export function BbtTooltip(props: {
{t(`cycle.ovulationTest.${p.ovulationTest}`)}
</p>
) : null}
{props.action ?? null}
</div>
);
}
@@ -9,8 +9,9 @@ import { describe, expect, it } from "vitest";
* The day view arrived on six metric pages and nowhere else, and the recovery
* page beside them offered no day at all: nothing noticed, because every check
* proved one chart. This guard walks `src/app` and `src/components` and holds
* three kinds of surface to the same doors (a click or a tap opens the day,
* the dashed line through the open day, the row of day dots, the caption):
* three kinds of surface to the same doors (a click opens the day, on touch
* the tooltip's "View the whole day", the dashed line through the open day,
* the row of day dots, the caption):
*
* A. a mount of a chart whose doors are a prop (`HealthChart`, its dynamic
* wrapper, `MoodChart`, the adherence chart, the nutrient bar chart)
@@ -20,7 +21,7 @@ import { describe, expect, it } from "vitest";
* B. a file that draws its own Recharts chart over a date axis (an
* `<XAxis>` keyed on a date-shaped field, or `scale="time"`) calls
* `useChartDayLinks` and renders its parts (`ChartDayFooter`,
* `OPEN_DAY_LINE`, the tooltip's props);
* `OPEN_DAY_LINE`, the tooltip's props and its touch action);
* C. a calendar heatmap (anything laid out by `heatmapDays`) calls
* `useHeatmapDay`.
*
@@ -195,14 +196,15 @@ function findMissingDoors(): {
/\buseChartDayLinks\(/.test(text) &&
/\bChartDayFooter\b/.test(text) &&
/\bOPEN_DAY_LINE\b/.test(text) &&
/\.tooltipProps\b/.test(text);
/\.tooltipProps\b/.test(text) &&
/\.tooltipAction\(/.test(text);
if (wired) {
doors.charts += 1;
doors.chartFiles.push(file);
} else if (!listed) {
missing.push({
file,
why: "a date-axis chart without the shared day doors (useChartDayLinks, ChartDayFooter, OPEN_DAY_LINE, tooltip props)",
why: "a date-axis chart without the shared day doors (useChartDayLinks, ChartDayFooter, OPEN_DAY_LINE, tooltip props and touch action)",
});
}
}
+45 -22
View File
@@ -1,6 +1,6 @@
"use client";
import { useCallback, useMemo, useRef } from "react";
import { useCallback, useMemo, useRef, type ReactNode } from "react";
import type { DateKey } from "@/lib/day/contract";
import { prefersReducedMotion } from "@/lib/charts/reduced-motion";
@@ -8,6 +8,7 @@ import { prefersReducedMotion } from "@/lib/charts/reduced-motion";
import {
ChartDayCaption,
DayRug,
TooltipDayAction,
useCoarsePointer,
type ChartDayAxis,
type ChartDayMark,
@@ -25,11 +26,13 @@ import { useTodayKey } from "./use-today-key";
* button, the dashed line through the open day and the row of dots under the
* axis behave the same on every page:
*
* - a click, or a tap, on the plot opens the day of the point under it.
* On a fine pointer hovering still shows the tooltip; on a touch screen
* the tap opens the day at once, and the day puts the tapped value at
* its top. A scroll that grazes the chart is a pan, not a tap, and opens
* nothing;
* - fine pointer: a click on the plot opens the day of the point under the
* cursor; hovering shows the tooltip as before;
* - touch: a tap shows the value and pins the tooltip, whose button
* ("View the whole day", a calm 44 px row) opens the day. Two steps on
* purpose (the maintainer's call, after trying one on the beta): a
* point is a small target, and one tap that opened a sheet would open
* days by accident;
* - the open day is marked with `OPEN_DAY_LINE` (a dashed vertical line)
* at `openIndex`;
* - `ChartDayFooter` draws the row of day dots and the one-line caption.
@@ -91,12 +94,19 @@ export interface ChartDayLinks {
onChartClick:
((state: ChartClickState | null | undefined) => void) | undefined;
/**
* Spread on `<Tooltip>`. On touch, and under reduced motion, the tooltip
* does not glide: Recharts slides it from where it last was for 400 ms,
* which on a tap drew a box sweeping across the chart while the day was
* already opening.
* Spread on `<Tooltip>`: on touch it stays where the tap put it and takes
* taps. It does not glide there either: Recharts slides the tooltip from
* where it last was for 400 ms, which turned "View the whole day" into a
* moving target under the finger. Reduced motion stops the glide on every
* pointer.
*/
tooltipProps: { isAnimationActive?: boolean };
tooltipProps: {
trigger: "click" | "hover";
wrapperStyle?: React.CSSProperties;
isAnimationActive?: boolean;
};
/** The tooltip's button for point `index`, on a touch screen only. */
tooltipAction: (index: number | undefined) => ReactNode | undefined;
}
export function useChartDayLinks({
@@ -134,16 +144,17 @@ export function useChartDayLinks({
return at === -1 ? undefined : at;
}, [enabled, openKey, days]);
const onChartClick = enabled
? (state: ChartClickState | null | undefined) => {
if (!state) return;
const index = indexOfClick
? indexOfClick(state)
: Number(state.activeTooltipIndex);
if (index === null || !Number.isInteger(index)) return;
open(index);
}
: undefined;
const onChartClick =
enabled && !coarse
? (state: ChartClickState | null | undefined) => {
if (!state) return;
const index = indexOfClick
? indexOfClick(state)
: Number(state.activeTooltipIndex);
if (index === null || !Number.isInteger(index)) return;
open(index);
}
: undefined;
return {
active: enabled,
@@ -159,7 +170,19 @@ export function useChartDayLinks({
plotClassName: enabled && !coarse ? "cursor-pointer" : "",
onChartClick,
tooltipProps:
coarse || prefersReducedMotion() ? { isAnimationActive: false } : {},
enabled && coarse
? {
trigger: "click",
wrapperStyle: { pointerEvents: "auto", zIndex: 20 },
isAnimationActive: false,
}
: prefersReducedMotion()
? { trigger: "hover", isAnimationActive: false }
: { trigger: "hover" },
tooltipAction: (index) =>
enabled && coarse && index !== undefined && canOpen(index) ? (
<TooltipDayAction onOpen={() => open(index)} />
) : undefined,
};
}
+27 -4
View File
@@ -1,6 +1,7 @@
"use client";
import { useSyncExternalStore } from "react";
import { ArrowRight } from "lucide-react";
import type { DateKey } from "@/lib/day/contract";
import { dateOnlyKey } from "@/lib/tz/date-only";
@@ -15,10 +16,12 @@ import { useTodayKey } from "./use-today-key";
/**
* The day view's entry points on a value chart.
*
* - A click, or a tap, on the plot opens the day of the point under it,
* with that value at the top of the day. Hovering on a fine pointer
* keeps showing the tooltip; a scroll that grazes the chart is a pan,
* not a tap, and opens nothing.
* - On a fine pointer, a click on the plot opens the day of the point under
* the cursor; hovering keeps showing the tooltip as before.
* - On a touch screen a tap shows the value, as it always did, and the
* tooltip carries "View the whole day". Two steps on purpose: a point is
* a small target, and one tap that opened a sheet would open days by
* accident. A date in a list is a bigger target and opens in one tap.
* - Under the axis, a quiet row of dots marks the days that hold anything,
* a ring the days with a notable observation. Decoration for the eye; the
* keyboard and screen-reader way to a day is the chart's data table.
@@ -57,6 +60,26 @@ export function chartPointDayKey(timestamp: number): DateKey {
return dateOnlyKey(new Date(timestamp));
}
/** The tooltip's way to the day on a touch screen. */
export function TooltipDayAction({ onOpen }: { onOpen: () => void }) {
const { t } = useTranslations();
return (
<button
type="button"
data-slot="chart-tooltip-open-day"
onClick={(event) => {
event.stopPropagation();
onOpen();
}}
onPointerDown={(event) => event.stopPropagation()}
className="bg-muted hover:bg-muted/80 focus-visible:ring-ring/50 mt-2.5 flex min-h-11 w-full items-center justify-center gap-1.5 rounded-md px-3 text-sm font-medium transition-colors focus-visible:ring-[3px] focus-visible:outline-none"
>
{t("day.viewWholeDay")}
<ArrowRight className="size-4" aria-hidden="true" />
</button>
);
}
interface RugPoint {
timestamp: number;
}
+22 -10
View File
@@ -1,10 +1,10 @@
"use client";
import { useEffect, useState, type RefObject } from "react";
import { useEffect, useRef, useState, type RefObject } from "react";
import type { DateKey } from "@/lib/day/contract";
import { useCoarsePointer } from "./chart-day";
import { TooltipDayAction, useCoarsePointer } from "./chart-day";
import { openDay, useOpenDay } from "./day-layer-controller";
import { isOpenableDay } from "./day-url";
import { useTodayKey } from "./use-today-key";
@@ -13,10 +13,11 @@ import { useTodayKey } from "./use-today-key";
* The day view's doors on a calendar heatmap (mood, intake), the cell twin of
* `useChartDayLinks`:
*
* - a click, or a tap, on a cell opens its day; hovering a cell on a fine
* pointer shows its tooltip. A tap on a cell with nothing to open (no
* entry, or a day still ahead) pins its tooltip instead, so the cell
* still says what it is; a tap outside or Escape clears it;
* - fine pointer: hovering a cell shows its tooltip, a click opens its day;
* - touch: a tap pins the tooltip, and the tooltip's button ("View the
* whole day", the same calm 44 px row every chart's tooltip carries)
* opens it. Two steps, as on a chart: a cell is a small target. A tap
* outside or Escape clears the tooltip;
* - the open day's cell carries a dashed outline, the heatmap's form of
* the dashed line a chart draws through it (`OPEN_DAY_CELL`);
* - the keyboard and screen-reader way to a day is the heatmap's data
@@ -39,6 +40,8 @@ interface HeatmapTooltip {
text: string;
/** A touch tooltip stays after the finger lifts. */
pinned?: boolean;
/** The day a pinned tooltip offers to open. */
day?: DateKey;
}
export interface HeatmapCellInput {
@@ -51,6 +54,9 @@ export interface HeatmapCellInput {
export function useHeatmapDay(containerRef: RefObject<HTMLElement | null>) {
const [tooltip, setTooltip] = useState<HeatmapTooltip | null>(null);
// The pointer type of the last press decides whether the click that
// follows opens the day (mouse, pen) or only pinned the tooltip (touch).
const lastPointer = useRef<string>("mouse");
const today = useTodayKey();
const openKey = useOpenDay();
const coarse = useCoarsePointer();
@@ -104,17 +110,18 @@ export function useHeatmapDay(containerRef: RefObject<HTMLElement | null>) {
setTooltip((prev) => (prev?.pinned ? prev : null));
},
onPointerDown: (e: React.PointerEvent) => {
// A touch on a cell with a day to open opens it (the click below);
// any other cell pins its tooltip so the tap still answers.
if (e.pointerType !== "touch" || openable) return;
lastPointer.current = e.pointerType;
if (e.pointerType !== "touch") return;
setTooltip({
x: e.clientX,
y: e.clientY,
text: cell.describe(),
pinned: true,
day: openable ? cell.dateKey : undefined,
});
},
onClick: () => {
if (lastPointer.current === "touch") return;
if (!openable) return;
open(cell.dateKey);
},
@@ -129,7 +136,9 @@ export function useHeatmapDay(containerRef: RefObject<HTMLElement | null>) {
const tooltipNode = tooltip ? (
<div
data-slot="heatmap-tooltip"
className="bg-popover text-popover-foreground border-border pointer-events-none fixed z-50 max-w-[calc(100vw-1rem)] rounded-md border px-2.5 py-1.5 text-xs shadow-md"
className={`bg-popover text-popover-foreground border-border fixed z-50 max-w-[calc(100vw-1rem)] rounded-md border px-2.5 py-1.5 text-xs shadow-md ${
tooltip.day ? "pointer-events-auto" : "pointer-events-none"
}`}
// Clamp the left edge so a tap near the right border doesn't push
// the pinned label off-screen on a narrow viewport.
style={{
@@ -141,6 +150,9 @@ export function useHeatmapDay(containerRef: RefObject<HTMLElement | null>) {
}}
>
{tooltip.text}
{tooltip.day ? (
<TooltipDayAction onOpen={() => open(tooltip.day as DateKey)} />
) : null}
</div>
) : null;
@@ -216,6 +216,7 @@ export function MoodDimensionTrends({
active
label={fmt.dateShortSmartCalendar(String(row.date))}
rows={rows}
action={chartDays.tooltipAction(chartData.indexOf(row))}
/>
);
}}
@@ -469,6 +469,7 @@ export function SleepStageStackedBar({ breakdown }: SleepStageStackedBarProps) {
const row = payload[0]?.payload as
Record<string, number | string> | undefined;
const label = row?.label;
const rowIndex = row ? data.indexOf(row) : -1;
const napCount =
typeof row?.napCount === "number" ? row.napCount : 0;
// The footer's first line is the NIGHT. The nap is listed
@@ -549,6 +550,9 @@ export function SleepStageStackedBar({ breakdown }: SleepStageStackedBarProps) {
)}
</div>
)}
{chartDays.tooltipAction(
rowIndex === -1 ? undefined : rowIndex,
)}
</div>
);
}}
@@ -406,6 +406,7 @@ export function LabBiomarkerChart({
active
label={labShortDate(point.timestamp)}
rows={rows}
action={chartDays.tooltipAction(points.indexOf(point))}
/>
);
}}
@@ -378,11 +378,18 @@ export function EfficacyChart({
color: item.color ?? "var(--chart-1)",
};
});
const index =
typeof x === "number"
? displaySeries.findIndex((p) => p.t === x)
: -1;
return (
<RichChartTooltip
active
label={typeof x === "number" ? fmt.date(new Date(x)) : ""}
rows={rows}
action={chartDays.tooltipAction(
index === -1 ? undefined : index,
)}
/>
);
}}
@@ -326,6 +326,7 @@ export function DoseStrengthCurve({
active
label={fmt.dateShortSmart(new Date(point.t))}
rows={rows}
action={chartDays.tooltipAction(chartData.indexOf(point))}
/>
);
}}
@@ -183,6 +183,7 @@ export function AssessmentHistoryChart({
active
label={fmt.dateShortSmart(new Date(point.timestamp))}
rows={rowsOut}
action={chartDays.tooltipAction(points.indexOf(point))}
/>
);
}}