Skip to content

Commit beaf4ff

Browse files
committed
fix(studio): cancel FlatSlider drags on Escape and right-click
A fresh full-stack re-review checked 15 PR heads independently. Cross- checked all 9 remaining claims against the actual current tip: - heygen-com#2120 (id/selector key qualification, Hide All no-op/race, variable parity), heygen-com#2121 (opacity-zero fallback), heygen-com#2122 (GSAP preview sibling resolution, scrub-label), heygen-com#2124/heygen-com#2126 (negative metadata cache), and the keyboard-access half of heygen-com#2121/heygen-com#2186 were all already fixed by a later commit in this same stack (65954c3, PR heygen-com#2225) — the reviewed heads predate it. Verified each in the current source rather than taking the isolated-head review at face value. - heygen-com#2186's "no ESC/right-click cancel during drag" was the one claim that held up: FlatSlider had keyboard arrow-key support but no way to abort an in-progress pointer drag. Escape now reverts to the pre-drag value and releases pointer capture; a right-click (contextmenu) during a drag does the same instead of committing whatever position the pointer last reached while the native context menu opens over the slider. Both go through commitDraft (not just a visual reset) since the drag's leading- edge commit in onPointerDown may already have applied an intermediate value that needs actually undoing, not just hiding. propertyPanelFlatPrimitives.tsx crossed the 600-line file-size gate after this change; extracted FlatSelectRow into its own file, matching the FlatToggle/FlatMaskInsetRows precedent from earlier in this stack. New regression tests for Escape-cancel and contextmenu-cancel. Full studio suite still at the known pre-existing 55-failure baseline, zero new regressions. Typecheck/oxlint/oxfmt clean.
1 parent 39107ac commit beaf4ff

3 files changed

Lines changed: 209 additions & 85 deletions

File tree

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

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,86 @@ describe("FlatSlider", () => {
473473
expect(track.getAttribute("aria-valuenow")).toBe("80");
474474
act(() => root.unmount());
475475
});
476+
477+
it("Escape during a drag reverts to the pre-drag value and releases pointer capture, instead of leaving the last dragged-to position committed", () => {
478+
const onCommit = vi.fn();
479+
const { host, root } = renderInto(
480+
<FlatSlider
481+
label="Opacity"
482+
value={10}
483+
min={0}
484+
max={100}
485+
tier="explicitCustom"
486+
displayValue="10%"
487+
onCommit={onCommit}
488+
/>,
489+
);
490+
const track = host.querySelector<HTMLElement>('[data-flat-slider-track="true"]');
491+
if (!track) throw new Error("expected a track element");
492+
Object.defineProperty(track, "getBoundingClientRect", {
493+
value: () => ({ left: 0, width: 100, top: 0, height: 20, right: 100, bottom: 20 }),
494+
});
495+
act(() => {
496+
track.dispatchEvent(
497+
new PointerEvent("pointerdown", { bubbles: true, clientX: 30, pointerId: 1 }),
498+
);
499+
});
500+
// The leading-edge commit already applied the dragged-to value (30).
501+
expect(onCommit).toHaveBeenLastCalledWith(30);
502+
act(() => {
503+
track.dispatchEvent(
504+
new KeyboardEvent("keydown", { key: "Escape", bubbles: true, cancelable: true }),
505+
);
506+
});
507+
expect(onCommit).toHaveBeenLastCalledWith(10);
508+
expect(track.getAttribute("aria-valuenow")).toBe("10");
509+
expect(track.hasPointerCapture(1)).toBe(false);
510+
// A subsequent pointermove for the now-released pointer must not resume
511+
// the cancelled drag.
512+
onCommit.mockClear();
513+
act(() => {
514+
track.dispatchEvent(
515+
new PointerEvent("pointermove", { bubbles: true, clientX: 80, pointerId: 1 }),
516+
);
517+
});
518+
expect(onCommit).not.toHaveBeenCalled();
519+
act(() => root.unmount());
520+
});
521+
522+
it("right-click (contextmenu) during a drag cancels it and reverts to the pre-drag value, instead of committing the last dragged-to position", () => {
523+
const onCommit = vi.fn();
524+
const { host, root } = renderInto(
525+
<FlatSlider
526+
label="Opacity"
527+
value={10}
528+
min={0}
529+
max={100}
530+
tier="explicitCustom"
531+
displayValue="10%"
532+
onCommit={onCommit}
533+
/>,
534+
);
535+
const track = host.querySelector<HTMLElement>('[data-flat-slider-track="true"]');
536+
if (!track) throw new Error("expected a track element");
537+
Object.defineProperty(track, "getBoundingClientRect", {
538+
value: () => ({ left: 0, width: 100, top: 0, height: 20, right: 100, bottom: 20 }),
539+
});
540+
act(() => {
541+
track.dispatchEvent(
542+
new PointerEvent("pointerdown", { bubbles: true, clientX: 65, pointerId: 1 }),
543+
);
544+
});
545+
expect(onCommit).toHaveBeenLastCalledWith(65);
546+
const contextMenuEvent = new MouseEvent("contextmenu", { bubbles: true, cancelable: true });
547+
act(() => {
548+
track.dispatchEvent(contextMenuEvent);
549+
});
550+
expect(contextMenuEvent.defaultPrevented).toBe(true);
551+
expect(onCommit).toHaveBeenLastCalledWith(10);
552+
expect(track.getAttribute("aria-valuenow")).toBe("10");
553+
expect(track.hasPointerCapture(1)).toBe(false);
554+
act(() => root.unmount());
555+
});
476556
});
477557

478558
describe("FlatSlider — Grade extensions", () => {

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

Lines changed: 37 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,11 @@ export function FlatSlider({
318318
// loss (another element steals it, or the browser reclaims it for a
319319
// scroll/touch gesture) where no other handler is about to run.
320320
const explicitReleaseRef = useRef(false);
321+
// The committed value when the current drag began — Escape and right-click
322+
// both cancel an in-progress drag by reverting to this, not by leaving
323+
// whatever position the pointer last reached committed.
324+
const dragStartValueRef = useRef(value);
325+
const activePointerIdRef = useRef<number | null>(null);
321326

322327
useEffect(() => {
323328
if (draggingRef.current) return;
@@ -372,6 +377,22 @@ export function FlatSlider({
372377
}, 40 - elapsed);
373378
}
374379
};
380+
// Reverts to the pre-drag value instead of leaving whatever position the
381+
// pointer last reached committed — the drag's own leading-edge commit (in
382+
// onPointerDown) may already have applied an intermediate value, so this
383+
// must go through commitDraft (not just a visual setDraft) to actually
384+
// undo it.
385+
const cancelDrag = (target: HTMLDivElement) => {
386+
if (!draggingRef.current) return;
387+
draggingRef.current = false;
388+
const pointerId = activePointerIdRef.current;
389+
if (pointerId !== null && target.hasPointerCapture(pointerId)) {
390+
explicitReleaseRef.current = true;
391+
target.releasePointerCapture(pointerId);
392+
}
393+
setDraft(dragStartValueRef.current);
394+
commitDraft(dragStartValueRef.current);
395+
};
375396

376397
return (
377398
<div className="flex min-h-[28px] items-center gap-2.5">
@@ -390,6 +411,8 @@ export function FlatSlider({
390411
onPointerDown={(e) => {
391412
if (disabled) return;
392413
draggingRef.current = true;
414+
dragStartValueRef.current = latestValueRef.current;
415+
activePointerIdRef.current = e.pointerId;
393416
e.currentTarget.setPointerCapture(e.pointerId);
394417
const stepped = stepFromClientX(e.clientX, e.currentTarget.getBoundingClientRect());
395418
setDraft(stepped);
@@ -447,12 +470,25 @@ export function FlatSlider({
447470
}}
448471
onKeyDown={(e) => {
449472
if (disabled) return;
473+
if (e.key === "Escape" && draggingRef.current) {
474+
e.preventDefault();
475+
cancelDrag(e.currentTarget);
476+
return;
477+
}
450478
const next = sliderKeyTarget(e.key, draft, min, max, step);
451479
if (next === null) return;
452480
e.preventDefault();
453481
setDraft(next);
454482
commitDraft(next);
455483
}}
484+
onContextMenu={(e) => {
485+
// Right-click during a drag must cancel it (revert to the pre-drag
486+
// value), not leave the last dragged-to position committed while
487+
// the native context menu opens on top of the slider.
488+
if (!draggingRef.current) return;
489+
e.preventDefault();
490+
cancelDrag(e.currentTarget);
491+
}}
456492
>
457493
<div className="absolute inset-x-0 top-1/2 h-0.5 -translate-y-1/2 rounded-full bg-panel-hover">
458494
{centerTick && (
@@ -505,88 +541,4 @@ export function FlatSlider({
505541
);
506542
}
507543

508-
/* ------------------------------------------------------------------ */
509-
/* FlatSelectRow — label/value row backed by a native <select> */
510-
/* ------------------------------------------------------------------ */
511-
512-
export function FlatSelectRow({
513-
label,
514-
ariaLabel,
515-
value,
516-
options,
517-
tier,
518-
disabled,
519-
onChange,
520-
onReset,
521-
}: {
522-
label: string;
523-
/** Accessible name when a caller renders the visible label OUTSIDE this
524-
* row (label="" to avoid a duplicate) — e.g. Grade's "Preset" row, which
525-
* shows its own label span and would otherwise leave the <select>
526-
* unnamed. Falls back to `label` when omitted. */
527-
ariaLabel?: string;
528-
value: string;
529-
options: Array<string | { value: string; label: string }>;
530-
tier: PropertyValueTier;
531-
disabled?: boolean;
532-
onChange: (nextValue: string) => void;
533-
onReset?: () => void;
534-
}) {
535-
const normalizedOptions = options.map((option) =>
536-
typeof option === "string" ? { value: option, label: option } : option,
537-
);
538-
// A valid authored value outside the preset list (e.g. a `mix-blend-mode`
539-
// or `object-position` this row doesn't offer as a preset) must not be
540-
// silently misrepresented as the first option — the native <select> falls
541-
// back to selectedIndex 0 when `value` matches no <option>, and reselecting
542-
// that visible-but-wrong preset overwrites the real persisted value. Prepend
543-
// the current value so it's always representable, matching legacy
544-
// `SelectField`'s same guard.
545-
const renderedOptions =
546-
value && !normalizedOptions.some((option) => option.value === value)
547-
? [{ value, label: value }, ...normalizedOptions]
548-
: normalizedOptions;
549-
return (
550-
<div className="group flex min-h-[30px] items-center justify-between">
551-
<span className={`text-[11px] ${VALUE_TIER_LABEL_CLASS[tier]}`}>{label}</span>
552-
<span className="flex items-center gap-2">
553-
<label className="flex items-center gap-1.5">
554-
<select
555-
value={value}
556-
disabled={disabled}
557-
aria-label={ariaLabel || label || undefined}
558-
onChange={(e) => onChange(e.target.value)}
559-
className={`appearance-none bg-transparent text-right font-mono text-[11px] outline-none disabled:cursor-not-allowed ${VALUE_TIER_VALUE_CLASS[tier]}`}
560-
>
561-
{renderedOptions.map((option) => (
562-
<option key={option.value} value={option.value}>
563-
{option.label}
564-
</option>
565-
))}
566-
</select>
567-
<svg
568-
width="10"
569-
height="10"
570-
viewBox="0 0 10 10"
571-
fill="currentColor"
572-
className="flex-shrink-0 text-panel-text-5"
573-
>
574-
<path d="M2 3l3 4 3-4z" />
575-
</svg>
576-
</label>
577-
{tier === "explicitCustom" && onReset && (
578-
<button
579-
type="button"
580-
data-flat-select-reset="true"
581-
title="Remove — fall back to default"
582-
disabled={disabled}
583-
onClick={onReset}
584-
className="flex-shrink-0 text-panel-text-3 opacity-0 transition-opacity hover:text-panel-text-1 group-hover:opacity-100 disabled:cursor-not-allowed disabled:opacity-40"
585-
>
586-
<RotateCcw size={11} />
587-
</button>
588-
)}
589-
</span>
590-
</div>
591-
);
592-
}
544+
export { FlatSelectRow } from "./propertyPanelFlatSelectRow";
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
import { RotateCcw } from "../../icons/SystemIcons";
2+
import {
3+
VALUE_TIER_LABEL_CLASS,
4+
VALUE_TIER_VALUE_CLASS,
5+
type PropertyValueTier,
6+
} from "./propertyPanelValueTier";
7+
8+
/* ------------------------------------------------------------------ */
9+
/* FlatSelectRow — label/value row backed by a native <select> */
10+
/* ------------------------------------------------------------------ */
11+
12+
export function FlatSelectRow({
13+
label,
14+
ariaLabel,
15+
value,
16+
options,
17+
tier,
18+
disabled,
19+
onChange,
20+
onReset,
21+
}: {
22+
label: string;
23+
/** Accessible name when a caller renders the visible label OUTSIDE this
24+
* row (label="" to avoid a duplicate) — e.g. Grade's "Preset" row, which
25+
* shows its own label span and would otherwise leave the <select>
26+
* unnamed. Falls back to `label` when omitted. */
27+
ariaLabel?: string;
28+
value: string;
29+
options: Array<string | { value: string; label: string }>;
30+
tier: PropertyValueTier;
31+
disabled?: boolean;
32+
onChange: (nextValue: string) => void;
33+
onReset?: () => void;
34+
}) {
35+
const normalizedOptions = options.map((option) =>
36+
typeof option === "string" ? { value: option, label: option } : option,
37+
);
38+
// A valid authored value outside the preset list (e.g. a `mix-blend-mode`
39+
// or `object-position` this row doesn't offer as a preset) must not be
40+
// silently misrepresented as the first option — the native <select> falls
41+
// back to selectedIndex 0 when `value` matches no <option>, and reselecting
42+
// that visible-but-wrong preset overwrites the real persisted value. Prepend
43+
// the current value so it's always representable, matching legacy
44+
// `SelectField`'s same guard.
45+
const renderedOptions =
46+
value && !normalizedOptions.some((option) => option.value === value)
47+
? [{ value, label: value }, ...normalizedOptions]
48+
: normalizedOptions;
49+
return (
50+
<div className="group flex min-h-[30px] items-center justify-between">
51+
<span className={`text-[11px] ${VALUE_TIER_LABEL_CLASS[tier]}`}>{label}</span>
52+
<span className="flex items-center gap-2">
53+
<label className="flex items-center gap-1.5">
54+
<select
55+
value={value}
56+
disabled={disabled}
57+
aria-label={ariaLabel || label || undefined}
58+
onChange={(e) => onChange(e.target.value)}
59+
className={`appearance-none bg-transparent text-right font-mono text-[11px] outline-none disabled:cursor-not-allowed ${VALUE_TIER_VALUE_CLASS[tier]}`}
60+
>
61+
{renderedOptions.map((option) => (
62+
<option key={option.value} value={option.value}>
63+
{option.label}
64+
</option>
65+
))}
66+
</select>
67+
<svg
68+
width="10"
69+
height="10"
70+
viewBox="0 0 10 10"
71+
fill="currentColor"
72+
className="flex-shrink-0 text-panel-text-5"
73+
>
74+
<path d="M2 3l3 4 3-4z" />
75+
</svg>
76+
</label>
77+
{tier === "explicitCustom" && onReset && (
78+
<button
79+
type="button"
80+
data-flat-select-reset="true"
81+
title="Remove — fall back to default"
82+
disabled={disabled}
83+
onClick={onReset}
84+
className="flex-shrink-0 text-panel-text-3 opacity-0 transition-opacity hover:text-panel-text-1 group-hover:opacity-100 disabled:cursor-not-allowed disabled:opacity-40"
85+
>
86+
<RotateCcw size={11} />
87+
</button>
88+
)}
89+
</span>
90+
</div>
91+
);
92+
}

0 commit comments

Comments
 (0)