The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
fix(web): theme hex field keeps your text while you delete it (#29550)
Co-authored-by: Julius Marminge <julius0216@outlook.com>
Erik committed
Nov 20, 2026 at 16:31 UTC
21d7feaff7edb445855c91e0d4526b9cce63d83a
3 files changed
+79
-15
apps/web/src/components/settings/ThemeColorPicker.tsx
+24
-4
@@ -65,6 +65,10 @@ function themeRgbToHex(value: string): string | null {
65
return `#${channels.map((channel) => channel.toString(16).padStart(2, "0")).join("")}`;
66
}
67
68
+function isShortHexColor(value: string): boolean {
69
+ return /^#[0-9a-f]{3,4}$/i.test(value.trim());
70
+}
71
+
72
function themeRgbValue(hex: string) {
73
return themeHexToRgb(hex).join(", ");
74
}
@@ -349,6 +353,10 @@ export const ThemeColorField = memo(function ThemeColorField({
353
const editorValue = value.trim().toLowerCase().startsWith("oklch(")
354
? (themeColorToHex(value) ?? value)
355
: value;
356
+ // While focused the input shows the typed text, not the committed value: a guided palette
357
+ // rewrites the committed color, and echoing that mid-edit would replace the user's keystrokes.
358
+ const [hexDraft, setHexDraft] = useState<string | null>(null);
359
+ const hexText = hexDraft ?? editorValue;
360
361
return (
362
<div
@@ -390,17 +398,29 @@ export const ThemeColorField = memo(function ThemeColorField({
398
value={swatchValue}
399
/>
400
<Input
393
- aria-invalid={!isColorValue}
401
+ aria-invalid={!isThemeColor(hexText)}
402
aria-label={gt("{label} hex value", { label })}
403
className="w-28 shrink-0"
404
font="mono"
405
id={`${role}-hex`}
406
nativeInput
399
- onChange={(event) => onChange(role, event.currentTarget.value)}
400
- onFocus={() => onSelect?.(role)}
407
+ onBlur={() => {
408
+ if (hexDraft !== null && isShortHexColor(hexDraft)) onChange(role, hexDraft);
409
+ setHexDraft(null);
410
+ }}
411
+ onChange={(event) => {
412
+ const nextValue = event.currentTarget.value;
413
+ setHexDraft(nextValue);
414
+ // Shorthand is usually a longer hex mid-deletion, so commit it on blur instead.
415
+ if (!isShortHexColor(nextValue)) onChange(role, nextValue);
416
+ }}
417
+ onFocus={() => {
418
+ setHexDraft(editorValue);
419
+ onSelect?.(role);
420
+ }}
421
onPointerDown={() => onSelect?.(role)}
422
size="compact"
403
- value={editorValue}
423
+ value={hexText}
424
/>
425
</div>
426
</div>
apps/web/src/components/settings/ThemeEditorPanel.browser.test.tsx
+52
-11
@@ -1,6 +1,8 @@
1
import { describe, expect, it, vi } from "vite-plus/test";
2
import { page, userEvent } from "vite-plus/test/browser";
3
4
+import type { ReactNode } from "react";
5
+
6
import { act, render } from "../../../test/render";
7
import { Dialog, DialogPopup, DialogTitle } from "../ui/dialog";
8
import { ThemeEditorPanel } from "./ThemeEditorPanel";
@@ -46,9 +48,9 @@ function pointerAt(target: HTMLElement, type: string, xFraction: number) {
48
);
49
}
50
49
-async function renderPanel() {
51
+async function renderPanel(appContent?: ReactNode) {
52
await page.viewport(1280, 900);
51
- const stack = new AsyncDisposableStack();
53
+ await using stack = new AsyncDisposableStack();
54
// Synthetic pointer ids are not active pointers, so the browser would reject capture.
55
stack.use(vi.spyOn(HTMLElement.prototype, "setPointerCapture").mockImplementation(() => {}));
56
// Tailwind is not loaded here; give the panel its fixed, card-sized box.
@@ -57,21 +59,25 @@ async function renderPanel() {
59
stack.defer(() => style.remove());
60
const screen = stack.use(
61
await render(
60
- <ThemeEditorPanel
61
- open
62
- onOpenChange={() => {}}
63
- onSaved={() => true}
64
- editingTheme={null}
65
- initialAppearance="dark"
66
- restoreTheme={() => {}}
67
- />,
62
+ <>
63
+ {appContent}
64
+ <ThemeEditorPanel
65
+ open
66
+ onOpenChange={() => {}}
67
+ onSaved={() => true}
68
+ editingTheme={null}
69
+ initialAppearance="dark"
70
+ restoreTheme={() => {}}
71
+ />
72
+ </>,
73
),
74
);
75
const panel = screen.getByRole("dialog", { name: "Create theme" });
76
await expect.element(panel).toBeVisible();
77
+ const owned = stack.move();
78
return {
79
panel: panel.element() as HTMLElement,
74
- [Symbol.asyncDispose]: () => stack.disposeAsync(),
80
+ [Symbol.asyncDispose]: () => owned.disposeAsync(),
81
};
82
}
83
@@ -191,6 +197,41 @@ describe("ThemeEditorPanel", () => {
197
expect((hex.element() as HTMLInputElement).value).toBe(original);
198
});
199
200
+ it("keeps the typed text while backspacing the background hex past valid shorthand", async () => {
201
+ await using _view = await renderPanel();
202
+ const hex = page.getByRole("textbox", { name: "Background hex value" });
203
+ await userEvent.fill(hex, "#222222");
204
+ await expect.element(hex).toHaveValue("#222222");
205
+
206
+ // Four- and three-digit prefixes are valid shorthand; the guided palette must not rewrite them.
207
+ for (const expected of ["#22222", "#2222", "#222", "#22", "#2", "#", ""]) {
208
+ await userEvent.keyboard("{Backspace}");
209
+ await expect.element(hex).toHaveValue(expected);
210
+ }
211
+
212
+ await userEvent.keyboard("#3366cc");
213
+ await expect.element(hex).toHaveValue("#3366cc");
214
+ await act(async () => (hex.element() as HTMLInputElement).blur());
215
+ await expect.element(hex).toHaveValue("#3366cc");
216
+ });
217
+
218
+ it("commits a typed shorthand hex when inspecting an advanced color switches modes", async () => {
219
+ await using _view = await renderPanel(<div className="bg-sidebar" data-testid="app-sidebar" />);
220
+ await userEvent.click(page.getByRole("button", { name: "Inspect app colors" }));
221
+ await userEvent.fill(page.getByRole("textbox", { name: "Background hex value" }), "#abc");
222
+
223
+ // Inspect mode cancels the pointerdown, so the hex field keeps focus while it unmounts.
224
+ const sidebar = page.getByTestId("app-sidebar").element() as HTMLElement;
225
+ await act(async () => pointerAt(sidebar, "pointerdown", 0.5));
226
+
227
+ await expect
228
+ .element(page.getByRole("switch", { name: "Use advanced theme colors" }))
229
+ .toBeChecked();
230
+ await expect
231
+ .element(page.getByRole("textbox", { name: "Background hex value" }))
232
+ .toHaveValue("#aabbcc");
233
+ });
234
+
235
it("closes on Escape without closing Settings", async () => {
236
await using view = await renderBesideSettings();
237
(await editorElement("input")).focus();
apps/web/src/components/settings/ThemeEditorPanel.tsx
+3
@@ -694,6 +694,9 @@ export function ThemeEditorPanel({
694
: inspectThemeRoleAtElement(target);
695
if (!inspection) return;
696
clearHover();
697
+ // preventDefault keeps focus put; blur so a focused color field commits its draft before
698
+ // selectThemeRole can switch modes and unmount it.
699
+ if (document.activeElement instanceof HTMLElement) document.activeElement.blur();
700
selectThemeRole(inspection.role, true);
701
shouldDisarmAfterClick = true;
702
};