Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

fix(Slider): a drag belongs to the pointer that started it (#8152)

Christopher Chedeau655b54364aa6
diff --git a/.changeset/slider-one-pointer-per-drag.md b/.changeset/slider-one-pointer-per-drag.md
new file mode 100644
index 0000000000..efec21482e
--- /dev/null
+++ b/.changeset/slider-one-pointer-per-drag.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] Slider: a drag belongs to the pointer that started it. A second finger that lands on the slider, moves or lifts during a drag no longer moves the value, takes over the drag or ends it, and two fingers can still drag two sliders at once. A drag whose pointer capture is lost without a pointer up also ends, with one `onChangeEnd`, so the slider keeps answering new presses.
+
+@vjeux
diff --git a/packages/core/src/Slider/Slider.test.tsx b/packages/core/src/Slider/Slider.test.tsx
index 9f7c96cee1..32ebd2b421 100644
--- a/packages/core/src/Slider/Slider.test.tsx
+++ b/packages/core/src/Slider/Slider.test.tsx
@@ -19,6 +19,7 @@ import {
} from '../__tests__/pressState';
import * as stylex from '@stylexjs/stylex';
import {Slider} from './Slider';
+import {FormLayout} from '../FormLayout';
import {focusOutlineStyles} from '../utils/focusOutline.stylex';
import {
__resetInteractionModalityForTest,
@@ -1330,6 +1331,148 @@ describe('Slider', () => {
});
});
+describe('one pointer per drag', () => {
+ // valueDisplay="none" keeps the thumb a direct child of the track, so the
+ // mocked rect lands on it: 200 px wide, thumbs travel 10 px (0) to 190 px
+ // (100), value = (x - 10) / 1.8.
+ function ControlledSlider({
+ id,
+ onChangeEnd,
+ }: {
+ id: string;
+ onChangeEnd?: (value: number) => void;
+ }) {
+ const [value, setValue] = useState(50);
+ return (
+ <Slider
+ label={id}
+ value={value}
+ min={0}
+ max={100}
+ valueDisplay="none"
+ onChange={setValue}
+ onChangeEnd={onChangeEnd}
+ />
+ );
+ }
+
+ function trackOf(thumb: HTMLElement): HTMLElement {
+ const track = thumb.parentElement!;
+ track.getBoundingClientRect = () => ({
+ left: 0,
+ top: 0,
+ right: 200,
+ bottom: 20,
+ width: 200,
+ height: 20,
+ x: 0,
+ y: 0,
+ toJSON: () => {},
+ });
+ return track;
+ }
+
+ it('ignores a second finger that lands, moves or lifts during a drag', () => {
+ const onChangeEnd = vi.fn();
+ render(<ControlledSlider id="Volume" onChangeEnd={onChangeEnd} />);
+ const thumb = screen.getByRole('slider');
+ const track = trackOf(thumb);
+
+ fireEvent.pointerDown(track, {clientX: 100, clientY: 10, pointerId: 1});
+ fireEvent.pointerDown(track, {clientX: 28, clientY: 10, pointerId: 2});
+ expect(thumb).toHaveAttribute('aria-valuenow', '50');
+
+ fireEvent.pointerMove(track, {clientX: 136, clientY: 10, pointerId: 1});
+ fireEvent.pointerMove(track, {clientX: 46, clientY: 10, pointerId: 2});
+ expect(thumb).toHaveAttribute('aria-valuenow', '70');
+
+ // The second finger lifting does not end the first finger's drag.
+ fireEvent.pointerUp(track, {clientX: 46, clientY: 10, pointerId: 2});
+ expect(onChangeEnd).not.toHaveBeenCalled();
+ expect(declaresPressedOverlay(thumb)).toBe(true);
+
+ fireEvent.pointerMove(track, {clientX: 154, clientY: 10, pointerId: 1});
+ fireEvent.pointerUp(track, {clientX: 154, clientY: 10, pointerId: 1});
+ expect(thumb).toHaveAttribute('aria-valuenow', '80');
+ expect(onChangeEnd).toHaveBeenCalledOnce();
+ expect(onChangeEnd).toHaveBeenCalledWith(80);
+ expect(declaresPressedOverlay(thumb)).toBe(false);
+ });
+
+ it('ends the drag when its pointer capture is lost without an up', () => {
+ const onChangeEnd = vi.fn();
+ render(<ControlledSlider id="Volume" onChangeEnd={onChangeEnd} />);
+ const thumb = screen.getByRole('slider');
+ const track = trackOf(thumb);
+
+ fireEvent.pointerDown(track, {clientX: 136, clientY: 10, pointerId: 1});
+ fireEvent.lostPointerCapture(track, {pointerId: 1});
+ expect(onChangeEnd).toHaveBeenCalledOnce();
+ expect(onChangeEnd).toHaveBeenCalledWith(70);
+ expect(declaresPressedOverlay(thumb)).toBe(false);
+
+ // The slider answers the next press.
+ fireEvent.pointerDown(track, {clientX: 28, clientY: 10, pointerId: 3});
+ expect(thumb).toHaveAttribute('aria-valuenow', '10');
+ });
+
+ it('ends the drag when its control node is replaced mid-drag and the capture is lost', () => {
+ // A FormLayout direction change re-renders the field in another layout,
+ // replacing the control node while the drag holds it. The browser then
+ // fires lostpointercapture at the document, not at the removed node.
+ const onChangeEnd = vi.fn();
+ function InLayout({direction}: {direction: 'vertical' | 'horizontal'}) {
+ return (
+ <FormLayout direction={direction}>
+ <ControlledSlider id="Volume" onChangeEnd={onChangeEnd} />
+ </FormLayout>
+ );
+ }
+ const {rerender} = render(<InLayout direction="vertical" />);
+ const before = trackOf(screen.getByRole('slider'));
+ fireEvent.pointerDown(before, {clientX: 136, clientY: 10, pointerId: 1});
+
+ rerender(<InLayout direction="horizontal" />);
+ fireEvent.lostPointerCapture(document, {pointerId: 1});
+ expect(onChangeEnd).toHaveBeenCalledOnce();
+
+ // The slider answers the next press on whichever node it renders now.
+ const thumb = screen.getByRole('slider');
+ const after = trackOf(thumb);
+ fireEvent.pointerDown(after, {clientX: 28, clientY: 10, pointerId: 3});
+ expect(thumb).toHaveAttribute('aria-valuenow', '10');
+ });
+
+ it('lets two fingers drag two sliders at once', () => {
+ render(
+ <>
+ <ControlledSlider id="Treble" />
+ <ControlledSlider id="Bass" />
+ </>,
+ );
+ const treble = screen.getByRole('slider', {name: 'Treble'});
+ const bass = screen.getByRole('slider', {name: 'Bass'});
+ const trebleTrack = trackOf(treble);
+ const bassTrack = trackOf(bass);
+
+ fireEvent.pointerDown(trebleTrack, {
+ clientX: 100,
+ clientY: 10,
+ pointerId: 1,
+ });
+ fireEvent.pointerDown(bassTrack, {clientX: 100, clientY: 10, pointerId: 2});
+ fireEvent.pointerMove(trebleTrack, {
+ clientX: 154,
+ clientY: 10,
+ pointerId: 1,
+ });
+ fireEvent.pointerMove(bassTrack, {clientX: 28, clientY: 10, pointerId: 2});
+
+ expect(treble).toHaveAttribute('aria-valuenow', '80');
+ expect(bass).toHaveAttribute('aria-valuenow', '10');
+ });
+});
+
describe('pressed state', () => {
it('paints the pressed overlay on the thumb for as long as it is dragged, from wherever the press lands', () => {
render(
diff --git a/packages/core/src/Slider/Slider.tsx b/packages/core/src/Slider/Slider.tsx
index 65c4a94b71..aad4b7d6fc 100644
--- a/packages/core/src/Slider/Slider.tsx
+++ b/packages/core/src/Slider/Slider.tsx
@@ -4,8 +4,8 @@
/**
* @file Slider.tsx
- * @input Uses React, useId, useRef, useCallback, Field, Tooltip, useTooltip, VisuallyHidden
- * @output Exports Slider and its props; unfilled marks share the track token; modifier-only key presses do not restore the thumb focus ring
+ * @input Uses React, useId, useRef, useCallback, useEffect, Field, Tooltip, useTooltip, VisuallyHidden
+ * @output Exports Slider and its props; unfilled marks share the track token; modifier-only key presses do not restore the thumb focus ring; a drag belongs to the pointer that started it
* @position Core implementation; consumed by index.ts, tested by Slider.test.tsx
*
* SYNC: When modified, update these files to stay in sync:
@@ -17,6 +17,7 @@
*/
import {
+ useEffect,
useId,
useMemo,
useRef,
@@ -736,12 +737,22 @@ export function Slider({ref, ...props}: SliderProps) {
);
// Pointer handlers
+ // The pointer that owns the drag in progress. A control tracks one touch
+ // (UIKit's `isMultipleTouchEnabled` is false by default): another finger
+ // landing on the slider, moving or lifting never takes over or ends the
+ // drag the first one owns.
+ const dragPointerRef = useRef<number | null>(null);
+
const handlePointerDown = useCallback(
(e: PointerEvent<HTMLDivElement>) => {
if (isDisabled) {
return;
}
e.preventDefault();
+ if (draggingThumbRef.current !== null) {
+ return;
+ }
+ dragPointerRef.current = e.pointerId;
// If the click originated from a mark element, snap to that mark's value
// instead of calculating from pointer position (avoids off-by-one when
@@ -787,7 +798,11 @@ export function Slider({ref, ...props}: SliderProps) {
const handlePointerMove = useCallback(
(e: PointerEvent<HTMLDivElement>) => {
- if (draggingThumbRef.current === null || isDisabled) {
+ if (
+ draggingThumbRef.current === null ||
+ e.pointerId !== dragPointerRef.current ||
+ isDisabled
+ ) {
return;
}
const newVal = getValueFromPosition(e.clientX, e.clientY);
@@ -796,10 +811,14 @@ export function Slider({ref, ...props}: SliderProps) {
[isDisabled, getValueFromPosition, updateValue],
);
- const handlePointerUp = useCallback(
- (_e: PointerEvent<HTMLDivElement>) => {
- if (draggingThumbRef.current !== null) {
+ const endDrag = useCallback(
+ (pointerId: number) => {
+ if (
+ draggingThumbRef.current !== null &&
+ pointerId === dragPointerRef.current
+ ) {
draggingThumbRef.current = null;
+ dragPointerRef.current = null;
setDraggingThumb(null);
fireChangeEnd();
}
@@ -807,6 +826,28 @@ export function Slider({ref, ...props}: SliderProps) {
[fireChangeEnd],
);
+ const handlePointerUp = useCallback(
+ (e: PointerEvent<HTMLDivElement>) => endDrag(e.pointerId),
+ [endDrag],
+ );
+
+ // A capture lost without an up or a cancel (the browser releases it, or
+ // script does) still ends the drag, so the slider never stays held by a
+ // pointer that is gone and keeps answering new presses. The listener sits
+ // on the document, where lostpointercapture bubbles to and where the browser
+ // fires it when the captured node is removed, so a control node replaced
+ // before or during a drag (a FormLayout direction change) is still covered.
+ useEffect(() => {
+ const doc = trackRef.current?.ownerDocument;
+ if (doc == null) {
+ return;
+ }
+ const onLostCapture = (event: globalThis.PointerEvent) =>
+ endDrag(event.pointerId);
+ doc.addEventListener('lostpointercapture', onLostCapture);
+ return () => doc.removeEventListener('lostpointercapture', onLostCapture);
+ }, [endDrag]);
+
// Keyboard handler
const handleKeyDown = useCallback(
(thumbIndex: number, e: KeyboardEvent<HTMLDivElement>) => {