Skip to content

Commit 4a8a73b

Browse files
fix(web): size the list gutter from the measured marker, not 3ch
3ch over-reserves in proportional fonts, where the period and space are narrower than a digit, so lists whose markers already fit were indented further and rewrapped. Measure `0. ` in the interface font instead and keep 1.25rem whenever the marker fits. AI-assisted; verified with the tests and tools listed above.
1 parent 19552e1 commit 4a8a73b

3 files changed

Lines changed: 60 additions & 8 deletions

File tree

‎apps/web/src/appearanceFonts.browser.test.ts‎

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,4 +52,40 @@ describe("applyAppearanceFontVariables", () => {
5252
else root.setAttribute("style", style);
5353
}
5454
});
55+
56+
it("widens the numbered-list gutter only when the interface font's marker would clip", () => {
57+
const root = document.documentElement;
58+
const style = root.getAttribute("style");
59+
const gutter = (sans: string) => {
60+
applyAppearanceFontVariables(root, {
61+
sans,
62+
code: "",
63+
composer: "",
64+
sizeInterface: 16,
65+
sizePrompt: 14,
66+
sizeCode: 13,
67+
smoothing: true,
68+
separate: false,
69+
});
70+
const markdown = document.createElement("div");
71+
markdown.className = "chat-markdown";
72+
markdown.style.fontSize = "14px";
73+
const list = document.createElement("ol");
74+
markdown.append(list);
75+
document.body.append(markdown);
76+
try {
77+
return Number.parseFloat(getComputedStyle(list).paddingInlineStart);
78+
} finally {
79+
markdown.remove();
80+
}
81+
};
82+
try {
83+
// `0. ` in a 14px monospace font is about 25px, past the 20px default.
84+
expect(gutter("monospace")).toBeGreaterThan(24);
85+
expect(gutter("sans-serif")).toBe(20);
86+
} finally {
87+
if (style === null) root.removeAttribute("style");
88+
else root.setAttribute("style", style);
89+
}
90+
});
5591
});

‎apps/web/src/appearanceFonts.ts‎

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ export function applyAppearanceFontVariables(
8888
}
8989
}
9090

91+
const markerWidth = probeWidth(
92+
appearanceFontStack(preferences.sans, DEFAULT_SANS_FONT_STACK),
93+
LIST_MARKER_PROBE_TEXT,
94+
);
95+
if (markerWidth === null) {
96+
root.style.removeProperty("--list-marker-sans");
97+
} else {
98+
root.style.setProperty("--list-marker-sans", `${(markerWidth / PROBE_FONT_SIZE).toFixed(3)}em`);
99+
}
100+
91101
root.style.fontSize = `${clampInterfaceFontSize(preferences.sizeInterface)}px`;
92102
root.style.setProperty(
93103
"--font-size-prompt",
@@ -127,15 +137,18 @@ function clampCodeFontSize(value: number): number {
127137
}
128138

129139
const FONT_PROBE_TEXT = "mmmmmmmmMMWli1O0@# fjord";
140+
// The widest single-digit `ol` marker; CSS can't measure it to size the list gutter.
141+
const LIST_MARKER_PROBE_TEXT = "0. ";
142+
const PROBE_FONT_SIZE = 16;
130143
let fontProbeContext: CanvasRenderingContext2D | null | undefined;
131144

132-
function probeWidth(fontList: string): number | null {
145+
function probeWidth(fontList: string, text = FONT_PROBE_TEXT): number | null {
133146
if (fontProbeContext === undefined) {
134147
fontProbeContext = document.createElement("canvas").getContext("2d");
135148
}
136149
if (fontProbeContext === null) return null;
137-
fontProbeContext.font = `16px ${fontList}`;
138-
return fontProbeContext.measureText(FONT_PROBE_TEXT).width;
150+
fontProbeContext.font = `${PROBE_FONT_SIZE}px ${fontList}`;
151+
return fontProbeContext.measureText(text).width;
139152
}
140153

141154
/** Probes canvas metrics because document.fonts.check() accepts uninstalled families. */

‎apps/web/src/index.css‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1933,12 +1933,11 @@ code {
19331933
list-style-type: disc;
19341934
}
19351935

1936-
/* Outside markers paint past the padding box, so the gutter must fit `1. `
1937-
(3ch in a monospace font; CSS can't measure it, so every font gets 3ch).
1938-
ChatMarkdown widens it inline for multi-digit markers; setting it here stops
1939-
a nested ol inheriting that. `ch` resolves against each list's own font. */
1936+
/* Outside markers paint past the padding box, so the gutter must fit `0. `,
1937+
measured per font in appearanceFonts. ChatMarkdown widens it inline for
1938+
multi-digit markers; setting it here stops a nested ol inheriting that. */
19401939
:is(.chat-markdown, .composer-tiptap) ol {
1941-
--list-gutter: max(1.25rem, 3ch);
1940+
--list-gutter: max(1.25rem, var(--list-marker-sans, 0px));
19421941
padding-inline-start: var(--list-gutter, 1.25rem);
19431942
list-style-type: decimal;
19441943
}
@@ -1955,6 +1954,10 @@ code {
19551954

19561955
/* The prompt font and size can differ from the interface, so its gutter must
19571956
also follow the prompt's font metrics. */
1957+
.composer-tiptap ol {
1958+
--list-gutter: max(1.25rem, 3ch);
1959+
}
1960+
19581961
.composer-tiptap ol:has(> li[data-marker-width="3"]) {
19591962
--list-gutter: max(1.25rem, 4ch);
19601963
}

0 commit comments

Comments
 (0)