Skip to content

Commit d6a40c3

Browse files
committed
fix(studio): version-scope Grade persist, flush pending edits via effect cleanup not render
Fixes three of the adversarial findings from the third heygen-com#2416 tip re-review: - Grade rollback was identity-scoped but not attempt-scoped: two edits on the SAME element (e.g. drag Exposure, then Contrast, before Exposure's persist settles) could have the earlier edit's late completion stamp confirmedGradingRef with its now-superseded value, or revert `grading` out from under the newer optimistic edit. Added a monotonic per-commit version via the existing bumpDomEditCommitVersion primitive (the same one the DOM-attribute commit runner uses for the identical race) — persistColorGradingValue now checks both identity AND "is this still the latest attempt for this element" before applying any effect. - The render-phase identity-reset block consumed shared mutable state (clearing the pending-persist timer, reading and nulling pendingPersistValueRef) directly during render. Adjusting STATE during render this way is React's documented pattern and safe to repeat, but consuming a ref this way is not: if React discarded/interrupted that specific render before it committed, the timer would already be cancelled and the pending value already nulled, with no corresponding effect ever running to compensate, silently losing the edit. Replaced with the idiomatic pattern for "clean up a per-identity resource when it changes" — a useEffect keyed on identityKey whose CLEANUP performs the cancellation/flush. A cleanup only ever runs for the effect instance that actually committed, closing the gap entirely. The render-phase block now only performs pure, idempotent state resets. - FlatSelectRow's Preset row passes label="" (the visible "Preset" text is a sibling span, to avoid rendering it twice) which left the underlying <select> with no accessible name at all. Added a dedicated `ariaLabel` prop, distinct from the visible `label`, so a caller can supply a name without a duplicate visible label. Also hardened FlatSlider's lostpointercapture handling: it now resyncs the draft directly from a latestValueRef immediately, instead of only clearing the dragging flag and waiting for the separate [value]-keyed effect to notice — closing a narrow ordering gap where a value change arriving while still dragging, followed by capture loss with no further render, could otherwise leave the knob stuck. propertyPanelFlatPrimitives.tsx crossed the 600-line file-size gate after these changes; extracted FlatToggle (and its tests) into their own files, matching the FlatMaskInsetRows precedent from an earlier commit in this stack. New/updated regression tests: same-element version race, Preset select's aria-label. Full studio suite still at the known pre-existing 55-failure baseline, zero regressions.
1 parent 34979a0 commit d6a40c3

9 files changed

Lines changed: 318 additions & 146 deletions

‎packages/studio/src/components/editor/propertyPanelFlatColorGradingSection.test.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,10 @@ describe("FlatColorGradingSection — Preset + LUT", () => {
238238
);
239239
if (!presetSelect) throw new Error("expected a preset select");
240240
expect(presetSelect.value).toBe("neutral");
241+
// The visible "Preset" label is a sibling span outside FlatSelectRow
242+
// (label="" there, to avoid rendering it twice) — the select still
243+
// needs its own accessible name via the dedicated ariaLabel prop.
244+
expect(presetSelect.getAttribute("aria-label")).toBe("Preset");
241245
act(() => {
242246
presetSelect.value = "fresh-pop";
243247
presetSelect.dispatchEvent(new Event("change", { bubbles: true }));

‎packages/studio/src/components/editor/propertyPanelFlatColorGradingSection.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,7 @@ export function FlatColorGradingSection({
312312
<span className="text-[11px] text-panel-text-2">Preset</span>
313313
<FlatSelectRow
314314
label=""
315+
ariaLabel="Preset"
315316
value={grading.preset ?? "neutral"}
316317
options={PRESET_OPTIONS}
317318
tier={resolveValueTier(

‎packages/studio/src/components/editor/propertyPanelFlatMediaSection.tsx‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import {
99
parseNumericValue,
1010
stripQueryAndHash,
1111
} from "./propertyPanelHelpers";
12-
import { FlatSelectRow, FlatSlider, FlatToggle } from "./propertyPanelFlatPrimitives";
12+
import { FlatSelectRow, FlatSlider } from "./propertyPanelFlatPrimitives";
13+
import { FlatToggle } from "./propertyPanelFlatToggle";
1314

1415
// fallow-ignore-next-line complexity
1516
export function FlatMediaSection({

‎packages/studio/src/components/editor/propertyPanelFlatPrimitives.test.tsx‎

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import {
99
FlatSegmentedRow,
1010
FlatSelectRow,
1111
FlatSlider,
12-
FlatToggle,
1312
} from "./propertyPanelFlatPrimitives";
1413

1514
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
@@ -822,6 +821,55 @@ describe("FlatSlider — Grade extensions", () => {
822821
expect(track.getAttribute("aria-valuenow")).toBe("99");
823822
act(() => root.unmount());
824823
});
824+
825+
it("resyncs immediately from the latest value on lostpointercapture, even when the value changed WHILE still dragging", () => {
826+
const { host, root } = renderInto(
827+
<FlatSlider
828+
label="Opacity"
829+
value={10}
830+
min={0}
831+
max={100}
832+
tier="explicitCustom"
833+
displayValue="10%"
834+
onCommit={vi.fn()}
835+
/>,
836+
);
837+
const track = host.querySelector<HTMLElement>('[data-flat-slider-track="true"]');
838+
if (!track) throw new Error("expected a track element");
839+
Object.defineProperty(track, "getBoundingClientRect", {
840+
value: () => ({ left: 0, width: 100, top: 0, height: 20, right: 100, bottom: 20 }),
841+
});
842+
act(() => {
843+
track.dispatchEvent(
844+
new PointerEvent("pointerdown", { bubbles: true, clientX: 30, pointerId: 1 }),
845+
);
846+
});
847+
expect(track.getAttribute("aria-valuenow")).toBe("30");
848+
// Value changes to 99 WHILE still dragging — the [value] sync effect
849+
// must skip it (draggingRef is still true), so draft stays at 30.
850+
act(() => {
851+
root.render(
852+
<FlatSlider
853+
label="Opacity"
854+
value={99}
855+
min={0}
856+
max={100}
857+
tier="explicitCustom"
858+
displayValue="99%"
859+
onCommit={vi.fn()}
860+
/>,
861+
);
862+
});
863+
expect(track.getAttribute("aria-valuenow")).toBe("30");
864+
act(() => {
865+
// Capture lost with NO further render afterward — if the resync
866+
// depended on a subsequent [value] effect run rather than reading
867+
// latestValueRef directly, this would leave the knob stuck at 30.
868+
track.dispatchEvent(new Event("lostpointercapture", { bubbles: true }));
869+
});
870+
expect(track.getAttribute("aria-valuenow")).toBe("99");
871+
act(() => root.unmount());
872+
});
825873
});
826874

827875
describe("FlatSelectRow", () => {
@@ -967,44 +1015,3 @@ describe("FlatSelectRow — label/value options", () => {
9671015
act(() => root.unmount());
9681016
});
9691017
});
970-
971-
describe("FlatToggle", () => {
972-
it("renders the off state with a dim label and dim knob, and fires onChange(true) on click", () => {
973-
const onChange = vi.fn();
974-
const { host, root } = renderInto(
975-
<FlatToggle label="Loop" checked={false} onChange={onChange} />,
976-
);
977-
const label = host.querySelector('[data-flat-toggle-label="true"]');
978-
expect(label?.className).toContain("text-panel-text-3");
979-
const pill = host.querySelector<HTMLButtonElement>('[data-flat-toggle="true"]');
980-
expect(pill).not.toBeNull();
981-
act(() => pill?.dispatchEvent(new MouseEvent("click", { bubbles: true })));
982-
expect(onChange).toHaveBeenCalledWith(true);
983-
act(() => root.unmount());
984-
});
985-
986-
it("renders the on state with an emphasized label and mint knob, and fires onChange(false) on click", () => {
987-
const onChange = vi.fn();
988-
const { host, root } = renderInto(<FlatToggle label="Loop" checked onChange={onChange} />);
989-
const label = host.querySelector('[data-flat-toggle-label="true"]');
990-
expect(label?.className).toContain("text-panel-text-2");
991-
const knob = host.querySelector('[data-flat-toggle-knob="true"]');
992-
expect(knob?.className).toContain("bg-panel-accent");
993-
const pill = host.querySelector<HTMLButtonElement>('[data-flat-toggle="true"]');
994-
act(() => pill?.dispatchEvent(new MouseEvent("click", { bubbles: true })));
995-
expect(onChange).toHaveBeenCalledWith(false);
996-
act(() => root.unmount());
997-
});
998-
999-
it("does not fire onChange when disabled", () => {
1000-
const onChange = vi.fn();
1001-
const { host, root } = renderInto(
1002-
<FlatToggle label="Loop" checked={false} disabled onChange={onChange} />,
1003-
);
1004-
const pill = host.querySelector<HTMLButtonElement>('[data-flat-toggle="true"]');
1005-
expect(pill?.disabled).toBe(true);
1006-
act(() => pill?.dispatchEvent(new MouseEvent("click", { bubbles: true })));
1007-
expect(onChange).not.toHaveBeenCalled();
1008-
act(() => root.unmount());
1009-
});
1010-
});

‎packages/studio/src/components/editor/propertyPanelFlatPrimitives.tsx‎

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,11 @@ export function FlatSlider({
300300
// different control in between.
301301
const onCommitRef = useRef(onCommit);
302302
onCommitRef.current = onCommit;
303+
// Always this render's committed value — read directly (not via the
304+
// effect below) by onLostPointerCapture, so the resync there doesn't
305+
// depend on ordering between the native event and the [value] effect.
306+
const latestValueRef = useRef(value);
307+
latestValueRef.current = value;
303308

304309
useEffect(() => {
305310
if (draggingRef.current) return;
@@ -407,10 +412,16 @@ export function FlatSlider({
407412
onLostPointerCapture={() => {
408413
// Capture can be lost without either pointerup or pointercancel
409414
// firing first (e.g. another element steals it, or the browser
410-
// reclaims it for a scroll/touch gesture) — without this,
411-
// draggingRef stays stuck true and the knob permanently stops
412-
// syncing to the committed value prop.
415+
// reclaims it for a scroll/touch gesture). Resync immediately and
416+
// directly from latestValueRef, rather than only clearing
417+
// draggingRef and waiting for the [value] effect to notice —
418+
// that effect depends on `value` actually changing again to
419+
// re-run, so if this event and any concurrent value update are
420+
// ordered unfavorably, the knob could otherwise stay stuck at
421+
// its mid-drag position indefinitely.
413422
draggingRef.current = false;
423+
setDraft(latestValueRef.current);
424+
lastCommittedRef.current = latestValueRef.current;
414425
}}
415426
onKeyDown={(e) => {
416427
if (disabled) return;
@@ -478,6 +489,7 @@ export function FlatSlider({
478489

479490
export function FlatSelectRow({
480491
label,
492+
ariaLabel,
481493
value,
482494
options,
483495
tier,
@@ -486,6 +498,11 @@ export function FlatSelectRow({
486498
onReset,
487499
}: {
488500
label: string;
501+
/** Accessible name when a caller renders the visible label OUTSIDE this
502+
* row (label="" to avoid a duplicate) — e.g. Grade's "Preset" row, which
503+
* shows its own label span and would otherwise leave the <select>
504+
* unnamed. Falls back to `label` when omitted. */
505+
ariaLabel?: string;
489506
value: string;
490507
options: Array<string | { value: string; label: string }>;
491508
tier: PropertyValueTier;
@@ -515,7 +532,7 @@ export function FlatSelectRow({
515532
<select
516533
value={value}
517534
disabled={disabled}
518-
aria-label={label || undefined}
535+
aria-label={ariaLabel || label || undefined}
519536
onChange={(e) => onChange(e.target.value)}
520537
className={`appearance-none bg-transparent text-right font-mono text-[11px] outline-none disabled:cursor-not-allowed ${VALUE_TIER_VALUE_CLASS[tier]}`}
521538
>
@@ -551,49 +568,3 @@ export function FlatSelectRow({
551568
</div>
552569
);
553570
}
554-
555-
/* ------------------------------------------------------------------ */
556-
/* FlatToggle — 24×14 pill switch */
557-
/* ------------------------------------------------------------------ */
558-
559-
export function FlatToggle({
560-
label,
561-
checked,
562-
disabled,
563-
onChange,
564-
}: {
565-
label: string;
566-
checked: boolean;
567-
disabled?: boolean;
568-
onChange: (next: boolean) => void;
569-
}) {
570-
return (
571-
<div className="flex min-h-[30px] items-center justify-between">
572-
<span
573-
data-flat-toggle-label="true"
574-
className={`text-[11px] ${checked ? "text-panel-text-2" : "text-panel-text-3"}`}
575-
>
576-
{label}
577-
</span>
578-
<button
579-
type="button"
580-
data-flat-toggle="true"
581-
role="switch"
582-
aria-checked={checked}
583-
aria-label={label}
584-
disabled={disabled}
585-
onClick={() => onChange(!checked)}
586-
className={`relative h-[14px] w-6 flex-shrink-0 rounded-full transition-colors disabled:cursor-not-allowed disabled:opacity-50 ${
587-
checked ? "bg-panel-accent/35" : "bg-panel-hover"
588-
}`}
589-
>
590-
<span
591-
data-flat-toggle-knob="true"
592-
className={`absolute top-0.5 h-2.5 w-2.5 rounded-full transition-all ${
593-
checked ? "right-0.5 bg-panel-accent" : "left-0.5 bg-panel-text-4"
594-
}`}
595-
/>
596-
</button>
597-
</div>
598-
);
599-
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
// @vitest-environment happy-dom
2+
3+
import React, { act } from "react";
4+
import { createRoot } from "react-dom/client";
5+
import { afterEach, describe, expect, it, vi } from "vitest";
6+
import { FlatToggle } from "./propertyPanelFlatToggle";
7+
8+
(globalThis as unknown as { IS_REACT_ACT_ENVIRONMENT: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
9+
10+
afterEach(() => {
11+
document.body.innerHTML = "";
12+
});
13+
14+
function renderInto(node: React.ReactElement) {
15+
const host = document.createElement("div");
16+
document.body.append(host);
17+
const root = createRoot(host);
18+
act(() => {
19+
root.render(node);
20+
});
21+
return { host, root };
22+
}
23+
24+
describe("FlatToggle", () => {
25+
it("renders the off state with a dim label and dim knob, and fires onChange(true) on click", () => {
26+
const onChange = vi.fn();
27+
const { host, root } = renderInto(
28+
<FlatToggle label="Loop" checked={false} onChange={onChange} />,
29+
);
30+
const label = host.querySelector('[data-flat-toggle-label="true"]');
31+
expect(label?.className).toContain("text-panel-text-3");
32+
const pill = host.querySelector<HTMLButtonElement>('[data-flat-toggle="true"]');
33+
expect(pill).not.toBeNull();
34+
act(() => pill?.dispatchEvent(new MouseEvent("click", { bubbles: true })));
35+
expect(onChange).toHaveBeenCalledWith(true);
36+
act(() => root.unmount());
37+
});
38+
39+
it("renders the on state with an emphasized label and mint knob, and fires onChange(false) on click", () => {
40+
const onChange = vi.fn();
41+
const { host, root } = renderInto(<FlatToggle label="Loop" checked onChange={onChange} />);
42+
const label = host.querySelector('[data-flat-toggle-label="true"]');
43+
expect(label?.className).toContain("text-panel-text-2");
44+
const knob = host.querySelector('[data-flat-toggle-knob="true"]');
45+
expect(knob?.className).toContain("bg-panel-accent");
46+
const pill = host.querySelector<HTMLButtonElement>('[data-flat-toggle="true"]');
47+
act(() => pill?.dispatchEvent(new MouseEvent("click", { bubbles: true })));
48+
expect(onChange).toHaveBeenCalledWith(false);
49+
act(() => root.unmount());
50+
});
51+
52+
it("does not fire onChange when disabled", () => {
53+
const onChange = vi.fn();
54+
const { host, root } = renderInto(
55+
<FlatToggle label="Loop" checked={false} disabled onChange={onChange} />,
56+
);
57+
const pill = host.querySelector<HTMLButtonElement>('[data-flat-toggle="true"]');
58+
expect(pill?.disabled).toBe(true);
59+
act(() => pill?.dispatchEvent(new MouseEvent("click", { bubbles: true })));
60+
expect(onChange).not.toHaveBeenCalled();
61+
act(() => root.unmount());
62+
});
63+
});
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* ------------------------------------------------------------------ */
2+
/* FlatToggle — 24×14 pill switch */
3+
/* (split out of propertyPanelFlatPrimitives.tsx to stay under the */
4+
/* 600-line file-size gate) */
5+
/* ------------------------------------------------------------------ */
6+
7+
export function FlatToggle({
8+
label,
9+
checked,
10+
disabled,
11+
onChange,
12+
}: {
13+
label: string;
14+
checked: boolean;
15+
disabled?: boolean;
16+
onChange: (next: boolean) => void;
17+
}) {
18+
return (
19+
<div className="flex min-h-[30px] items-center justify-between">
20+
<span
21+
data-flat-toggle-label="true"
22+
className={`text-[11px] ${checked ? "text-panel-text-2" : "text-panel-text-3"}`}
23+
>
24+
{label}
25+
</span>
26+
<button
27+
type="button"
28+
data-flat-toggle="true"
29+
role="switch"
30+
aria-checked={checked}
31+
aria-label={label}
32+
disabled={disabled}
33+
onClick={() => onChange(!checked)}
34+
className={`relative h-[14px] w-6 flex-shrink-0 rounded-full transition-colors disabled:cursor-not-allowed disabled:opacity-50 ${
35+
checked ? "bg-panel-accent/35" : "bg-panel-hover"
36+
}`}
37+
>
38+
<span
39+
data-flat-toggle-knob="true"
40+
className={`absolute top-0.5 h-2.5 w-2.5 rounded-full transition-all ${
41+
checked ? "right-0.5 bg-panel-accent" : "left-0.5 bg-panel-text-4"
42+
}`}
43+
/>
44+
</button>
45+
</div>
46+
);
47+
}

0 commit comments

Comments
 (0)