Skip to content

Commit 781f6db

Browse files
drdatorclaude
andcommitted
fix(web): Esc in the theme color picker restores the previous color
Esc closed a theme color picker but kept the edited color. The theme editor now snapshots its colors when a picker opens and restores them when the picker is dismissed with Esc. The picker drops commits that arrive after an Esc close, so a pending frame or a drag still moving through the exit animation cannot overwrite the restored colors. Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 5ef5685 commit 781f6db

3 files changed

Lines changed: 90 additions & 6 deletions

File tree

‎apps/web/src/components/settings/ThemeColorPicker.tsx‎

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { memo, useEffect, useState } from "react";
1+
import { memo, useEffect, useRef, useState } from "react";
22
import { isThemeColor, themeColorToHex, type ThemeColorRole } from "../../themePalette";
33
import { cn } from "../../lib/utils";
44
import { hexToHsv, hsvToHex, type HsvColor } from "../../lib/color";
@@ -240,21 +240,31 @@ function ThemeColorPicker({
240240
label,
241241
value,
242242
onChange,
243+
onEditStart,
244+
onEditCancel,
243245
onInteract,
244246
}: {
245247
label: string;
246248
value: string;
247249
onChange: (value: string) => void;
250+
onEditStart?: () => void;
251+
onEditCancel?: () => void;
248252
onInteract?: () => void;
249253
}) {
250254
const [committer] = useState(createFrameCommitter);
255+
// Escape reverts the edit, so drop the pending frame and any drag still firing through the exit
256+
// animation instead of committing them over the restored colors.
257+
const cancelledRef = useRef(false);
251258
// Unmounting with the popover open (the editor closes mid-drag) never fires onOpenChange.
252259
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- flushes the frame committer on unmount, since an open popover never fires onOpenChange
253260
useEffect(() => committer.flush, [committer]);
254261
return (
255262
<Popover
256-
onOpenChange={(open) => {
257-
if (!open) committer.flush();
263+
onOpenChange={(open, eventDetails) => {
264+
cancelledRef.current = !open && eventDetails.reason === "escape-key";
265+
if (open) onEditStart?.();
266+
else if (cancelledRef.current) onEditCancel?.();
267+
else committer.flush();
258268
}}
259269
>
260270
<Tooltip>
@@ -290,7 +300,9 @@ function ThemeColorPicker({
290300
<ThemeColorPickerPanel
291301
committer={committer}
292302
label={label}
293-
onChange={onChange}
303+
onChange={(nextValue) => {
304+
if (!cancelledRef.current) onChange(nextValue);
305+
}}
294306
value={value}
295307
/>
296308
</PopoverPopup>
@@ -302,6 +314,8 @@ export const ThemeColorField = memo(function ThemeColorField({
302314
role,
303315
value,
304316
onChange,
317+
onEditStart,
318+
onEditCancel,
305319
onSelect,
306320
onToggleSelected,
307321
selected = false,
@@ -310,6 +324,8 @@ export const ThemeColorField = memo(function ThemeColorField({
310324
role: ThemeColorRole;
311325
value: string;
312326
onChange: (role: ThemeColorRole, value: string) => void;
327+
onEditStart?: (role: ThemeColorRole) => void;
328+
onEditCancel?: (role: ThemeColorRole) => void;
313329
onSelect?: (role: ThemeColorRole) => void;
314330
onToggleSelected?: (role: ThemeColorRole) => void;
315331
selected?: boolean;
@@ -350,6 +366,8 @@ export const ThemeColorField = memo(function ThemeColorField({
350366
<ThemeColorPicker
351367
label={label}
352368
onChange={(nextValue) => onChange(role, nextValue)}
369+
onEditCancel={() => onEditCancel?.(role)}
370+
onEditStart={() => onEditStart?.(role)}
353371
onInteract={() => onSelect?.(role)}
354372
value={swatchValue}
355373
/>

‎apps/web/src/components/settings/ThemeEditorPanel.browser.test.tsx‎

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { describe, expect, it, vi } from "vite-plus/test";
2-
import { page } from "vite-plus/test/browser";
2+
import { page, userEvent } from "vite-plus/test/browser";
33

4-
import { render } from "../../../test/render";
4+
import { act, render } from "../../../test/render";
55
import { ThemeEditorPanel } from "./ThemeEditorPanel";
66

77
function dragHeader(panel: HTMLElement, toX: number, toY: number) {
@@ -32,6 +32,19 @@ function dragResizeHandle(panel: HTMLElement, byX: number) {
3232
handle.dispatchEvent(new PointerEvent("pointerup", init));
3333
}
3434

35+
function pointerAt(target: HTMLElement, type: string, xFraction: number) {
36+
const rect = target.getBoundingClientRect();
37+
target.dispatchEvent(
38+
new PointerEvent(type, {
39+
bubbles: true,
40+
button: 0,
41+
pointerId: 3,
42+
clientX: rect.left + rect.width * xFraction,
43+
clientY: rect.top + rect.height / 2,
44+
}),
45+
);
46+
}
47+
3548
async function renderPanel() {
3649
await page.viewport(1280, 900);
3750
const stack = new AsyncDisposableStack();
@@ -110,4 +123,29 @@ describe("ThemeEditorPanel", () => {
110123
await page.viewport(1280, 900);
111124
await expect.poll(() => (panel.element() as HTMLElement).style.left).toBe("700px");
112125
});
126+
127+
it("restores the colors from before the picker opened when Escape closes it", async () => {
128+
await using _view = await renderPanel();
129+
const hex = page.getByRole("textbox", { name: "Accent hex value" });
130+
const original = (hex.element() as HTMLInputElement).value;
131+
await userEvent.click(page.getByRole("button", { name: "Choose Accent color" }));
132+
const hue = page.getByRole("slider", { name: "Accent hue" });
133+
await expect.element(hue).toBeInTheDocument();
134+
const hueSlider = hue.element() as HTMLElement;
135+
136+
await act(async () => {
137+
pointerAt(hueSlider, "pointerdown", 0.5);
138+
pointerAt(hueSlider, "pointerup", 0.5);
139+
});
140+
await expect.element(hex).not.toHaveValue(original);
141+
142+
await act(async () => {
143+
pointerAt(hueSlider, "pointerdown", 0.25);
144+
hueSlider.dispatchEvent(new KeyboardEvent("keydown", { bubbles: true, key: "Escape" }));
145+
// The drag keeps going while the picker animates out.
146+
pointerAt(hueSlider, "pointermove", 0.75);
147+
pointerAt(hueSlider, "pointerup", 0.75);
148+
});
149+
expect((hex.element() as HTMLInputElement).value).toBe(original);
150+
});
113151
});

‎apps/web/src/components/settings/ThemeEditorPanel.tsx‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,11 @@ export function ThemeEditorPanel({
354354
const [position, setPosition] = useState<{ x: number; y: number; width: number } | null>(null);
355355
const [size, setSize] = useState<{ width: number; height: number } | null>(null);
356356
const panelRef = useRef<HTMLDivElement>(null);
357+
const colorEditSnapshotRef = useRef<{
358+
colorsByAppearance: ThemeEditorColorsByAppearance;
359+
simpleColorsDirtyByAppearance: Record<ThemeAppearance, boolean>;
360+
shouldRegenerateGuidedColors: boolean;
361+
} | null>(null);
357362
const dragOffsetRef = useRef<{ dx: number; dy: number; width: number } | null>(null);
358363
const resizeStartRef = useRef<{
359364
pointerX: number;
@@ -472,6 +477,25 @@ export function ThemeEditorPanel({
472477
if (isAdvanced) setShouldRegenerateGuidedColors(true);
473478
};
474479

480+
// A color edit also rewrites derived roles, so cancelling restores the whole snapshot rather than
481+
// replaying the previous value through updateColor.
482+
const startColorEdit = () => {
483+
colorEditSnapshotRef.current = {
484+
colorsByAppearance,
485+
simpleColorsDirtyByAppearance,
486+
shouldRegenerateGuidedColors,
487+
};
488+
};
489+
490+
const cancelColorEdit = () => {
491+
const snapshot = colorEditSnapshotRef.current;
492+
colorEditSnapshotRef.current = null;
493+
if (!snapshot) return;
494+
setColorsByAppearance(snapshot.colorsByAppearance);
495+
setSimpleColorsDirtyByAppearance(snapshot.simpleColorsDirtyByAppearance);
496+
setShouldRegenerateGuidedColors(snapshot.shouldRegenerateGuidedColors);
497+
};
498+
475499
const selectThemeRole = (role: ThemeColorRole, reveal = false) => {
476500
const visibleRole = getThemeEditorColorFamily(role)?.role ?? role;
477501
setSelectedRole(visibleRole);
@@ -947,6 +971,8 @@ export function ThemeEditorPanel({
947971
key={family.id}
948972
label={family.label}
949973
onChange={updateColor}
974+
onEditCancel={cancelColorEdit}
975+
onEditStart={startColorEdit}
950976
onSelect={selectThemeRole}
951977
onToggleSelected={toggleThemeRole}
952978
role={family.role}
@@ -986,6 +1012,8 @@ export function ThemeEditorPanel({
9861012
<ThemeColorField
9871013
key={role}
9881014
onChange={updateColor}
1015+
onEditCancel={cancelColorEdit}
1016+
onEditStart={startColorEdit}
9891017
onSelect={selectThemeRole}
9901018
onToggleSelected={toggleThemeRole}
9911019
role={role}

0 commit comments

Comments
 (0)