Skip to content

Commit 71afa62

Browse files
committed
refactor(core): BottomSheet's drag runs on the gesture recognizers
The sheet's input moves from its own pointer and touch listeners onto the shared gesture host: - the handle's pan: vertical, one touch, any pointer, no text selection; - the body's pans: a pull down from the content's top edge (`scrollEdge`, any pointer), and for a finger or pen a push up at the content's end that also takes over a scroll that runs out mid-gesture (`scrollEdge` and `scrollHandoff`), only below the tallest detent. At the edge both claim the touch early (5 px), under WebKit's own slow-drag claim, so a slow pull never becomes the content's overscroll; - a touch-down watcher (a long press of no duration that never claims) that catches a settling sheet where it is drawn; - the host's presented hit-testing replaces the dialog's capture listeners, and its blur and visibility handling the sheet's own. The release velocity is the pan's (spec:AST-100's filter): under a mouse it is the same blend as a finger's instead of the last 100 ms, and a pointer held still for more than 40 ms reads 0. Pans begin only while the host counts one finger on the screen (two are the browser's pinch); a drag that has begun keeps its finger when another lands (spec:AST-094 FR10). The pan reports UIKit's translation, which leaves out the 10 px before it begins. The scroll hand-off (`scrollEdge` with `scrollHandoff`) is today's. The tests drive the hook and the component through the host with real touch and pointer events on explicit timestamps.
1 parent 6dbfa9c commit 71afa62

7 files changed

Lines changed: 1429 additions & 1764 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@astryxdesign/core': patch
3+
---
4+
5+
[fix] BottomSheet's drag runs on the shared gesture recognizers. A drag begins after 10 px of travel and moves the sheet by the travel past that, as UIKit's does. A mouse's release velocity is now the same filter as a finger's (it was the speed over the last 100 ms), and a pointer held still for more than 40 ms before the release throws nothing. A second finger that lands during a drag no longer sends the sheet back to its detent.
6+
7+
@vjeux

‎packages/core/src/BottomSheet/BottomSheet.test.tsx‎

Lines changed: 109 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,96 @@ function getHandle(): HTMLElement {
218218
return handle;
219219
}
220220

221-
// Drive a pointer drag on the grab handle. jsdom PointerEvents don't carry
222-
// clientY, so dispatch plain events with the coords the handlers read.
223-
function drag(handle: HTMLElement, points: {y: number}[]) {
224-
const [down, ...rest] = points;
225-
fireEvent.pointerDown(handle, {
221+
// Input reaches the sheet through the document's gesture host, as in a
222+
// browser: a mouse (the stubbed primary pointer is fine) on explicit
223+
// timestamps. The host outlives a test and its clock never runs backwards,
224+
// so every test's times start past the previous test's.
225+
let timeBase = 0;
226+
beforeEach(() => {
227+
timeBase += 1_000_000;
228+
pendingPress = null;
229+
// A test that ends mid-drag leaves its pointer down in the host: losing
230+
// window focus cancels every contact in flight.
231+
window.dispatchEvent(new Event('blur'));
232+
// jsdom has no hit test; the host asks for one only on iOS WebKit, while
233+
// the animator moves an element. Null keeps the touch where it was sent.
234+
Object.defineProperty(document, 'elementFromPoint', {
235+
configurable: true,
236+
value: () => null,
237+
});
238+
});
239+
240+
function mouse(
241+
target: Element,
242+
type: 'pointerdown' | 'pointermove' | 'pointerup',
243+
y: number,
244+
time: number,
245+
): boolean {
246+
const event = new PointerEvent(type, {
247+
bubbles: true,
248+
cancelable: true,
249+
composed: true,
226250
pointerId: 1,
227-
clientY: down.y,
228-
button: 0,
251+
pointerType: 'mouse',
229252
isPrimary: true,
253+
clientX: 50,
254+
clientY: y,
255+
button: type === 'pointermove' ? -1 : 0,
256+
buttons: type === 'pointerup' ? 0 : 1,
230257
});
231-
for (const p of rest) {
232-
fireEvent.pointerMove(handle, {pointerId: 1, clientY: p.y});
258+
Object.defineProperty(event, 'timeStamp', {value: timeBase + time});
259+
return fireEvent(target, event);
260+
}
261+
262+
// A finger where a test stubs a coarse primary pointer, else a mouse. jsdom
263+
// lacks TouchEvent: the touch events carry the fields the host reads.
264+
function contact(
265+
target: Element,
266+
type: 'pointerdown' | 'pointermove' | 'pointerup',
267+
y: number,
268+
time: number,
269+
): boolean {
270+
if (!matchMedia('(pointer: coarse)').matches) {
271+
return mouse(target, type, y, time);
233272
}
234-
const last = points[points.length - 1];
235-
fireEvent.pointerUp(handle, {pointerId: 1, clientY: last.y});
273+
const touchType = {
274+
pointerdown: 'touchstart',
275+
pointermove: 'touchmove',
276+
pointerup: 'touchend',
277+
}[type];
278+
const event = new Event(touchType, {
279+
bubbles: true,
280+
cancelable: true,
281+
composed: true,
282+
});
283+
const touch = {identifier: 1, clientX: 50, clientY: y, target};
284+
Object.defineProperties(event, {
285+
changedTouches: {value: [touch]},
286+
touches: {value: type === 'pointerup' ? [] : [touch]},
287+
timeStamp: {value: timeBase + time},
288+
});
289+
return fireEvent(target, event);
290+
}
291+
292+
// The handle's pan reports UIKit's translation, which leaves out the travel
293+
// before the pan begins: a press lands that far behind its first move, so the
294+
// positions a test moves to are the drag's (one that never moves lifts where
295+
// it landed).
296+
const PAN_THRESHOLD = 10;
297+
let pendingPress: {y: number; time: number} | null = null;
298+
299+
// Drive a drag on the grab handle, 16 ms between its events.
300+
function drag(handle: HTMLElement, points: {y: number}[]) {
301+
points.forEach((point, i) =>
302+
fireTimedPointer(handle, i === 0 ? 'pointerdown' : 'pointermove', {
303+
time: i * 16,
304+
y: point.y,
305+
}),
306+
);
307+
fireTimedPointer(handle, 'pointerup', {
308+
time: points.length * 16,
309+
y: points[points.length - 1].y,
310+
});
236311
}
237312

238313
// The sheet's translation: its resting transform plus the travel channel
@@ -284,16 +359,22 @@ function fireTimedPointer(
284359
target: Element,
285360
type: 'pointerdown' | 'pointermove' | 'pointerup',
286361
{time, y}: {time: number; y: number},
287-
) {
288-
const event = new Event(type, {bubbles: true, cancelable: true});
289-
Object.defineProperties(event, {
290-
button: {value: 0},
291-
clientY: {value: y},
292-
isPrimary: {value: true},
293-
pointerId: {value: 1},
294-
timeStamp: {value: time},
295-
});
296-
return fireEvent(target, event);
362+
): boolean {
363+
if (type === 'pointerdown') {
364+
pendingPress = {y, time};
365+
return true;
366+
}
367+
if (pendingPress != null) {
368+
const landing = pendingPress;
369+
pendingPress = null;
370+
contact(
371+
target,
372+
'pointerdown',
373+
landing.y - Math.sign(y - landing.y) * PAN_THRESHOLD,
374+
landing.time,
375+
);
376+
}
377+
return contact(target, type, y, time);
297378
}
298379

299380
describe('BottomSheet', () => {
@@ -1077,14 +1158,16 @@ describe('BottomSheet', () => {
10771158
</BottomSheet>,
10781159
);
10791160
measureTallSheet();
1161+
// A sheet takes a finger once its entrance has landed.
1162+
await landSpring();
10801163
const dialog = screen.getByRole('dialog');
10811164
const handle = getHandle();
10821165
fireTimedPointer(handle, 'pointerdown', {time: 0, y: 0});
1166+
fireTimedPointer(handle, 'pointermove', {time: 1000, y: 200});
10831167
// The dim follows the finger with no easing of its own.
10841168
expect(dialog.style.getPropertyValue('--_sheet-scrim-duration')).toBe(
10851169
'0s',
10861170
);
1087-
fireTimedPointer(handle, 'pointermove', {time: 1000, y: 200});
10881171
// Gone is 736px below open (the sheet's visible height): the dim is
10891172
// linear in position to there.
10901173
expect(
@@ -1108,13 +1191,8 @@ describe('BottomSheet', () => {
11081191
const handle = getHandle();
11091192
const dialog = screen.getByRole('dialog');
11101193

1111-
fireEvent.pointerDown(handle, {
1112-
pointerId: 1,
1113-
clientY: 0,
1114-
button: 0,
1115-
isPrimary: true,
1116-
});
1117-
fireEvent.pointerMove(handle, {pointerId: 1, clientY: 300});
1194+
fireTimedPointer(handle, 'pointerdown', {time: 0, y: 0});
1195+
fireTimedPointer(handle, 'pointermove', {time: 16, y: 300});
11181196
expect(translationOf(getSheet())).toBe(300);
11191197

11201198
expect(fireEvent.contextMenu(handle)).toBe(false);
@@ -2226,16 +2304,11 @@ describe('BottomSheet', () => {
22262304
'348px',
22272305
);
22282306

2229-
const pointerDownAllowed = fireEvent.pointerDown(getHandle(), {
2230-
pointerId: 1,
2231-
clientY: 0,
2232-
button: 0,
2233-
isPrimary: true,
2234-
});
2307+
const pointerDownAllowed = mouse(getHandle(), 'pointerdown', 0, 0);
22352308
expect(pointerDownAllowed).toBe(false);
22362309
expect(document.activeElement).toBe(input);
22372310

2238-
fireEvent.pointerMove(getHandle(), {pointerId: 1, clientY: 40});
2311+
mouse(getHandle(), 'pointermove', 40, 16);
22392312

22402313
expect(document.activeElement).toBe(sheet);
22412314
expect(positioner.style.getPropertyValue('--_sheet-keyboard-lift')).toBe(

‎packages/core/src/BottomSheet/BottomSheetSwitcher.test.tsx‎

Lines changed: 78 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,73 @@ import {BottomSheetSwitcher} from './BottomSheetSwitcher';
3434
// dialogs are modal, standing in for the `:modal` pseudo-class jsdom cannot
3535
// match.
3636
const modalDialogs = new WeakSet<HTMLDialogElement>();
37+
38+
// Input reaches a sheet through the document's gesture host, as in a browser:
39+
// a mouse (the stubbed primary pointer is fine) on explicit timestamps. The
40+
// host outlives a test and its clock never runs backwards, so every test's
41+
// times start past the previous test's.
42+
let timeBase = 0;
43+
// The handle's pan reports UIKit's translation, which leaves out the travel
44+
// before the pan begins: a press lands that far behind its first move, so the
45+
// positions a test moves to are the drag's.
46+
const PAN_THRESHOLD = 10;
47+
let pendingPress: {y: number; time: number} | null = null;
48+
beforeEach(() => {
49+
timeBase += 1_000_000;
50+
pendingPress = null;
51+
// A test that ends mid-drag leaves its pointer down in the host: losing
52+
// window focus cancels every contact in flight.
53+
window.dispatchEvent(new Event('blur'));
54+
// jsdom has no hit test; null keeps a touch where it was sent.
55+
Object.defineProperty(document, 'elementFromPoint', {
56+
configurable: true,
57+
value: () => null,
58+
});
59+
});
60+
61+
function mouse(
62+
target: Element,
63+
type: 'pointerdown' | 'pointermove' | 'pointerup',
64+
y: number,
65+
time: number,
66+
): boolean {
67+
const event = new PointerEvent(type, {
68+
bubbles: true,
69+
cancelable: true,
70+
composed: true,
71+
pointerId: 1,
72+
pointerType: 'mouse',
73+
isPrimary: true,
74+
clientX: 50,
75+
clientY: y,
76+
button: type === 'pointermove' ? -1 : 0,
77+
buttons: type === 'pointerup' ? 0 : 1,
78+
});
79+
Object.defineProperty(event, 'timeStamp', {value: timeBase + time});
80+
return fireEvent(target, event);
81+
}
82+
83+
function fireTimedPointer(
84+
target: Element,
85+
type: 'pointerdown' | 'pointermove' | 'pointerup',
86+
{time, y}: {time: number; y: number},
87+
): boolean {
88+
if (type === 'pointerdown') {
89+
pendingPress = {y, time};
90+
return true;
91+
}
92+
if (pendingPress != null) {
93+
const landing = pendingPress;
94+
pendingPress = null;
95+
mouse(
96+
target,
97+
'pointerdown',
98+
landing.y - Math.sign(y - landing.y) * PAN_THRESHOLD,
99+
landing.time,
100+
);
101+
}
102+
return mouse(target, type, y, time);
103+
}
37104
beforeEach(() => {
38105
HTMLDialogElement.prototype.showModal = vi.fn(function (
39106
this: HTMLDialogElement,
@@ -773,18 +840,8 @@ describe('BottomSheetSwitcher', () => {
773840
throw new Error('sheet handle not found');
774841
}
775842

776-
fireEvent.pointerDown(handle, {
777-
pointerId: 1,
778-
clientY: 100,
779-
timeStamp: 0,
780-
button: 0,
781-
isPrimary: true,
782-
});
783-
fireEvent.pointerMove(handle, {
784-
pointerId: 1,
785-
clientY: 600,
786-
timeStamp: 16,
787-
});
843+
fireTimedPointer(handle, 'pointerdown', {time: 0, y: 100});
844+
fireTimedPointer(handle, 'pointermove', {time: 16, y: 600});
788845
const detailsDialog = getSheetDialog('details-sheet');
789846
expect(
790847
detailsDialog.style.getPropertyValue('--_sheet-scrim-opacity'),
@@ -796,11 +853,7 @@ describe('BottomSheetSwitcher', () => {
796853
expect(detailsDialog).toHaveStyle({'--_sheet-scrim-opacity': '1'});
797854
expect(getActiveDialog()).toHaveStyle({'--_sheet-scrim-opacity': '0'});
798855

799-
fireEvent.pointerMove(handle, {
800-
pointerId: 1,
801-
clientY: 680,
802-
timeStamp: 32,
803-
});
856+
fireTimedPointer(handle, 'pointermove', {time: 32, y: 680});
804857
expect(detailsDialog).toHaveStyle({'--_sheet-scrim-opacity': '1'});
805858
expect(getActiveDialog()).toHaveStyle({'--_sheet-scrim-opacity': '0'});
806859
});
@@ -970,14 +1023,9 @@ describe('BottomSheetSwitcher', () => {
9701023
}
9711024

9721025
fireEvent.click(dialog);
973-
fireEvent.pointerDown(handle, {
974-
pointerId: 1,
975-
clientY: 0,
976-
button: 0,
977-
isPrimary: true,
978-
});
979-
fireEvent.pointerMove(handle, {pointerId: 1, clientY: 120});
980-
fireEvent.pointerUp(handle, {pointerId: 1, clientY: 120});
1026+
fireTimedPointer(handle, 'pointerdown', {time: 0, y: 0});
1027+
fireTimedPointer(handle, 'pointermove', {time: 16, y: 120});
1028+
fireTimedPointer(handle, 'pointerup', {time: 32, y: 120});
9811029

9821030
expect(onActiveSheetChange).not.toHaveBeenCalled();
9831031
expect(dialog).toHaveStyle({'--_sheet-scrim-opacity': '1'});
@@ -1040,14 +1088,9 @@ describe('BottomSheetSwitcher', () => {
10401088
fireEvent.click(dialog);
10411089
fireEvent.keyDown(dialog, {key: 'Escape'});
10421090
fireEvent(dialog, new Event('cancel', {cancelable: true}));
1043-
fireEvent.pointerDown(handle, {
1044-
pointerId: 1,
1045-
clientY: 0,
1046-
button: 0,
1047-
isPrimary: true,
1048-
});
1049-
fireEvent.pointerMove(handle, {pointerId: 1, clientY: 120});
1050-
fireEvent.pointerUp(handle, {pointerId: 1, clientY: 120});
1091+
fireTimedPointer(handle, 'pointerdown', {time: 0, y: 0});
1092+
fireTimedPointer(handle, 'pointermove', {time: 16, y: 120});
1093+
fireTimedPointer(handle, 'pointerup', {time: 32, y: 120});
10511094

10521095
expect(onActiveSheetChange).not.toHaveBeenCalled();
10531096
expect(dialog).toHaveStyle({'--_sheet-scrim-opacity': '1'});
@@ -1071,13 +1114,8 @@ describe('BottomSheetSwitcher', () => {
10711114
throw new Error('sheet handle not found');
10721115
}
10731116

1074-
fireEvent.pointerDown(handle, {
1075-
pointerId: 1,
1076-
clientY: 0,
1077-
button: 0,
1078-
isPrimary: true,
1079-
});
1080-
fireEvent.pointerMove(handle, {pointerId: 1, clientY: 300});
1117+
fireTimedPointer(handle, 'pointerdown', {time: 0, y: 0});
1118+
fireTimedPointer(handle, 'pointermove', {time: 16, y: 300});
10811119
// The drag moves the sheet on its travel channel (`translate`).
10821120
expect(panel.style.translate).toBe('0px 300px');
10831121

0 commit comments

Comments
 (0)