Skip to content

Commit 7a2ccdc

Browse files
vanceingallsclaude
andcommitted
fix(studio): support drag on FlatSlider, not just click-to-set
Only onPointerDown was wired, so dragging the knob/track only ever committed the initial click position — nothing tracked the pointer after that. Uses the Pointer Capture API (setPointerCapture on pointerdown, onPointerMove while captured, release on pointerup) so the value follows the cursor continuously during a drag, matching how the legacy native <input type="range"> control behaves for free. Co-Authored-By: Claude Sonnet 5 <[email protected]>
1 parent 415ac65 commit 7a2ccdc

2 files changed

Lines changed: 86 additions & 0 deletions

File tree

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,82 @@ describe("FlatSlider", () => {
280280
expect(onCommit).toHaveBeenCalledWith(10);
281281
act(() => root.unmount());
282282
});
283+
284+
it("commits continuously while dragging, not just on the initial pointerdown", () => {
285+
const onCommit = vi.fn();
286+
const { host, root } = renderInto(
287+
<FlatSlider
288+
label="Opacity"
289+
value={50}
290+
min={0}
291+
max={100}
292+
tier="explicitCustom"
293+
displayValue="50%"
294+
onCommit={onCommit}
295+
/>,
296+
);
297+
const track = host.querySelector<HTMLElement>('[data-flat-slider-track="true"]');
298+
if (!track) throw new Error("expected a track element");
299+
Object.defineProperty(track, "getBoundingClientRect", {
300+
value: () => ({ left: 0, width: 200, top: 0, height: 20, right: 200, bottom: 20 }),
301+
});
302+
act(() => {
303+
track.dispatchEvent(
304+
new PointerEvent("pointerdown", { bubbles: true, clientX: 20, pointerId: 1 }),
305+
);
306+
});
307+
expect(onCommit).toHaveBeenLastCalledWith(10);
308+
act(() => {
309+
track.dispatchEvent(
310+
new PointerEvent("pointermove", { bubbles: true, clientX: 160, pointerId: 1 }),
311+
);
312+
});
313+
expect(onCommit).toHaveBeenLastCalledWith(80);
314+
act(() => {
315+
track.dispatchEvent(
316+
new PointerEvent("pointermove", { bubbles: true, clientX: 100, pointerId: 1 }),
317+
);
318+
});
319+
expect(onCommit).toHaveBeenLastCalledWith(50);
320+
act(() => {
321+
track.dispatchEvent(new PointerEvent("pointerup", { bubbles: true, pointerId: 1 }));
322+
});
323+
act(() => root.unmount());
324+
});
325+
326+
it("ignores pointermove once a drag has ended (pointer capture released)", () => {
327+
const onCommit = vi.fn();
328+
const { host, root } = renderInto(
329+
<FlatSlider
330+
label="Opacity"
331+
value={50}
332+
min={0}
333+
max={100}
334+
tier="explicitCustom"
335+
displayValue="50%"
336+
onCommit={onCommit}
337+
/>,
338+
);
339+
const track = host.querySelector<HTMLElement>('[data-flat-slider-track="true"]');
340+
if (!track) throw new Error("expected a track element");
341+
Object.defineProperty(track, "getBoundingClientRect", {
342+
value: () => ({ left: 0, width: 200, top: 0, height: 20, right: 200, bottom: 20 }),
343+
});
344+
act(() => {
345+
track.dispatchEvent(
346+
new PointerEvent("pointerdown", { bubbles: true, clientX: 20, pointerId: 1 }),
347+
);
348+
track.dispatchEvent(new PointerEvent("pointerup", { bubbles: true, pointerId: 1 }));
349+
});
350+
onCommit.mockClear();
351+
act(() => {
352+
track.dispatchEvent(
353+
new PointerEvent("pointermove", { bubbles: true, clientX: 160, pointerId: 1 }),
354+
);
355+
});
356+
expect(onCommit).not.toHaveBeenCalled();
357+
act(() => root.unmount());
358+
});
283359
});
284360

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

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,8 +262,18 @@ export function FlatSlider({
262262
className={`relative h-5 flex-1 ${disabled ? "cursor-not-allowed" : "cursor-pointer"}`}
263263
onPointerDown={(e) => {
264264
if (disabled) return;
265+
e.currentTarget.setPointerCapture(e.pointerId);
265266
commitFromClientX(e.clientX, e.currentTarget.getBoundingClientRect());
266267
}}
268+
onPointerMove={(e) => {
269+
if (disabled || !e.currentTarget.hasPointerCapture(e.pointerId)) return;
270+
commitFromClientX(e.clientX, e.currentTarget.getBoundingClientRect());
271+
}}
272+
onPointerUp={(e) => {
273+
if (e.currentTarget.hasPointerCapture(e.pointerId)) {
274+
e.currentTarget.releasePointerCapture(e.pointerId);
275+
}
276+
}}
267277
>
268278
<div className="absolute inset-x-0 top-1/2 h-0.5 -translate-y-1/2 rounded-full bg-panel-hover">
269279
{centerTick && (

0 commit comments

Comments
 (0)