Skip to content

Commit bb02fc5

Browse files
fix(ui): make chat inline code readable on dark themes (#121376)
Every dark palette defines --secondary with the same hex as --card, so the markdown code chip painted with --secondary collapsed into the surface it sat on (1.00:1 against a user bubble, 1.06-1.08:1 against the flat assistant column) and --border was too close to the background to draw an edge. Light mode never showed the bug because it overrode both properties. Promote the light override into the single canonical rule: code chips and code blocks paint --bg-muted with a --border-strong edge in every theme, and the mode-specific overrides (including the raw rgba border) are deleted. Also fixes the theme-contrast guard, which merged repeated `:root` blocks by overwriting instead of merging, so the default `dark` theme resolved to an empty token map and skipped every assertion. A new case reads the chip tokens out of the shipped rule and asserts surface and border separation from --card and --bg on all six themes.
1 parent 4fe078f commit bb02fc5

2 files changed

Lines changed: 67 additions & 15 deletions

File tree

‎ui/src/styles/base-theme-contrast.node.test.ts‎

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,20 @@ const SURFACE_TOKENS = ["--bg", "--bg-elevated", "--bg-muted", "--card", "--pane
3131

3232
const AA_NORMAL_TEXT_MIN = 4.5;
3333

34+
/*
35+
* Separation guardrail for the markdown code chip.
36+
*
37+
* Text contrast was never the failure mode here: the chip surface itself
38+
* collapsed. Every dark palette sets `--secondary` to the same hex as `--card`,
39+
* so a chip painted with it was invisible inside a user bubble while light mode
40+
* (which overrode the surface) looked correct. The chip tokens are read out of
41+
* the live rule so swapping them back for a collapsing pair fails here.
42+
*/
43+
const CODE_CHIP_RULE = ".chat-text :where(:not(pre) > code)";
44+
const CODE_CHIP_HOST_SURFACES = ["--card", "--bg"] as const;
45+
const CHIP_SURFACE_MIN_STEP = 1.05;
46+
const CHIP_BORDER_MIN_STEP = 1.25;
47+
3448
type TokenMap = Map<string, string>;
3549

3650
function parseThemeBlocks(baseCss: string): Map<string, TokenMap> {
@@ -39,7 +53,10 @@ function parseThemeBlocks(baseCss: string): Map<string, TokenMap> {
3953
for (const match of baseCss.matchAll(blockPattern)) {
4054
const selector = match[1] ?? "";
4155
const body = match[2] ?? "";
42-
const tokens: TokenMap = new Map();
56+
// base.css declares `:root` more than once (palette, then the standalone
57+
// --cursor-action blocks). Merging keeps the palette; overwriting made the
58+
// default `dark` theme resolve to an empty map and skip every assertion.
59+
const tokens: TokenMap = blocks.get(selector) ?? new Map();
4360
for (const line of body.split("\n")) {
4461
const declaration = line.match(/^\s*(--[\w-]+)\s*:\s*([^;]+);/);
4562
const name = declaration?.[1];
@@ -92,6 +109,17 @@ function contrastRatio(foregroundHex: string, backgroundHex: string): number {
92109
return (lighter + 0.05) / (darker + 0.05);
93110
}
94111

112+
/** Read the surface/border tokens the shipped code-chip rule actually paints. */
113+
function readCodeChipTokens(chatTextCss: string): { surface: string; border: string } {
114+
const rule = chatTextCss.split(CODE_CHIP_RULE)[1]?.split("}")[0] ?? "";
115+
const surface = rule.match(/background:\s*var\((--[\w-]+)\)/u)?.[1];
116+
const border = rule.match(/border:[^;]*var\((--[\w-]+)\)/u)?.[1];
117+
if (!surface || !border) {
118+
throw new Error(`could not read chip tokens from "${CODE_CHIP_RULE}"`);
119+
}
120+
return { surface, border };
121+
}
122+
95123
describe("Control UI theme contrast", () => {
96124
const baseCss = fs.readFileSync(path.join(stylesDir, "base.css"), "utf8");
97125
const themes = resolveThemes(parseThemeBlocks(baseCss));
@@ -120,4 +148,35 @@ describe("Control UI theme contrast", () => {
120148
}
121149
expect(failures).toEqual([]);
122150
});
151+
152+
it("keeps the markdown code chip separated from every surface it sits on", () => {
153+
const chatTextCss = fs.readFileSync(path.join(stylesDir, "chat", "text.css"), "utf8");
154+
const chip = readCodeChipTokens(chatTextCss);
155+
const failures: string[] = [];
156+
for (const [themeName, tokens] of themes) {
157+
const surface = tokens.get(chip.surface);
158+
const border = tokens.get(chip.border);
159+
expect(surface, `${themeName}: ${chip.surface} is not a hex token`).toMatch(/^#/u);
160+
expect(border, `${themeName}: ${chip.border} is not a hex token`).toMatch(/^#/u);
161+
for (const hostToken of CODE_CHIP_HOST_SURFACES) {
162+
const host = tokens.get(hostToken);
163+
if (!host?.startsWith("#")) {
164+
continue;
165+
}
166+
const surfaceStep = contrastRatio(surface ?? "", host);
167+
const borderStep = contrastRatio(border ?? "", host);
168+
if (surfaceStep < CHIP_SURFACE_MIN_STEP) {
169+
failures.push(
170+
`${themeName}: chip ${chip.surface} ${surface} on ${hostToken} ${host} = ${surfaceStep.toFixed(2)}:1 (< ${CHIP_SURFACE_MIN_STEP}:1)`,
171+
);
172+
}
173+
if (borderStep < CHIP_BORDER_MIN_STEP) {
174+
failures.push(
175+
`${themeName}: chip border ${chip.border} ${border} on ${hostToken} ${host} = ${borderStep.toFixed(2)}:1 (< ${CHIP_BORDER_MIN_STEP}:1)`,
176+
);
177+
}
178+
}
179+
}
180+
expect(failures).toEqual([]);
181+
});
123182
});

‎ui/src/styles/chat/text.css‎

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -194,18 +194,21 @@
194194
margin-top: 0;
195195
}
196196

197+
/* Code surfaces must lift off the host bubble in every theme. --secondary equals
198+
--card on every dark palette, so chips painted with it vanished inside user
199+
bubbles; --bg-muted/--border-strong separate in both modes without an override. */
197200
.chat-text :where(:not(pre) > code) {
198-
background: var(--secondary);
201+
background: var(--bg-muted);
199202
padding: 0.15em 0.35em;
200203
border-radius: var(--radius-sm);
201-
border: 1px solid var(--border);
204+
border: 1px solid var(--border-strong);
202205
overflow-wrap: anywhere;
203206
word-break: break-word;
204207
}
205208

206209
.chat-text :where(pre) {
207-
background: var(--secondary);
208-
border: 1px solid var(--border);
210+
background: var(--bg-muted);
211+
border: 1px solid var(--border-strong);
209212
border-radius: var(--radius-md);
210213
margin-top: 0.75em;
211214
padding: 10px 12px;
@@ -270,16 +273,6 @@
270273
background: rgba(0, 0, 0, 0.04);
271274
}
272275

273-
:root[data-theme-mode="light"] .chat-text :where(:not(pre) > code) {
274-
background: var(--bg-muted);
275-
border: 1px solid rgba(0, 0, 0, 0.1);
276-
}
277-
278-
:root[data-theme-mode="light"] .chat-text :where(pre) {
279-
background: var(--bg-muted);
280-
border: 1px solid rgba(0, 0, 0, 0.1);
281-
}
282-
283276
.chat-text :where(hr) {
284277
border: none;
285278
border-top: 1px solid var(--border);

0 commit comments

Comments
 (0)