Merge branch 'fix/studio-sfx-u9' into fix/studio-ui-audit-fixes

This commit is contained in:
Miguel Angel Simon Sierra
2026-07-15 19:32:22 -04:00
5 changed files with 194 additions and 4 deletions
@@ -118,6 +118,7 @@ export function PropertyRow({
{PROP_LABELS[prop] ?? prop}
</span>
<input
key={String(val)}
type="text"
defaultValue={String(val)}
className="flex-1 bg-transparent text-[11px] text-neutral-200 outline-none"
@@ -0,0 +1,154 @@
// @vitest-environment happy-dom
import React, { act, useState } from "react";
import { createRoot, type Root } from "react-dom/client";
import { afterEach, describe, expect, it, vi } from "vitest";
import { PropertyRow } from "./AnimationCardParts";
import { ColorField } from "./propertyPanelColor";
import { GradientField } from "./propertyPanelFill";
import { DetailField } from "./propertyPanelPrimitives";
Object.defineProperty(globalThis, "IS_REACT_ACT_ENVIRONMENT", {
configurable: true,
value: true,
});
const roots: Root[] = [];
afterEach(() => {
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(<DetailField label="Width" value={value} onCommit={onCommit} />);
const input = host.querySelector<HTMLInputElement>("input");
if (!input) throw new Error("CommitField input was not rendered");
return {
input,
rerender(nextValue: string) {
act(() => root.render(<DetailField label="Width" value={nextValue} onCommit={onCommit} />));
},
};
}
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 (
<PropertyRow
prop={prop}
val={value}
onCommit={setValue}
onRemove={() => undefined}
removeTitle="Remove property"
/>
);
}
const { host } = render(<Harness />);
const preset = Array.from(host.querySelectorAll("button")).find(
(button) => button.textContent === presetLabel,
);
const input = host.querySelector<HTMLInputElement>('input[type="text"]');
if (!preset || !input) throw new Error("String property controls were not rendered");
act(() => preset.click());
expect(host.querySelector<HTMLInputElement>('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(
<GradientField
value="linear-gradient(90deg, #000000 0%, #ffffff 100%)"
fallbackColor={undefined}
onCommit={onCommit}
/>,
);
const track = host.querySelector<HTMLDivElement>('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(<ColorField label="Fill" value="#123456" onCommit={onCommit} />);
const openButton = document.querySelector<HTMLButtonElement>(
'button[aria-label="Pick fill color"]',
);
if (!openButton) throw new Error("Color picker button was not rendered");
act(() => openButton.click());
const input = document.querySelector<HTMLInputElement>('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();
});
});
@@ -331,6 +331,7 @@ export function ColorField({
<input
value={hexDraft}
onChange={(event) => handleHexCommit(event.target.value)}
onBlur={() => setHexDraft(toHexColor(draftColor).toUpperCase())}
className={`${FIELD} h-10 w-full text-[11px] font-medium outline-none`}
spellCheck={false}
/>
@@ -206,6 +206,7 @@ export function GradientField({
onCommit: (nextValue: string) => void;
}) {
const previewRef = useRef<HTMLDivElement | null>(null);
const [selectedStopIndex, setSelectedStopIndex] = useState<number | null>(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) => (
<div
<button
key={`stop-preview-${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)]"
type="button"
disabled={disabled}
aria-label={`Select stop ${index + 1}`}
aria-pressed={selectedStopIndex === index}
onClick={(event) => {
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,
@@ -17,11 +17,14 @@ function CommitField({
const valueRef = useRef(value);
const draftRef = useRef(draft);
const inputRef = useRef<HTMLInputElement>(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);
}}