fix(charts): match chart loading shells to the charts they become

The footer reserve under a day-linked chart's loading shell still held the
caption line that was dropped from those charts, so every such shell stood
20 px taller than its chart (40 px on a phone). The dashboard's mood chart
loaded behind the generic shell, which is about 30 px shorter than the
mood card. When the shared chart chunk arrived late the dashboard's chart
row changed height under a reader already scrolled past it; the tablet
shell journey measured <main> right after such a swap and found it no
longer at its end.

The reserve now holds only the day rug, and the mood chart gets a shell
built from the same card primitives and boxes. The tablet journey also waits
for every chart cell to reveal and every loading shell to go before it
scrolls to the end, instead of trusting two equal heights 250 ms apart.
This commit is contained in:
Marc-André Bombeck committed 2026-11-15 10:58:13 +01:00
1 parent 23a6f85b0f
commit e84c358fdf
5 files changed
+103 -16

No files matched your search

+5
View File
@@ -53,6 +53,11 @@
switching tab had started a moment before the switch could answer with the
record being left and overwrite the switch the other tabs were waiting on.
Such an answer is now discarded and asked again.
- **Charts no longer jump while they load.** The placeholder of every chart
that links its days was 20 px taller than the chart it turned into (40 px
on a phone), and the mood chart's placeholder was about 30 px shorter, so
the dashboard and the metric pages shifted under the reader when the charts
arrived.
### Security
+36
View File
@@ -54,6 +54,42 @@ async function openDashboard(page: Page) {
).toBeVisible();
// The module-gated destinations join the rail once `/api/auth/me` lands.
await expect(page.locator('aside nav a[href="/medications"]')).toHaveCount(1);
await expectChartRowSettled(page);
}
/**
* The dashboard's chart row has swapped every loading shell for its chart.
*
* The charts are one lazily loaded chunk, and each cell holds a skeleton until
* it lands and the row reveals. The swap changes the row's height, so a page
* measured before it is not the page the reader ends up with: on a loaded
* runner the chunk arrived a second after the dashboard painted, after
* `scrollMainToEnd` had already seen two equal heights, and `<main>` was no
* longer at its end when it was measured. Waited for on the cells' own state
* rather than on time.
*/
async function expectChartRowSettled(page: Page) {
await expect
.poll(
() =>
page.evaluate(() => {
const main = document.getElementById("main-content")!;
const cells = main.querySelectorAll(
'[data-slot="dashboard-chart-cell"]',
);
const held = main.querySelectorAll(
'[data-slot="dashboard-chart-cell"]:not([data-revealed="true"])',
);
const shells = main.querySelectorAll('[data-slot="chart-skeleton"]');
return {
cells: cells.length > 0,
held: held.length,
shells: shells.length,
};
}),
{ timeout: 15_000 },
)
.toEqual({ cells: true, held: 0, shells: 0 });
}
/** The shell fills the window exactly and the document has nothing to scroll. */
+5 -2
View File
@@ -12,7 +12,10 @@ import { queryKeys } from "@/lib/query-keys";
import { useTranslations } from "@/lib/i18n/context";
import { useInsightsLayoutPrefs } from "@/hooks/use-insights-layout-prefs";
import { Button } from "@/components/ui/button";
import { ChartSkeleton } from "@/components/charts/chart-skeleton";
import {
ChartSkeleton,
MoodChartSkeleton,
} from "@/components/charts/chart-skeleton";
import { MetricEmptyState } from "@/components/insights/metric-empty-state";
import { MetricTargetSummary } from "@/components/insights/metric-target-summary";
import {
@@ -54,7 +57,7 @@ const MoodChart = dynamic(
import("@/components/charts/chart-runtime").then((mod) => ({
default: mod.MoodChart,
})),
{ ssr: false, loading: () => <ChartSkeleton /> },
{ ssr: false, loading: () => <MoodChartSkeleton /> },
);
/**
+52 -12
View File
@@ -2,6 +2,7 @@
import { useEffect, useState } from "react";
import { Card, CardContent, CardHeader } from "@/components/ui/card";
import { Skeleton } from "@/components/ui/skeleton";
import { cn } from "@/lib/utils";
import { useTranslations } from "@/lib/i18n/context";
@@ -50,12 +51,16 @@ const SLOW_HINT_DELAY_MS = 3_000;
/**
* The footer a loaded `<HealthChart>` paints under its plot: the row of day
* dots and its caption on a day-linked chart, and the collapsed "show as
* table" disclosure. A loading shell without it was ~100 px shorter than the
* card it turned into (CLS on every metric page, the BMI page most visibly),
* so the shells reserve the same boxes: `h-3` rug, `mt-1` caption (two lines
* on a phone, one from `sm`), and the disclosure's `mt-3 border-t pt-3` frame
* around its `min-h-11` button.
* dots on a day-linked chart, and the collapsed "show as table" disclosure. A
* loading shell without it was ~100 px shorter than the card it turned into
* (CLS on every metric page, the BMI page most visibly), so the shells reserve
* the same boxes: the `h-3` rug, and the disclosure's `mt-3 border-t pt-3`
* frame around its `min-h-11` button.
*
* The rug is all a day-linked chart paints under its plot. It used to carry a
* one-line caption as well, and the reserve kept that line after the caption
* went: every day-linked shell stood 20 px taller than its chart (40 px on a
* phone) and the page shrank under the reader when the chart arrived.
*/
export function ChartFooterReserve({
dayLinks = false,
@@ -67,12 +72,7 @@ export function ChartFooterReserve({
if (!dayLinks && !dataTable) return null;
return (
<div aria-hidden="true" data-slot="chart-footer-reserve">
{dayLinks ? (
<>
<div className="h-3" />
<div className="mt-1 h-9 sm:h-4" />
</>
) : null}
{dayLinks ? <div className="h-3" /> : null}
{dataTable ? (
<div className="border-border mt-3 border-t pt-3">
<div className="h-11" />
@@ -172,3 +172,43 @@ export function ChartSkeleton({
</div>
);
}
/**
* The loading shell for a full-size, day-linked `<MoodChart>`.
*
* The mood chart is drawn in a `Card` (vertical padding and a gap between
* header and plot, where `ChartSkeleton` has padding and a margin), its plot
* band is 200 / 220 px rather than 240 / 280, and it paints the day rug and
* the collapsed data-table disclosure under the plot. Shown in the generic
* shell, the card grew by about 30 px when the chart chunk arrived, under a
* reader already scrolled past it. So it gets the same primitives and the
* same boxes.
*/
export function MoodChartSkeleton() {
const { t } = useTranslations();
return (
<Card
data-slot="chart-skeleton"
role="status"
aria-busy="true"
aria-live="polite"
>
<span className="sr-only">{t("charts.loadingLabel")}</span>
<CardHeader>
<div className="flex flex-col gap-2 sm:flex-row sm:items-center sm:justify-between">
<Skeleton className="h-5 w-32" />
<div className="flex h-11 items-center gap-2">
<Skeleton className="h-6 w-12" />
<Skeleton className="h-6 w-12" />
<Skeleton className="h-6 w-12" />
<Skeleton className="h-6 w-12" />
</div>
</div>
</CardHeader>
<CardContent>
<Skeleton className="h-[var(--chart-height,200px)] w-full md:h-[var(--chart-height-md,220px)]" />
<ChartFooterReserve dayLinks dataTable />
</CardContent>
</Card>
);
}
@@ -4,7 +4,10 @@ import type { ComponentProps, ReactNode } from "react";
import dynamic from "next/dynamic";
import { ChartErrorBoundary } from "@/components/charts/chart-error-state";
import { ChartSkeleton } from "@/components/charts/chart-skeleton";
import {
ChartSkeleton,
MoodChartSkeleton,
} from "@/components/charts/chart-skeleton";
import { HealthChartDynamic } from "@/components/charts/health-chart-dynamic";
import { RecentWorkoutsTile } from "@/components/dashboard/recent-workouts-tile";
import { RecentAchievementsCard } from "@/components/gamification/recent-achievements-card";
@@ -26,7 +29,7 @@ const MoodChartLazy = dynamic(
importWithRetry(() => import("@/components/charts/chart-runtime")).then(
(mod) => ({ default: mod.MoodChart }),
),
{ ssr: false, loading: () => <ChartSkeleton /> },
{ ssr: false, loading: () => <MoodChartSkeleton /> },
);
const MoodChart = (props: ComponentProps<typeof MoodChartLazy>) => (
<ChartErrorBoundary>