mirror of
https://github.com/bitbaum/bip-kit.git
synced 2026-12-04 04:57:05 +01:00
fix(react): readable Mermaid labels and a screen-edge long-form gutter (0.8.2) (#64)
- Mermaid labels take --bp-diagram-text (0.875em of the body) and a wide diagram shrinks only to --bp-diagram-min-text (max(12px, 0.75em)), then scrolls; labels measured on orangecat.ch at 390px were 4.1-9.4px. - Labels are measured in the face they are drawn in, so boxes stop cutting them short. - .bp-article pads to --bp-gutter minus the host column's own distance from the screen edge: no double gutter on a padded phone column, no zero gutter between 720px and the measure. Claude-Session: https://claude.ai/code/session_019MDtmvT2nQyPBK4EfWf3Rn Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
1 parent
8052af0697
commit
83e2e19f58
7 files changed
+242
-26
No files matched your search
@@ -7,6 +7,28 @@ Same code, same version, same repository; the package now lives under the
|
||||
`@bitbaum/bip-kit` and import from it (`bip-kit` stays installable and is deprecated
|
||||
once no adopter uses it). No API changed.
|
||||
|
||||
## 0.8.2 — 2026-11-13
|
||||
|
||||
- Mermaid labels were unreadable on a phone. A wide diagram was scaled to fit
|
||||
the column, and its labels with it: on orangecat.ch at 390px they measured
|
||||
4.1–9.4px against 17px body text (8.7px at 1440px). `MermaidBlock` now sets
|
||||
the label size from `--bp-diagram-text` (default 0.875em of the body) for
|
||||
flowcharts and sequence diagrams alike, lets a wide diagram shrink only until
|
||||
its labels reach `--bp-diagram-min-text` (default `max(12px, 0.75em)`), and
|
||||
past that the box scrolls sideways and takes keyboard focus. `.bp-mermaid` is
|
||||
no longer a centred flex row, which pushed an overflowing diagram's left side
|
||||
out of scroll reach.
|
||||
- Mermaid boxes cut their labels short ("Smal", "$1–5k / mont"): Mermaid
|
||||
measured text with `fontFamily: "inherit"` and drew it in the article's face.
|
||||
It now measures in the face it draws in.
|
||||
- The long-form gutter is measured to the screen edge: `--bp-gutter` (default
|
||||
`clamp(1rem, 4vw, 1.5rem)`) minus however far a centred host column already
|
||||
sits from it. A host column with its own padding no longer gets a second
|
||||
16px on a phone (OrangeCat's text sat 16px inside its own title), and a host
|
||||
with none keeps the gutter from 720px up to the measure, where it used to drop
|
||||
to zero and the text touched the edge. `.bp-article` is `content-box`, so a
|
||||
border-box reset no longer narrows the measure.
|
||||
|
||||
## 0.8.1 — 2026-11-13
|
||||
|
||||
- The share row's primary action ("Post on …") lost its label on hosts that set
|
||||
|
||||
@@ -140,7 +140,7 @@ import { MermaidBlock } from "@bitbaum/bip-kit/react/mermaid"; // requires the m
|
||||
|
||||
Without the override, mermaid blocks render their source in a styled `<pre>` — honest degradation, never a broken build.
|
||||
|
||||
`MermaidBlock` follows your theme however you switch it: an explicit `data-theme="light|dark"` on `<html>` wins, then a `light`/`dark` class (next-themes with `attribute="class"`, Tailwind's `.dark`), then the OS preference — and it re-renders live on any of them changing. Diagram colors resolve from your `--bp-*` tokens and are normalized to a form mermaid's color parser accepts, so tokens authored in `oklch()`/`lab()` (Tailwind v4 palettes) render correctly instead of blanking the diagram (fixed in 0.2.1).
|
||||
`MermaidBlock` follows your theme however you switch it: an explicit `data-theme="light|dark"` on `<html>` wins, then a `light`/`dark` class (next-themes with `attribute="class"`, Tailwind's `.dark`), then the OS preference — and it re-renders live on any of them changing. Diagram colors resolve from your `--bp-*` tokens and are normalized to a form mermaid's color parser accepts, so tokens authored in `oklch()`/`lab()` (Tailwind v4 palettes) render correctly instead of blanking the diagram (fixed in 0.2.1). Labels are sized from `--bp-diagram-text` and measured in the article's own face; a diagram wider than the column shrinks only until its labels reach `--bp-diagram-min-text`, then scrolls sideways (and takes keyboard focus) instead of shrinking further (0.8.2).
|
||||
|
||||
## The block vocabulary
|
||||
|
||||
@@ -464,6 +464,8 @@ Client islands (each tiny, dependency-free): `Toc` (sticky scroll-spy; hides its
|
||||
| `--bp-leading` | Body line-height (1.7) |
|
||||
| `--bp-measure` | Line length (70ch) |
|
||||
| `--bp-flow` | Vertical rhythm unit (1.5rem) |
|
||||
| `--bp-gutter` | Least space between the text and the screen edge (`clamp(1rem, 4vw, 1.5rem)`); a centred host column that already pads itself by this much gets none added |
|
||||
| `--bp-diagram-text` / `--bp-diagram-min-text` | Mermaid label size (0.875em of the body) and the smallest a wide diagram may shrink it to (`max(12px, 0.75em)`) before it scrolls sideways |
|
||||
| `--bp-radius` / `--bp-radius-sm` | Corner radii |
|
||||
| `--bp-bg` / `--bp-surface` | Page & raised-surface backgrounds |
|
||||
| `--bp-fg` / `--bp-fg-muted` / `--bp-fg-faint` | Text hierarchy |
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "@bitbaum/bip-kit",
|
||||
"version": "0.8.1",
|
||||
"version": "0.8.2",
|
||||
"description": "Building in Public, as a content contract: parse markdown into typed blocks and render them with a reference React renderer (@bitbaum/bip-kit/react) \u2014 blog, roadmap, changelog on your product site, bring your own design tokens.",
|
||||
"license": "MIT",
|
||||
"author": "Mao Nakamoto",
|
||||
|
||||
+94
-20
@@ -10,7 +10,10 @@
|
||||
* <ArticleBody blocks={blocks} components={{ mermaid: MermaidBlock }} />
|
||||
*
|
||||
* Theme colors resolve from the bp-* CSS vars at render time (ported from
|
||||
* loki's MermaidDiagram), so diagrams follow the consumer's tokens.
|
||||
* loki's MermaidDiagram), so diagrams follow the consumer's tokens. So does the
|
||||
* label size: `--bp-diagram-text` (default 0.875em of the body text), never
|
||||
* shrunk below `--bp-diagram-min-text` (default the larger of 12px and 0.75em)
|
||||
* to fit a phone; a diagram too wide for that scrolls sideways instead.
|
||||
*/
|
||||
|
||||
import type { ReactNode } from "react";
|
||||
@@ -123,6 +126,66 @@ function subscribeDark(onChange: () => void): () => void {
|
||||
}
|
||||
const getServerDark = () => false;
|
||||
|
||||
/**
|
||||
* Mermaid's config for one render, pure over its inputs (exported for tests).
|
||||
* `fontSize` is the label size in px, resolved from `--bp-diagram-text`: Mermaid
|
||||
* lays every box out around it, and flowcharts read it from `themeVariables`
|
||||
* while sequence diagrams read their own three keys, so all of them are set.
|
||||
* `fontFamily` is the face the labels are DRAWN in: Mermaid sizes each box by
|
||||
* measuring its text, and measured with "inherit" it measured the page's face
|
||||
* rather than the article's, so labels were cut off ("Smal", "$1–5k / mont").
|
||||
*/
|
||||
export function mermaidConfig(input: {
|
||||
dark: boolean;
|
||||
fontSize: number;
|
||||
fontFamily?: string;
|
||||
themeVariables?: Record<string, string>;
|
||||
}) {
|
||||
const px = Math.round(input.fontSize * 100) / 100;
|
||||
return {
|
||||
startOnLoad: false,
|
||||
theme: input.dark ? ("dark" as const) : ("default" as const),
|
||||
fontFamily: input.fontFamily || "inherit",
|
||||
fontSize: px,
|
||||
sequence: { actorFontSize: px, messageFontSize: px, noteFontSize: px },
|
||||
themeVariables: {
|
||||
...input.themeVariables,
|
||||
fontSize: `${px}px`,
|
||||
...(input.fontFamily ? { fontFamily: input.fontFamily } : {}),
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* The narrowest a rendered diagram may be drawn, in px (exported for tests).
|
||||
* Mermaid draws its SVG at `viewBoxWidth` with labels at `fontSize`; scaling
|
||||
* it down to fit a phone scales the labels with it. It may shrink until they
|
||||
* reach `minFontSize` and no further: past that the box scrolls sideways.
|
||||
*/
|
||||
export function diagramMinWidth(input: {
|
||||
viewBoxWidth: number;
|
||||
fontSize: number;
|
||||
minFontSize: number;
|
||||
}): number {
|
||||
const { viewBoxWidth, fontSize, minFontSize } = input;
|
||||
if (!(viewBoxWidth > 0) || !(fontSize > 0)) return 0;
|
||||
const scale = Math.min(1, Math.max(0, minFontSize) / fontSize);
|
||||
return Math.ceil(viewBoxWidth * scale);
|
||||
}
|
||||
|
||||
// Resolves a length custom property to px by letting the browser compute it as
|
||||
// a font-size inside the diagram box, so `em` means the body text there.
|
||||
function resolveFontSizeVar(host: HTMLElement, cssVar: string, fallback: number): number {
|
||||
const el = document.createElement("span");
|
||||
el.style.position = "absolute";
|
||||
el.style.visibility = "hidden";
|
||||
el.style.fontSize = `var(${cssVar})`;
|
||||
host.appendChild(el);
|
||||
const px = parseFloat(getComputedStyle(el).fontSize);
|
||||
el.remove();
|
||||
return Number.isFinite(px) && px > 0 ? px : fallback;
|
||||
}
|
||||
|
||||
const LIGHT = { surface: "#f6f6f4", text: "#1a1a18", line: "#8a8a86" };
|
||||
const DARK = { surface: "#26262a", text: "#e8e8e6", line: "#8a8a90" };
|
||||
|
||||
@@ -135,35 +198,46 @@ export function MermaidBlock({ code }: { code: string }): ReactNode {
|
||||
let cancelled = false;
|
||||
import("mermaid")
|
||||
.then((m) => {
|
||||
if (cancelled) return;
|
||||
if (cancelled || !ref.current) return;
|
||||
const fallback = dark ? DARK : LIGHT;
|
||||
const base = {
|
||||
startOnLoad: false,
|
||||
theme: dark ? ("dark" as const) : ("default" as const),
|
||||
fontFamily: "inherit",
|
||||
};
|
||||
const fontSize = resolveFontSizeVar(ref.current, "--bp-diagram-text", 15);
|
||||
const minFontSize = resolveFontSizeVar(ref.current, "--bp-diagram-min-text", 12);
|
||||
const fontFamily = getComputedStyle(ref.current).fontFamily;
|
||||
try {
|
||||
m.default.initialize({
|
||||
...base,
|
||||
themeVariables: {
|
||||
background: "transparent",
|
||||
primaryColor: resolveColorVar("--bp-surface", fallback.surface),
|
||||
primaryTextColor: resolveColorVar("--bp-fg", fallback.text),
|
||||
lineColor: resolveColorVar("--bp-fg-muted", fallback.line),
|
||||
edgeLabelBackground: resolveColorVar("--bp-bg", fallback.surface),
|
||||
clusterBkg: resolveColorVar("--bp-surface", fallback.surface),
|
||||
},
|
||||
});
|
||||
m.default.initialize(
|
||||
mermaidConfig({
|
||||
dark,
|
||||
fontSize,
|
||||
fontFamily,
|
||||
themeVariables: {
|
||||
background: "transparent",
|
||||
primaryColor: resolveColorVar("--bp-surface", fallback.surface),
|
||||
primaryTextColor: resolveColorVar("--bp-fg", fallback.text),
|
||||
lineColor: resolveColorVar("--bp-fg-muted", fallback.line),
|
||||
edgeLabelBackground: resolveColorVar("--bp-bg", fallback.surface),
|
||||
clusterBkg: resolveColorVar("--bp-surface", fallback.surface),
|
||||
},
|
||||
}),
|
||||
);
|
||||
} catch {
|
||||
// A theme value Mermaid cannot parse must cost the reader the THEME,
|
||||
// not the diagram. initialize() runs outside render()'s promise
|
||||
// chain, so an uncaught throw here silently skipped rendering.
|
||||
m.default.initialize(base);
|
||||
m.default.initialize(mermaidConfig({ dark, fontSize, fontFamily }));
|
||||
}
|
||||
m.default
|
||||
.render(`bp-mermaid-${id}-${dark ? "d" : "l"}`, code)
|
||||
.then(({ svg }: { svg: string }) => {
|
||||
if (!cancelled && ref.current) ref.current.innerHTML = svg;
|
||||
const box = ref.current;
|
||||
if (cancelled || !box) return;
|
||||
box.innerHTML = svg;
|
||||
const drawn = box.querySelector("svg");
|
||||
const viewBoxWidth = drawn?.viewBox?.baseVal?.width ?? 0;
|
||||
const minWidth = diagramMinWidth({ viewBoxWidth, fontSize, minFontSize });
|
||||
if (drawn && minWidth > 0) drawn.style.minWidth = `${minWidth}px`;
|
||||
// A diagram wider than the box scrolls; a keyboard has to reach it.
|
||||
if (box.scrollWidth > box.clientWidth) box.tabIndex = 0;
|
||||
else box.removeAttribute("tabindex");
|
||||
})
|
||||
.catch(() => {
|
||||
if (!cancelled && ref.current) {
|
||||
|
||||
+30
-4
@@ -23,6 +23,13 @@
|
||||
--bp-leading: 1.7;
|
||||
--bp-measure: 70ch;
|
||||
--bp-flow: 1.5rem; /* vertical rhythm unit */
|
||||
/* The least space between the text and the edge of the SCREEN. A host column
|
||||
that already pads itself by this much gets none added (see .bp-article). */
|
||||
--bp-gutter: clamp(1rem, 4vw, 1.5rem);
|
||||
/* Diagram labels, relative to the body text they sit in, and the smallest a
|
||||
wide diagram may shrink them to before it scrolls sideways instead. */
|
||||
--bp-diagram-text: 0.875em;
|
||||
--bp-diagram-min-text: max(12px, 0.75em);
|
||||
|
||||
/* Shape */
|
||||
--bp-radius: 12px;
|
||||
@@ -316,10 +323,18 @@
|
||||
|
||||
/* ── Article base ───────────────────────────────────────────────────── */
|
||||
|
||||
/* The gutter is measured to the screen edge, not added to the host's own:
|
||||
(100vw - 100%) / 2 is how far a centred host column already sits from each
|
||||
edge (a padding percentage is of the containing block), so a phone column
|
||||
with 20px of its own padding gets no second 16px, and a host with none gets
|
||||
the whole gutter at every width, including 720px to the measure, where it
|
||||
used to drop to zero and the text touched the glass. content-box keeps the
|
||||
measure the measure under a border-box reset. */
|
||||
.bp-article {
|
||||
box-sizing: content-box;
|
||||
max-width: var(--bp-measure);
|
||||
margin-inline: auto;
|
||||
padding-inline: 1rem;
|
||||
padding-inline: max(0px, calc(var(--bp-gutter) - (100vw - 100%) / 2));
|
||||
font-family: var(--bp-font-sans);
|
||||
font-size: var(--bp-size);
|
||||
line-height: var(--bp-leading);
|
||||
@@ -332,7 +347,6 @@
|
||||
@media (min-width: 720px) {
|
||||
.bp-article {
|
||||
--bp-size: 1.125rem; /* 18px on wide screens */
|
||||
padding-inline: 0;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -768,9 +782,11 @@
|
||||
|
||||
/* ── Mermaid & math ─────────────────────────────────────────────────── */
|
||||
|
||||
/* A block, not a centred flex row: a flex row centres an overflowing diagram
|
||||
past its own left edge, where no scrollbar can reach. The SVG centres itself
|
||||
with auto margins, which fall to zero once it is wider than the box. */
|
||||
.bp-mermaid {
|
||||
display: flex;
|
||||
justify-content: center;
|
||||
display: block;
|
||||
overflow-x: auto;
|
||||
margin: calc(var(--bp-flow) * 1.2) 0;
|
||||
padding: 1rem;
|
||||
@@ -779,11 +795,20 @@
|
||||
background: var(--bp-surface);
|
||||
}
|
||||
|
||||
/* MermaidBlock sets an inline min-width so the labels never shrink below
|
||||
--bp-diagram-min-text; past that the box scrolls. */
|
||||
.bp-mermaid svg {
|
||||
display: block;
|
||||
margin-inline: auto;
|
||||
max-width: 100%;
|
||||
height: auto;
|
||||
}
|
||||
|
||||
.bp-mermaid:focus-visible {
|
||||
outline: 2px solid var(--bp-accent);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
.bp-mermaid--fallback,
|
||||
.bp-mermaid--error {
|
||||
margin: 0;
|
||||
@@ -1097,6 +1122,7 @@
|
||||
|
||||
.bp-article {
|
||||
max-width: none;
|
||||
padding-inline: 0;
|
||||
font-size: 11pt;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
import { test } from "node:test";
|
||||
import assert from "node:assert/strict";
|
||||
import { diagramMinWidth, mermaidConfig } from "../dist/react/mermaid.js";
|
||||
|
||||
/**
|
||||
* Diagram text size. On a 390px phone a 1,000px-wide sequence diagram was
|
||||
* scaled to fit the column and its labels came out at 4–5px against 17px body
|
||||
* text (measured on orangecat.ch). The sizing logic is pure; the browser part
|
||||
* only feeds it the viewBox width and the two resolved font sizes.
|
||||
*/
|
||||
|
||||
test("a narrow diagram is never constrained", () => {
|
||||
assert.equal(diagramMinWidth({ viewBoxWidth: 120, fontSize: 15, minFontSize: 12.75 }), 102);
|
||||
});
|
||||
|
||||
test("a wide diagram shrinks only until its labels reach the floor", () => {
|
||||
// 15px labels, 12.75px floor: it may be drawn at 85% and no smaller.
|
||||
const min = diagramMinWidth({ viewBoxWidth: 1000, fontSize: 15, minFontSize: 12.75 });
|
||||
assert.equal(min, 850);
|
||||
assert.ok((min / 1000) * 15 >= 12.75, "labels drawn at the minimum width stay at the floor");
|
||||
});
|
||||
|
||||
test("a floor at or above the label size forbids any shrinking, never enlarges", () => {
|
||||
assert.equal(diagramMinWidth({ viewBoxWidth: 640, fontSize: 12, minFontSize: 16 }), 640);
|
||||
});
|
||||
|
||||
test("an unmeasurable diagram gets no min-width rather than NaN", () => {
|
||||
assert.equal(diagramMinWidth({ viewBoxWidth: 0, fontSize: 15, minFontSize: 12 }), 0);
|
||||
assert.equal(diagramMinWidth({ viewBoxWidth: Number.NaN, fontSize: 15, minFontSize: 12 }), 0);
|
||||
assert.equal(diagramMinWidth({ viewBoxWidth: 500, fontSize: 0, minFontSize: 12 }), 0);
|
||||
});
|
||||
|
||||
test("the label size reaches flowcharts and sequence diagrams alike", () => {
|
||||
const config = mermaidConfig({ dark: false, fontSize: 15.75 });
|
||||
assert.equal(config.fontSize, 15.75);
|
||||
assert.equal(config.themeVariables.fontSize, "15.75px");
|
||||
assert.deepEqual(config.sequence, {
|
||||
actorFontSize: 15.75,
|
||||
messageFontSize: 15.75,
|
||||
noteFontSize: 15.75,
|
||||
});
|
||||
assert.equal(config.theme, "default");
|
||||
assert.equal(mermaidConfig({ dark: true, fontSize: 15 }).theme, "dark");
|
||||
});
|
||||
|
||||
test("labels are measured in the face they are drawn in", () => {
|
||||
const face = 'Inter, "Inter Fallback", system-ui, sans-serif';
|
||||
const config = mermaidConfig({ dark: false, fontSize: 15, fontFamily: face });
|
||||
assert.equal(config.fontFamily, face);
|
||||
assert.equal(config.themeVariables.fontFamily, face);
|
||||
assert.equal(mermaidConfig({ dark: false, fontSize: 15 }).fontFamily, "inherit");
|
||||
});
|
||||
|
||||
test("theme colors pass through beside the size", () => {
|
||||
const config = mermaidConfig({
|
||||
dark: false,
|
||||
fontSize: 15,
|
||||
themeVariables: { primaryColor: "#f4f4f2" },
|
||||
});
|
||||
assert.equal(config.themeVariables.primaryColor, "#f4f4f2");
|
||||
});
|
||||
@@ -269,3 +269,34 @@ test("ink on a --bp-fg fill never comes from --bp-bg, which hosts may make trans
|
||||
"--bp-on-fg is set for light, dark media and [data-theme=dark]",
|
||||
);
|
||||
});
|
||||
|
||||
test("the article gutter is measured to the screen edge, at every width", () => {
|
||||
const article = ruleBody(".bp-article");
|
||||
assert.ok(article, ".bp-article rule not found in styles.css");
|
||||
assert.match(
|
||||
article,
|
||||
/padding-inline:\s*max\(0px,\s*calc\(var\(--bp-gutter\)\s*-\s*\(100vw - 100%\)\s*\/\s*2\)\)/,
|
||||
"the gutter must subtract the host column's own distance from the screen edge, " +
|
||||
"or a padded host column gets the gutter twice",
|
||||
);
|
||||
assert.match(article, /box-sizing:\s*content-box/, "a border-box reset would eat the measure");
|
||||
assert.doesNotMatch(
|
||||
css,
|
||||
/@media \(min-width: 720px\)\s*\{\s*\.bp-article\s*\{[^}]*padding-inline:\s*0/,
|
||||
"the gutter dropped to zero at 720px and text touched the screen edge up to the measure",
|
||||
);
|
||||
assert.match(css, /--bp-gutter:/, "--bp-gutter needs a default");
|
||||
});
|
||||
|
||||
test("a wide diagram scrolls from its left edge instead of being centred past it", () => {
|
||||
const box = ruleBody(".bp-mermaid");
|
||||
assert.ok(box, ".bp-mermaid rule not found in styles.css");
|
||||
assert.match(box, /overflow-x:\s*auto/);
|
||||
assert.doesNotMatch(
|
||||
box,
|
||||
/justify-content:\s*center/,
|
||||
"a centred flex row pushes an overflowing diagram's left side out of scroll reach",
|
||||
);
|
||||
assert.match(css, /--bp-diagram-text:/, "--bp-diagram-text needs a default");
|
||||
assert.match(css, /--bp-diagram-min-text:\s*max\(12px/, "diagram labels floor at 12px or more");
|
||||
});
|
||||
Reference in new issue
Block a user