mirror of
https://github.com/MBombeck/HealthLog.git
synced 2026-11-19 00:12:18 +00:00
fix(charts): keep axis unit labels on one line; 44 px date and time triggers
Recharts word-wraps a tick label that it measures wider than its axis, and it measures in the page's body font rather than the tick's 11 px. "62 bpm" on the pulse chart under a workout and "106 mg/dL" on the glucose page therefore broke onto two lines at every width, and the pulse chart's second line sat on the first x tick. Axis units are now joined with a no-break space through `axisUnitSuffix`, so the label is one word and stays on one line. A guard fails when a chart axis joins a unit with a plain space. The calendar and clock buttons in the date and time fields paint at 32 px; a `::before` halo now gives them a 44 px touch target without changing the field.
This commit is contained in:
1 parent
801295f55a
commit
debd44a75b
9 files changed
+109
-6
No files matched your search
@@ -0,0 +1,56 @@
|
||||
import { readFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { describe, expect, it } from "vitest";
|
||||
|
||||
import {
|
||||
stripComments,
|
||||
walkSourceFiles,
|
||||
} from "@/__tests__/helpers/source-files";
|
||||
import { axisUnitSuffix } from "../axis-unit";
|
||||
|
||||
const COMPONENTS = join(process.cwd(), "src", "components");
|
||||
|
||||
/**
|
||||
* Every `<XAxis … />` / `<YAxis … />` tag's `unit=` value in the tree. The tag
|
||||
* runs to its `/>`: stopping at the first `>` would end it inside an arrow
|
||||
* function prop and miss a `unit` written after one.
|
||||
*/
|
||||
function axisUnitProps(): Array<{ file: string; value: string }> {
|
||||
const out: Array<{ file: string; value: string }> = [];
|
||||
for (const rel of walkSourceFiles(COMPONENTS, {
|
||||
floor: 300,
|
||||
extensions: [".tsx"],
|
||||
})) {
|
||||
if (rel.includes("__tests__")) continue;
|
||||
const src = stripComments(readFileSync(join(COMPONENTS, rel), "utf8"));
|
||||
for (const tag of src.matchAll(/<[XY]Axis\b[\s\S]*?\/>/g)) {
|
||||
const unit = /\bunit=(\{[\s\S]*?\}|"[^"]*")/.exec(tag[0]);
|
||||
if (unit) out.push({ file: rel, value: unit[1] });
|
||||
}
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
describe("axis unit suffix", () => {
|
||||
it("joins the unit with a no-break space", () => {
|
||||
expect(axisUnitSuffix("bpm")).toBe(" bpm");
|
||||
expect(axisUnitSuffix("")).toBeUndefined();
|
||||
expect(axisUnitSuffix(null)).toBeUndefined();
|
||||
});
|
||||
|
||||
it("finds the axis units it is meant to police", () => {
|
||||
// A matcher that finds nothing would pass the check below by default.
|
||||
expect(axisUnitProps().length).toBeGreaterThanOrEqual(6);
|
||||
});
|
||||
|
||||
it("no chart axis joins a unit with a breaking space", () => {
|
||||
// Recharts wraps a tick label at a breaking space when it measures the
|
||||
// label wider than the axis, and it measures in the body font: "62 bpm"
|
||||
// broke onto two lines and its second line sat on the first x tick.
|
||||
const offenders = axisUnitProps()
|
||||
.filter(({ value }) => /["`] /.test(value))
|
||||
.map(({ file, value }) => `${file}: unit=${value}`);
|
||||
expect(offenders).toEqual([]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,15 @@
|
||||
/**
|
||||
* The unit suffix a chart axis appends to each tick value ("62 bpm").
|
||||
*
|
||||
* Joined with a no-break space, never a plain one. Recharts word-wraps a
|
||||
* tick label that is wider than the axis, and it measures the label in the
|
||||
* page's body font rather than the tick's own 11 px, so "62 bpm" and
|
||||
* "106 mg/dL" measured wider than their axis and broke onto two lines on
|
||||
* every screen. The second line dropped onto the first x tick. A label with
|
||||
* no breaking space is one word and stays on one line.
|
||||
*/
|
||||
export function axisUnitSuffix(
|
||||
unit: string | null | undefined,
|
||||
): string | undefined {
|
||||
return unit ? ` ${unit}` : undefined;
|
||||
}
|
||||
Reference in new issue
Block a user