From c427ab965683076bbbdea0b98389f0eaad5b89d1 Mon Sep 17 00:00:00 2001 From: Miguel Angel Simon Sierra Date: Wed, 15 Jul 2026 18:43:34 -0400 Subject: [PATCH] fix(studio): correct inspector input commit and edit behaviors --- .../components/editor/AnimationCardParts.tsx | 1 + .../editor/InspectorInputCorrectness.test.tsx | 154 ++++++++++++++++++ .../components/editor/propertyPanelColor.tsx | 1 + .../components/editor/propertyPanelFill.tsx | 25 ++- .../editor/propertyPanelPrimitives.tsx | 17 +- 5 files changed, 194 insertions(+), 4 deletions(-) create mode 100644 packages/studio/src/components/editor/InspectorInputCorrectness.test.tsx diff --git a/packages/studio/src/components/editor/AnimationCardParts.tsx b/packages/studio/src/components/editor/AnimationCardParts.tsx index 8c33f68e80..a150d92c11 100644 --- a/packages/studio/src/components/editor/AnimationCardParts.tsx +++ b/packages/studio/src/components/editor/AnimationCardParts.tsx @@ -118,6 +118,7 @@ export function PropertyRow({ {PROP_LABELS[prop] ?? prop} { + for (const root of roots.splice(0)) act(() => root.unmount()); + document.body.innerHTML = ""; +}); + +function render(element: React.ReactNode) { + const host = document.createElement("div"); + document.body.append(host); + const root = createRoot(host); + roots.push(root); + act(() => root.render(element)); + return { host, root }; +} + +function setInputValue(input: HTMLInputElement, value: string) { + const setter = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, "value")?.set; + if (!setter) throw new Error("HTMLInputElement value setter is unavailable"); + setter.call(input, value); + input.dispatchEvent(new Event("input", { bubbles: true })); +} + +function renderCommitField(value: string, onCommit: (nextValue: string) => void) { + const { host, root } = render(); + const input = host.querySelector("input"); + if (!input) throw new Error("CommitField input was not rendered"); + return { + input, + rerender(nextValue: string) { + act(() => root.render()); + }, + }; +} + +describe("inspector input correctness", () => { + it("preserves a focused CommitField draft across external value refreshes", () => { + const onCommit = vi.fn(); + const { input, rerender } = renderCommitField("100px", onCommit); + + act(() => input.focus()); + act(() => setInputValue(input, "125px")); + rerender("110px"); + + expect(input.value).toBe("125px"); + act(() => input.blur()); + expect(onCommit).toHaveBeenCalledWith("125px"); + }); + + it("does not commit a stale CommitField value when focus was clean", () => { + const onCommit = vi.fn(); + const { input, rerender } = renderCommitField("100px", onCommit); + + act(() => input.focus()); + rerender("110px"); + + expect(input.value).toBe("110px"); + act(() => input.blur()); + expect(onCommit).not.toHaveBeenCalled(); + }); + + it.each([ + ["filter", "Blur", "blur(4px)"], + ["clipPath", "Circle", "circle(50% at 50% 50%)"], + ])("shows a clicked %s preset in its input", (prop, presetLabel, presetValue) => { + function Harness() { + const [value, setValue] = useState("none"); + return ( + undefined} + removeTitle="Remove property" + /> + ); + } + + const { host } = render(); + const preset = Array.from(host.querySelectorAll("button")).find( + (button) => button.textContent === presetLabel, + ); + const input = host.querySelector('input[type="text"]'); + if (!preset || !input) throw new Error("String property controls were not rendered"); + + act(() => preset.click()); + + expect(host.querySelector('input[type="text"]')?.value).toBe(presetValue); + }); + + it("selects an existing gradient stop and adds only from empty track space", () => { + const onCommit = vi.fn(); + const { host } = render( + , + ); + const track = host.querySelector('div[style*="background-image"]'); + const firstStop = track?.firstElementChild; + if (!track || !(firstStop instanceof HTMLElement)) { + throw new Error("Gradient preview controls were not rendered"); + } + track.getBoundingClientRect = () => new DOMRect(0, 0, 200, 44); + + act(() => track.dispatchEvent(new MouseEvent("click", { bubbles: true, clientX: 4 }))); + + expect(onCommit).not.toHaveBeenCalled(); + expect(firstStop.getAttribute("aria-pressed")).toBe("true"); + + act(() => track.dispatchEvent(new MouseEvent("click", { bubbles: true, clientX: 100 }))); + + expect(onCommit).toHaveBeenCalledTimes(1); + expect(onCommit.mock.calls[0]?.[0]).toContain("#808080 50%"); + }); + + it("rolls an invalid hex draft back to the last valid value on blur", () => { + const onCommit = vi.fn(); + render(); + const openButton = document.querySelector( + 'button[aria-label="Pick fill color"]', + ); + if (!openButton) throw new Error("Color picker button was not rendered"); + act(() => openButton.click()); + const input = document.querySelector('input[spellcheck="false"]'); + if (!input) throw new Error("Hex input was not rendered"); + + act(() => input.focus()); + act(() => setInputValue(input, "not-a-color")); + expect(input.value).toBe("not-a-color"); + expect(onCommit).not.toHaveBeenCalled(); + + act(() => input.blur()); + + expect(input.value).toBe("#123456"); + expect(onCommit).not.toHaveBeenCalled(); + }); +}); diff --git a/packages/studio/src/components/editor/propertyPanelColor.tsx b/packages/studio/src/components/editor/propertyPanelColor.tsx index 962f0e9172..31b8bb51a2 100644 --- a/packages/studio/src/components/editor/propertyPanelColor.tsx +++ b/packages/studio/src/components/editor/propertyPanelColor.tsx @@ -331,6 +331,7 @@ export function ColorField({ handleHexCommit(event.target.value)} + onBlur={() => setHexDraft(toHexColor(draftColor).toUpperCase())} className={`${FIELD} h-10 w-full text-[11px] font-medium outline-none`} spellCheck={false} /> diff --git a/packages/studio/src/components/editor/propertyPanelFill.tsx b/packages/studio/src/components/editor/propertyPanelFill.tsx index 3b1669e047..6d98471194 100644 --- a/packages/studio/src/components/editor/propertyPanelFill.tsx +++ b/packages/studio/src/components/editor/propertyPanelFill.tsx @@ -206,6 +206,7 @@ export function GradientField({ onCommit: (nextValue: string) => void; }) { const previewRef = useRef(null); + const [selectedStopIndex, setSelectedStopIndex] = useState(null); const parsed = parseGradient(value) ?? buildDefaultGradientModel(fallbackColor); const commit = (next: GradientModel) => onCommit(serializeGradient(next)); @@ -247,13 +248,31 @@ export function GradientField({ if (disabled) return; const rect = previewRef.current?.getBoundingClientRect(); if (!rect || rect.width <= 0) return; - addStop(((event.clientX - rect.left) / rect.width) * 100); + const position = ((event.clientX - rect.left) / rect.width) * 100; + const hitStopIndex = parsed.stops.findIndex( + (stop) => (Math.abs(stop.position - position) / 100) * rect.width <= 8, + ); + if (hitStopIndex >= 0) { + setSelectedStopIndex(hitStopIndex); + return; + } + addStop(position); }} > {parsed.stops.map((stop, index) => ( -
{ + event.stopPropagation(); + setSelectedStopIndex(index); + }} + className={`absolute top-1/2 h-4 w-4 -translate-y-1/2 rounded-full border-2 border-white/90 shadow-[0_0_0_1px_rgba(0,0,0,0.35)] ${ + selectedStopIndex === index ? "ring-2 ring-panel-accent" : "" + }`} style={{ left: `calc(${stop.position}% - 8px)`, backgroundColor: stop.color, diff --git a/packages/studio/src/components/editor/propertyPanelPrimitives.tsx b/packages/studio/src/components/editor/propertyPanelPrimitives.tsx index f2916c4668..a0e01672ca 100644 --- a/packages/studio/src/components/editor/propertyPanelPrimitives.tsx +++ b/packages/studio/src/components/editor/propertyPanelPrimitives.tsx @@ -17,11 +17,14 @@ function CommitField({ const valueRef = useRef(value); const draftRef = useRef(draft); const inputRef = useRef(null); + const focusedRef = useRef(false); + const dirtyRef = useRef(false); valueRef.current = value; draftRef.current = draft; useEffect(() => { + if (focusedRef.current && dirtyRef.current) return; setDraft(value); }, [value]); @@ -36,6 +39,7 @@ function CommitField({ if (!nextDraft) return; e.preventDefault(); e.stopPropagation(); + dirtyRef.current = true; setDraft(nextDraft); scheduleCommitRef.current(nextDraft); }; @@ -70,11 +74,21 @@ function CommitField({ type="text" value={draft} disabled={disabled} + onFocus={() => { + focusedRef.current = true; + }} onChange={(e) => { + dirtyRef.current = true; setDraft(e.target.value); if (liveCommit) scheduleCommit(e.target.value); }} - onBlur={() => commitDraft(draft)} + onBlur={() => { + const wasDirty = dirtyRef.current; + focusedRef.current = false; + dirtyRef.current = false; + if (wasDirty) commitDraft(draft); + else setDraft(valueRef.current); + }} onKeyDown={(e) => { if (e.key === "Enter") { (e.target as HTMLInputElement).blur(); @@ -84,6 +98,7 @@ function CommitField({ const nextDraft = adjustNumericToken(draft, e.key === "ArrowUp" ? 1 : -1, e); if (!nextDraft) return; e.preventDefault(); + dirtyRef.current = true; setDraft(nextDraft); scheduleCommit(nextDraft); }}