Skip to content

Commit 00b2d0e

Browse files
dw3105chief-archivistclaudemaria-rcks
authored
fix(web): the prompt box follows the interface font size (#26559)
Co-authored-by: chief-archivist <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: maria-rcks <[email protected]>
1 parent 7d7db0c commit 00b2d0e

4 files changed

Lines changed: 107 additions & 24 deletions

File tree

‎apps/web/src/appInit.tsx‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import { scopedThreadKey, scopeThreadRef } from "@t3tools/client-runtime/environment";
22
import type { ClientSettings } from "@t3tools/contracts/settings";
33
import { createBrowserHistory, createHashHistory } from "@tanstack/react-router";
4+
import * as Schema from "effect/Schema";
45

56
import { applyAppearanceContrast } from "./appearanceContrast";
6-
import { applyAppearanceFontVariables } from "./appearanceFonts";
7+
import { applyAppearanceFontVariables, TYPOGRAPHY_ADVANCED_STORAGE_KEY } from "./appearanceFonts";
78
import { startElectronBrowserHost } from "./browser/ElectronBrowserHost";
89
import { startBrowserProfileReporting } from "./browser/BrowserProfileReporter";
910
import { startPreviewTitleRecording } from "./browserHistoryStore";
@@ -30,6 +31,7 @@ import {
3031
} from "./composerDraftStore";
3132
import { startDefaultThemeAdoption } from "./hooks/useDefaultTheme";
3233
import { startEnvironmentThemeRepaint } from "./hooks/useEnvironmentTheme";
34+
import { readLocalStorageValue, subscribeLocalStorageKey } from "./hooks/useLocalStorage";
3335
import {
3436
getClientSettings,
3537
startModelPreferencesMigration,
@@ -58,10 +60,18 @@ import { resolveThreadRouteTarget } from "./threadRoutes";
5860
function startClientAppearance(): void {
5961
const root = document.documentElement;
6062
let applied: ClientSettings | null = null;
63+
let appliedSeparateFonts: boolean | null = null;
6164
const apply = () => {
6265
const settings = getClientSettings();
6366
const previous = applied;
6467
applied = settings;
68+
const separateFonts = readLocalStorageValue(
69+
TYPOGRAPHY_ADVANCED_STORAGE_KEY,
70+
false,
71+
Schema.Boolean,
72+
);
73+
const previousSeparateFonts = appliedSeparateFonts;
74+
appliedSeparateFonts = separateFonts;
6575
if (previous?.diffColorScheme !== settings.diffColorScheme) {
6676
root.dataset.diffColorScheme = settings.diffColorScheme;
6777
}
@@ -86,7 +96,8 @@ function startClientAppearance(): void {
8696
previous.fontSizeInterface !== settings.fontSizeInterface ||
8797
previous.fontSizePrompt !== settings.fontSizePrompt ||
8898
previous.fontSizeCode !== settings.fontSizeCode ||
89-
previous.fontSmoothing !== settings.fontSmoothing
99+
previous.fontSmoothing !== settings.fontSmoothing ||
100+
previousSeparateFonts !== separateFonts
90101
) {
91102
applyAppearanceFontVariables(root, {
92103
sans: settings.fontFamilySans,
@@ -96,10 +107,12 @@ function startClientAppearance(): void {
96107
sizePrompt: settings.fontSizePrompt,
97108
sizeCode: settings.fontSizeCode,
98109
smoothing: settings.fontSmoothing,
110+
separate: separateFonts,
99111
});
100112
}
101113
};
102114
subscribeClientSettings(apply);
115+
subscribeLocalStorageKey(TYPOGRAPHY_ADVANCED_STORAGE_KEY, apply);
103116
apply();
104117
}
105118

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import "~/index.css";
4+
import { applyAppearanceFontVariables } from "./appearanceFonts";
5+
6+
function renderedFontSize(style: (probe: HTMLElement) => void): string {
7+
const probe = document.createElement("div");
8+
style(probe);
9+
document.body.append(probe);
10+
try {
11+
return getComputedStyle(probe).fontSize;
12+
} finally {
13+
probe.remove();
14+
}
15+
}
16+
17+
const composerFontSize = () =>
18+
renderedFontSize((probe) => {
19+
// The composer editor's own size class.
20+
probe.className = "text-(length:--font-size-prompt,var(--text-sm))";
21+
});
22+
23+
const touchComposerFontSize = () =>
24+
renderedFontSize((probe) => {
25+
probe.style.fontSize = "var(--font-size-prompt-touch)";
26+
});
27+
28+
describe("applyAppearanceFontVariables", () => {
29+
it("sizes the composer from the interface size until fonts are set separately", () => {
30+
const root = document.documentElement;
31+
const style = root.getAttribute("style");
32+
const preferences = {
33+
sans: "",
34+
code: "",
35+
composer: "",
36+
sizeInterface: 20,
37+
sizePrompt: 14,
38+
sizeCode: 13,
39+
smoothing: true,
40+
};
41+
try {
42+
applyAppearanceFontVariables(root, { ...preferences, separate: false });
43+
expect(composerFontSize()).toBe("17.5px");
44+
expect(touchComposerFontSize()).toBe("17.5px");
45+
46+
applyAppearanceFontVariables(root, { ...preferences, separate: true });
47+
expect(composerFontSize()).toBe("14px");
48+
// iOS zooms into inputs under 16px.
49+
expect(touchComposerFontSize()).toBe("16px");
50+
} finally {
51+
if (style === null) root.removeAttribute("style");
52+
else root.setAttribute("style", style);
53+
}
54+
});
55+
});

‎apps/web/src/appearanceFonts.ts‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,17 +66,18 @@ export interface AppearanceFontPreferences {
6666
readonly sizePrompt: number;
6767
readonly sizeCode: number;
6868
readonly smoothing: boolean;
69+
readonly separate: boolean;
6970
}
7071

71-
/** Always writes sizes: interface size sets the root; prompt and code use pixels to avoid double scaling. */
72+
/** Interface size sets the root; prompt and code use pixels to avoid double scaling. */
7273
export function applyAppearanceFontVariables(
7374
root: HTMLElement,
7475
preferences: AppearanceFontPreferences,
7576
): void {
7677
const families: ReadonlyArray<readonly [variable: string, custom: string, fallback: string]> = [
7778
["--font-sans", preferences.sans, DEFAULT_SANS_FONT_STACK],
7879
["--font-mono", preferences.code, DEFAULT_CODE_FONT_STACK],
79-
["--font-composer", preferences.composer, "var(--font-sans)"],
80+
["--font-composer", preferences.separate ? preferences.composer : "", "var(--font-sans)"],
8081
];
8182
for (const [variable, custom, fallback] of families) {
8283
const list = cssFontFamilies(custom);
@@ -88,7 +89,10 @@ export function applyAppearanceFontVariables(
8889
}
8990

9091
root.style.fontSize = `${clampInterfaceFontSize(preferences.sizeInterface)}px`;
91-
root.style.setProperty("--font-size-prompt", `${clampPromptFontSize(preferences.sizePrompt)}px`);
92+
root.style.setProperty(
93+
"--font-size-prompt",
94+
preferences.separate ? `${clampPromptFontSize(preferences.sizePrompt)}px` : "var(--text-sm)",
95+
);
9296
const code = clampCodeFontSize(preferences.sizeCode);
9397
root.style.setProperty("--font-size-code", `${code}px`);
9498
root.style.setProperty("--diffs-font-size", `${code}px`);

‎apps/web/src/hooks/useLocalStorage.ts‎

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,35 @@ function decodeOrInitial<T, E>(
123123
}
124124
}
125125

126+
export function readLocalStorageValue<T, E>(
127+
key: string,
128+
initialValue: T,
129+
schema: Schema.Codec<T, E>,
130+
): T {
131+
return decodeOrInitial(key, schema, readSerializedValue(key), initialValue);
132+
}
133+
134+
/** Fires on writes from this tab and from other tabs. */
135+
export function subscribeLocalStorageKey(key: string, onChange: () => void): () => void {
136+
const handleStorageChange = (event: StorageEvent) => {
137+
if (event.key === key) {
138+
onChange();
139+
}
140+
};
141+
const handleLocalChange = (event: CustomEvent<LocalStorageChangeDetail>) => {
142+
if (event.detail.key === key) {
143+
onChange();
144+
}
145+
};
146+
147+
window.addEventListener("storage", handleStorageChange);
148+
window.addEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
149+
return () => {
150+
window.removeEventListener("storage", handleStorageChange);
151+
window.removeEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
152+
};
153+
}
154+
126155
export function writeLocalStorageValue<T, E>(
127156
key: string,
128157
value: T | ((val: T) => T),
@@ -169,25 +198,7 @@ export function useLocalStorage<T, E>(
169198
): [T, (value: T | ((val: T) => T)) => void] {
170199
const getSnapshot = () => readSerializedValue(key);
171200

172-
const subscribe = (onStoreChange: () => void) => {
173-
const handleStorageChange = (event: StorageEvent) => {
174-
if (event.key === key) {
175-
onStoreChange();
176-
}
177-
};
178-
const handleLocalChange = (event: CustomEvent<LocalStorageChangeDetail>) => {
179-
if (event.detail.key === key) {
180-
onStoreChange();
181-
}
182-
};
183-
184-
window.addEventListener("storage", handleStorageChange);
185-
window.addEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
186-
return () => {
187-
window.removeEventListener("storage", handleStorageChange);
188-
window.removeEventListener(LOCAL_STORAGE_CHANGE_EVENT, handleLocalChange as EventListener);
189-
};
190-
};
201+
const subscribe = (onStoreChange: () => void) => subscribeLocalStorageKey(key, onStoreChange);
191202

192203
const serializedValue = useSyncExternalStore(subscribe, getSnapshot, getSnapshot);
193204
const storedValue = decodeOrInitial(key, schema, serializedValue, initialValue);

0 commit comments

Comments
 (0)