Skip to content

Commit ffa0821

Browse files
committed
fix(core): only a touch drag's release cross-fades into a snap point under Reduce Motion
design:reduced-motion DR2 cross-fades a transition the release of a touch drag starts; DR3 makes any other pointer's change immediate. The public pan now says which pointer drove a drag, so createDragToSnapPoints plays its release as a touch release only for 'touch': a mouse or pen drag's release, on a fine primary pointer under Reduce Motion, lands at once. The new test drives the release through a stand-in pan with each pointer type; the mouse and pen cases fail without the change.
1 parent 5a19f8b commit ffa0821

4 files changed

Lines changed: 166 additions & 8 deletions

File tree

‎packages/core/src/dragToSnapPoints/createDragToSnapPoints.doc.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export const docs = {
102102
category: 'Content',
103103
criterion: '2.3.3 Animation from Interactions',
104104
description:
105-
'When the person asks for reduced motion, the settle after a drag becomes a short cross-fade into the snap point, and `settleTo` lands at once with a mouse or fades on a touch screen. While the finger is down, the element still follows it.',
105+
'When the person asks for reduced motion, the settle after a touch drag becomes a short cross-fade into the snap point, and after a mouse or pen drag it lands at once. `settleTo` lands at once with a mouse and fades on a touch screen. While the finger is down, the element still follows it.',
106106
},
107107
],
108108
},
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file createDragToSnapPoints.reducedMotion.test.ts
5+
* @input vitest (jsdom, fake clock), the snap-point drag with the public pan
6+
* replaced by a stand-in that hands its pan events to the test, Reduce
7+
* Motion on with a fine primary pointer, a recording stand-in for
8+
* Element.animate
9+
* @output Checks `design:reduced-motion` DR2 and DR3 for the snap-point
10+
* drag's release (`spec:AST-107` FR2): a touch drag's release cross-fades
11+
* into the snap point, a mouse or pen drag's release lands at once, and
12+
* `settleTo` lands at once on a fine pointer
13+
* @position Testing; validates createDragToSnapPoints.ts's release plan
14+
*/
15+
16+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
17+
import type {
18+
PanGestureEvent,
19+
PanGestureOptions,
20+
} from '../gestures/createPanGesture';
21+
22+
let onPan: ((event: PanGestureEvent) => void) | null = null;
23+
24+
vi.mock('../gestures/createPanGesture', () => ({
25+
createPanGesture: (_element: Element, options: PanGestureOptions) => {
26+
onPan = options.onPan;
27+
return {destroy: () => {}};
28+
},
29+
}));
30+
31+
const {createDragToSnapPoints} = await import('./createDragToSnapPoints');
32+
33+
interface Recorded {
34+
keyframes: Keyframe[];
35+
}
36+
37+
const recorded: Recorded[] = [];
38+
const originalMatchMedia = window.matchMedia;
39+
40+
beforeEach(() => {
41+
vi.useFakeTimers({
42+
toFake: [
43+
'setTimeout',
44+
'clearTimeout',
45+
'performance',
46+
'requestAnimationFrame',
47+
'cancelAnimationFrame',
48+
],
49+
});
50+
// Reduce Motion on, with a fine primary pointer.
51+
window.matchMedia = (query: string) =>
52+
({
53+
matches: query.includes('prefers-reduced-motion: reduce'),
54+
media: query,
55+
onchange: null,
56+
addEventListener: () => {},
57+
removeEventListener: () => {},
58+
addListener: () => {},
59+
removeListener: () => {},
60+
dispatchEvent: () => false,
61+
}) as MediaQueryList;
62+
HTMLElement.prototype.animate = function (keyframes: Keyframe[]) {
63+
let start: number | null = null;
64+
const animation = {
65+
get startTime() {
66+
return start;
67+
},
68+
set startTime(value: number | null) {
69+
start = value;
70+
},
71+
playState: 'running',
72+
ready: new Promise(() => {}),
73+
finish() {},
74+
cancel() {},
75+
};
76+
requestAnimationFrame(() => {
77+
start ??= performance.now();
78+
});
79+
recorded.push({keyframes: Array.isArray(keyframes) ? keyframes : []});
80+
return animation as unknown as Animation;
81+
};
82+
});
83+
84+
afterEach(() => {
85+
window.matchMedia = originalMatchMedia;
86+
Reflect.deleteProperty(HTMLElement.prototype, 'animate');
87+
vi.useRealTimers();
88+
recorded.length = 0;
89+
onPan = null;
90+
document.body.replaceChildren();
91+
});
92+
93+
function drawer() {
94+
const element = document.createElement('div');
95+
document.body.append(element);
96+
const onSettle = vi.fn();
97+
const snap = createDragToSnapPoints(element, {
98+
axis: 'y',
99+
snapPoints: [0, 240, 480],
100+
onSettle,
101+
});
102+
return {element, onSettle, snap};
103+
}
104+
105+
/** A drag down by `dy`, released at `vy` px/s, driven by `pointerType`. */
106+
function release(
107+
dy: number,
108+
vy: number,
109+
pointerType: PanGestureEvent['pointerType'],
110+
): void {
111+
const at = (state: PanGestureEvent['state'], y: number, v: number) =>
112+
onPan?.({
113+
state,
114+
translation: {x: 0, y},
115+
velocity: {x: 0, y: v},
116+
pointerType,
117+
});
118+
at('began', 0, 0);
119+
at('changed', dy, 0);
120+
at('ended', dy, vy);
121+
}
122+
123+
const fades = () =>
124+
recorded.some(({keyframes}) =>
125+
keyframes.some(frame => frame.opacity !== undefined),
126+
);
127+
128+
describe('the release under Reduce Motion on a fine pointer (DR2, DR3)', () => {
129+
it('a touch drag\u2019s release cross-fades into the snap point', async () => {
130+
const {onSettle} = drawer();
131+
release(300, 0, 'touch');
132+
expect(fades()).toBe(true);
133+
await vi.advanceTimersByTimeAsync(1000);
134+
expect(onSettle).toHaveBeenCalledWith(1);
135+
});
136+
137+
it.each(['mouse', 'pen'] as const)(
138+
'a %s drag\u2019s release lands at once',
139+
async pointerType => {
140+
const {onSettle} = drawer();
141+
release(300, 0, pointerType);
142+
expect(fades()).toBe(false);
143+
await vi.advanceTimersByTimeAsync(0);
144+
expect(onSettle).toHaveBeenCalledWith(1);
145+
},
146+
);
147+
148+
it('settleTo lands at once', async () => {
149+
const {onSettle, snap} = drawer();
150+
snap.settleTo(2);
151+
expect(fades()).toBe(false);
152+
await vi.advanceTimersByTimeAsync(0);
153+
expect(onSettle).toHaveBeenCalledWith(2);
154+
});
155+
});

‎packages/core/src/dragToSnapPoints/createDragToSnapPoints.ts‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
* `flickThreshold` carries to the next one in its direction, and a release
2020
* moving back toward where the drag started returns to the snap point it
2121
* started from (`spec:AST-094` FR37). The settle plays the `springs.settle`
22-
* motion role from the release velocity (FR38); under Reduce Motion it is the
23-
* motion module's plan for a touch drag's release (`design:reduced-motion`
24-
* DR2). `settleTo(index)` settles without a drag: the keyboard path.
22+
* motion role from the release velocity (FR38); under Reduce Motion a touch
23+
* drag's release cross-fades and a mouse or pen drag's lands at once
24+
* (`design:reduced-motion` DR2, DR3). `settleTo(index)` settles without a drag: the keyboard path.
2525
* Importing this module does no DOM work.
2626
*
2727
* SYNC: When modified, update:
@@ -205,7 +205,7 @@ export function createDragToSnapPoints(
205205

206206
const pan = createPanGesture(element, {
207207
axis,
208-
onPan: ({state, translation, velocity}: PanGestureEvent) => {
208+
onPan: ({state, translation, velocity, pointerType}: PanGestureEvent) => {
209209
if (destroyed) {
210210
return;
211211
}
@@ -225,8 +225,11 @@ export function createDragToSnapPoints(
225225
}
226226
const {startIndex} = drag;
227227
drag = null;
228+
// Only a touch drag's release cross-fades under Reduce Motion; a mouse
229+
// or pen drag's lands at once (design:reduced-motion DR2, DR3).
230+
const touchRelease = pointerType === 'touch';
228231
if (state === 'cancelled') {
229-
settle(startIndex, 0, true);
232+
settle(startIndex, 0, touchRelease);
230233
return;
231234
}
232235
const release = velocity[axis];
@@ -239,7 +242,7 @@ export function createDragToSnapPoints(
239242
flickThreshold,
240243
}),
241244
release,
242-
true,
245+
touchRelease,
243246
);
244247
},
245248
});

‎packages/core/src/hooks/useDragToSnapPoints.doc.mjs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ export const docs = {
9090
category: 'Content',
9191
criterion: '2.3.3 Animation from Interactions',
9292
description:
93-
'When the person asks for reduced motion, the settle after a drag becomes a short cross-fade into the snap point, and `settleTo` lands at once with a mouse or fades on a touch screen. While the finger is down, the element still follows it.',
93+
'When the person asks for reduced motion, the settle after a touch drag becomes a short cross-fade into the snap point, and after a mouse or pen drag it lands at once. `settleTo` lands at once with a mouse and fades on a touch screen. While the finger is down, the element still follows it.',
9494
},
9595
],
9696
},

0 commit comments

Comments
 (0)