Skip to content

Commit d10ce38

Browse files
committed
fix(studio): debounce flat slider drag commits to prevent rapid-fire writes
1 parent ad36891 commit d10ce38

5 files changed

Lines changed: 114 additions & 15 deletions

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ function dragSliderTrack(row: Element, clientX: number, trackWidth: number) {
5252
});
5353
act(() => {
5454
track.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, clientX }));
55+
track.dispatchEvent(new MouseEvent("pointerup", { bubbles: true, clientX }));
5556
});
5657
}
5758

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ describe("FlatMediaSection — volume/rate/media-start", () => {
155155

156156
it("commits a new volume value on slider track pointerdown", () => {
157157
const onSetAttribute = vi.fn();
158-
const element = makeVideoElement({ dataAttributes: { volume: "0.5" } });
158+
const element = makeVideoElement({ dataAttributes: { volume: "0.2" } });
159159
const host = document.createElement("div");
160160
document.body.append(host);
161161
const root = createRoot(host);
@@ -177,8 +177,9 @@ describe("FlatMediaSection — volume/rate/media-start", () => {
177177
});
178178
act(() => {
179179
volumeTrack.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, clientX: 50 }));
180+
volumeTrack.dispatchEvent(new MouseEvent("pointerup", { bubbles: true, clientX: 50 }));
180181
});
181-
// min=0, max=100, ratio=0.5 -> raw=50 -> commit(50) -> 50/100=0.5 -> "0.5"
182+
// starting volume 0.2 (draft=20); min=0, max=100, ratio=0.5 -> raw=50 -> commit(50) -> 50/100=0.5 -> "0.5"
182183
expect(onSetAttribute).toHaveBeenCalledWith("volume", "0.5");
183184
act(() => root.unmount());
184185
});
@@ -207,6 +208,7 @@ describe("FlatMediaSection — volume/rate/media-start", () => {
207208
});
208209
act(() => {
209210
rateTrack.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, clientX: 100 }));
211+
rateTrack.dispatchEvent(new MouseEvent("pointerup", { bubbles: true, clientX: 100 }));
210212
});
211213
// min=25, max=300, ratio=1.0 -> raw=300 -> commit(300) -> 300/100=3 -> "3"
212214
expect(onSetAttribute).toHaveBeenCalledWith("playback-rate", "3");
@@ -237,6 +239,7 @@ describe("FlatMediaSection — volume/rate/media-start", () => {
237239
});
238240
act(() => {
239241
mediaStartTrack.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, clientX: 100 }));
242+
mediaStartTrack.dispatchEvent(new MouseEvent("pointerup", { bubbles: true, clientX: 100 }));
240243
});
241244
// no source-duration set -> mediaStartMax=Math.max(30, Math.ceil(0+10))=30 -> max=3000
242245
// ratio=1.0 -> raw=3000 -> commit(3000) -> (3000/100).toFixed(2) = "30.00"

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

Lines changed: 56 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,11 +234,11 @@ describe("FlatSlider", () => {
234234
const { host, root } = renderInto(
235235
<FlatSlider
236236
label="Opacity"
237-
value={50}
237+
value={10}
238238
min={0}
239239
max={100}
240240
tier="explicitCustom"
241-
displayValue="50%"
241+
displayValue="10%"
242242
onCommit={onCommit}
243243
/>,
244244
);
@@ -249,6 +249,7 @@ describe("FlatSlider", () => {
249249
});
250250
act(() => {
251251
track.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, clientX: 100 }));
252+
track.dispatchEvent(new MouseEvent("pointerup", { bubbles: true, clientX: 100 }));
252253
});
253254
expect(onCommit).toHaveBeenCalledWith(50);
254255
act(() => root.unmount());
@@ -276,12 +277,13 @@ describe("FlatSlider", () => {
276277
track.dispatchEvent(
277278
new MouseEvent("pointerdown", { bubbles: true, clientX: 20, clientY: 18 }),
278279
);
280+
track.dispatchEvent(new MouseEvent("pointerup", { bubbles: true, clientX: 20 }));
279281
});
280282
expect(onCommit).toHaveBeenCalledWith(10);
281283
act(() => root.unmount());
282284
});
283285

284-
it("commits continuously while dragging, not just on the initial pointerdown", () => {
286+
it("tracks the knob instantly on every pointermove during a drag (draft state)", () => {
285287
const onCommit = vi.fn();
286288
const { host, root } = renderInto(
287289
<FlatSlider
@@ -304,25 +306,73 @@ describe("FlatSlider", () => {
304306
new PointerEvent("pointerdown", { bubbles: true, clientX: 20, pointerId: 1 }),
305307
);
306308
});
307-
expect(onCommit).toHaveBeenLastCalledWith(10);
309+
// Instant, un-debounced knob feedback via aria-valuenow (draft state) —
310+
// this must update on every pointermove regardless of the commit debounce.
311+
expect(track.getAttribute("aria-valuenow")).toBe("10");
308312
act(() => {
309313
track.dispatchEvent(
310314
new PointerEvent("pointermove", { bubbles: true, clientX: 160, pointerId: 1 }),
311315
);
312316
});
313-
expect(onCommit).toHaveBeenLastCalledWith(80);
317+
expect(track.getAttribute("aria-valuenow")).toBe("80");
314318
act(() => {
315319
track.dispatchEvent(
316320
new PointerEvent("pointermove", { bubbles: true, clientX: 100, pointerId: 1 }),
317321
);
318322
});
319-
expect(onCommit).toHaveBeenLastCalledWith(50);
323+
expect(track.getAttribute("aria-valuenow")).toBe("50");
320324
act(() => {
321325
track.dispatchEvent(new PointerEvent("pointerup", { bubbles: true, pointerId: 1 }));
322326
});
323327
act(() => root.unmount());
324328
});
325329

330+
it("coalesces rapid drag commits to only the final value on release, not every step", () => {
331+
const onCommit = vi.fn();
332+
const { host, root } = renderInto(
333+
<FlatSlider
334+
label="Opacity"
335+
value={5}
336+
min={0}
337+
max={100}
338+
tier="explicitCustom"
339+
displayValue="5%"
340+
onCommit={onCommit}
341+
/>,
342+
);
343+
const track = host.querySelector<HTMLElement>('[data-flat-slider-track="true"]');
344+
if (!track) throw new Error("expected a track element");
345+
Object.defineProperty(track, "getBoundingClientRect", {
346+
value: () => ({ left: 0, width: 200, top: 0, height: 20, right: 200, bottom: 20 }),
347+
});
348+
act(() => {
349+
track.dispatchEvent(
350+
new PointerEvent("pointerdown", { bubbles: true, clientX: 20, pointerId: 1 }),
351+
);
352+
track.dispatchEvent(
353+
new PointerEvent("pointermove", { bubbles: true, clientX: 160, pointerId: 1 }),
354+
);
355+
track.dispatchEvent(
356+
new PointerEvent("pointermove", { bubbles: true, clientX: 100, pointerId: 1 }),
357+
);
358+
});
359+
// None of the rapid intermediate positions (10, 80) have committed yet —
360+
// only the debounce timer or the pointerup flush should ever call onCommit.
361+
expect(onCommit).not.toHaveBeenCalled();
362+
act(() => {
363+
// Real pointerup events always carry the pointer's true release position
364+
// (matches the last pointermove) — the handler recomputes from this
365+
// rather than trusting a possibly-stale `draft` closure.
366+
track.dispatchEvent(
367+
new PointerEvent("pointerup", { bubbles: true, clientX: 100, pointerId: 1 }),
368+
);
369+
});
370+
// Release flushes immediately with the LAST position only.
371+
expect(onCommit).toHaveBeenCalledTimes(1);
372+
expect(onCommit).toHaveBeenCalledWith(50);
373+
act(() => root.unmount());
374+
});
375+
326376
it("ignores pointermove once a drag has ended (pointer capture released)", () => {
327377
const onCommit = vi.fn();
328378
const { host, root } = renderInto(

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

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type ReactNode } from "react";
1+
import { useEffect, useRef, useState, type ReactNode } from "react";
22
import { RotateCcw } from "../../icons/SystemIcons";
33
import { CommitField } from "./propertyPanelPrimitives";
44
import {
@@ -241,13 +241,43 @@ export function FlatSlider({
241241
onReset?: () => void;
242242
onCommit: (nextValue: number) => void;
243243
}) {
244-
const clampedPct = Math.max(0, Math.min(100, ((value - min) / Math.max(max - min, 1e-6)) * 100));
244+
// Draft/debounce mirrors the legacy SliderControl (propertyPanelPrimitives.tsx):
245+
// a real drag fires pointermove far faster than any commit should hit the
246+
// network — `draft` gives the knob instant, drag-local feedback while the
247+
// actual onCommit call is coalesced to the last value every 40ms, with an
248+
// immediate flush on release so the drag never waits out the debounce.
249+
const [draft, setDraft] = useState(value);
250+
const commitTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
251+
const valueRef = useRef(value);
252+
valueRef.current = value;
245253

246-
const commitFromClientX = (clientX: number, rect: DOMRect) => {
254+
useEffect(() => {
255+
setDraft(value);
256+
}, [value]);
257+
useEffect(
258+
() => () => {
259+
if (commitTimerRef.current) clearTimeout(commitTimerRef.current);
260+
},
261+
[],
262+
);
263+
264+
const clampedPct = Math.max(0, Math.min(100, ((draft - min) / Math.max(max - min, 1e-6)) * 100));
265+
266+
const stepFromClientX = (clientX: number, rect: DOMRect) => {
247267
const ratio = Math.max(0, Math.min(1, (clientX - rect.left) / Math.max(rect.width, 1)));
248268
const raw = min + ratio * (max - min);
249269
const stepped = Math.round(raw / step) * step;
250-
onCommit(Math.max(min, Math.min(max, stepped)));
270+
return Math.max(min, Math.min(max, stepped));
271+
};
272+
const commitDraft = (nextDraft: number) => {
273+
if (commitTimerRef.current) clearTimeout(commitTimerRef.current);
274+
if (nextDraft !== valueRef.current) onCommit(nextDraft);
275+
};
276+
const scheduleCommit = (nextDraft: number) => {
277+
if (commitTimerRef.current) clearTimeout(commitTimerRef.current);
278+
commitTimerRef.current = setTimeout(() => {
279+
if (nextDraft !== valueRef.current) onCommit(nextDraft);
280+
}, 40);
251281
};
252282

253283
return (
@@ -257,22 +287,33 @@ export function FlatSlider({
257287
data-flat-slider-track="true"
258288
role="slider"
259289
aria-label={label}
260-
aria-valuenow={value}
290+
aria-valuenow={draft}
261291
aria-disabled={disabled}
262292
className={`relative h-5 flex-1 ${disabled ? "cursor-not-allowed" : "cursor-pointer"}`}
263293
onPointerDown={(e) => {
264294
if (disabled) return;
265295
e.currentTarget.setPointerCapture(e.pointerId);
266-
commitFromClientX(e.clientX, e.currentTarget.getBoundingClientRect());
296+
const stepped = stepFromClientX(e.clientX, e.currentTarget.getBoundingClientRect());
297+
setDraft(stepped);
298+
scheduleCommit(stepped);
267299
}}
268300
onPointerMove={(e) => {
269301
if (disabled || !e.currentTarget.hasPointerCapture(e.pointerId)) return;
270-
commitFromClientX(e.clientX, e.currentTarget.getBoundingClientRect());
302+
const stepped = stepFromClientX(e.clientX, e.currentTarget.getBoundingClientRect());
303+
setDraft(stepped);
304+
scheduleCommit(stepped);
271305
}}
272306
onPointerUp={(e) => {
273307
if (e.currentTarget.hasPointerCapture(e.pointerId)) {
274308
e.currentTarget.releasePointerCapture(e.pointerId);
275309
}
310+
// Recompute from the event itself rather than reading the `draft`
311+
// closure — if pointerdown+pointerup land in the same React batch
312+
// (e.g. a very fast click), the onPointerUp handler can still be
313+
// bound to the pre-drag render, making `draft` stale.
314+
const stepped = stepFromClientX(e.clientX, e.currentTarget.getBoundingClientRect());
315+
setDraft(stepped);
316+
commitDraft(stepped);
276317
}}
277318
>
278319
<div className="absolute inset-x-0 top-1/2 h-0.5 -translate-y-1/2 rounded-full bg-panel-hover">

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ describe("FlatStyleSection — blur sliders", () => {
372372
});
373373
act(() => {
374374
track.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, clientX: 50 }));
375+
track.dispatchEvent(new MouseEvent("pointerup", { bubbles: true, clientX: 50 }));
375376
});
376377
// filterBlurValue=4 -> max=Math.max(40, 4)=40; clientX=50 of width 100 -> ratio 0.5 -> 20px.
377378
expect(onSetStyle).toHaveBeenCalledWith("filter", "blur(20px)");
@@ -390,6 +391,7 @@ describe("FlatStyleSection — blur sliders", () => {
390391
});
391392
act(() => {
392393
backdropTrack.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, clientX: 50 }));
394+
backdropTrack.dispatchEvent(new MouseEvent("pointerup", { bubbles: true, clientX: 50 }));
393395
});
394396
// backdropBlurValue=6 -> max=Math.max(60, 6)=60; clientX=50 of width 100 -> ratio 0.5 -> 30px.
395397
expect(onSetStyle).toHaveBeenCalledWith("backdrop-filter", "blur(30px)");
@@ -551,6 +553,7 @@ describe("FlatStyleSection — Overflow and Mask", () => {
551553
});
552554
act(() => {
553555
maskInsetTrack.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, clientX: 50 }));
556+
maskInsetTrack.dispatchEvent(new MouseEvent("pointerup", { bubbles: true, clientX: 50 }));
554557
});
555558
// clipInsetValue=8 -> max=Math.max(120, 8)=120; clientX=50 of width 100 -> ratio 0.5 -> 60px.
556559
// border-radius is unset here, so the clip-path's own `round 4px` is not reused — radiusValue
@@ -587,6 +590,7 @@ describe("FlatStyleSection — Opacity", () => {
587590
});
588591
act(() => {
589592
opacityTrack.dispatchEvent(new MouseEvent("pointerdown", { bubbles: true, clientX: 50 }));
593+
opacityTrack.dispatchEvent(new MouseEvent("pointerup", { bubbles: true, clientX: 50 }));
590594
});
591595
expect(onSetStyle).toHaveBeenCalledWith("opacity", "0.5");
592596
act(() => root.unmount());

0 commit comments

Comments
 (0)