Skip to content

Commit d1e3f62

Browse files
committed
fix(BottomSheet): a finger's pull on the sheet's content is one touch drag from its touch-down, so a short flick closes the sheet
A finger raises a pointer event before each touch event. The content's pointer handlers, which serve a mouse or a pen, armed for the finger too and began the drag first, at the same 8 px as the touch listeners. That drag took pointer capture for the content element. Moving the capture off the element under the finger raises lostpointercapture, which bubbles through the content and cancelled the drag one move later. The touch listeners then began the drag again with a new velocity tracker. In Chromium, whose pointer id is not the touch identifier, the touch listener found no touch for the pointer's drag, kept the move, and the content's own pan could start under the drag. The release speed counted only the moves after the drag began. A fast flick on the content that the page received as two or three moves read as no speed and sprang back, where the same flick on the grab handle closed the sheet. The content's touch-down now drops the pointer handlers' arm, so the touch listeners alone drag for a finger. The contact's positions are tracked from its touch-down, or its mouse press, and handed to the drag when it begins, as the grab handle's drag has them. No release threshold changes.
1 parent 5311a85 commit d1e3f62

4 files changed

Lines changed: 387 additions & 6 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: a finger's pull down on the sheet's content is handled by the sheet's touch listeners alone, and it releases at the speed of the whole pull. Before, the content's pointer handlers, which serve a mouse or a pen, began the same drag first and took pointer capture, and the capture change cancelled that drag one move later. The touch listeners then began the drag again, or, in Chromium, the content's own pan could start under it. The release speed counted only the moves after the drag began, so a fast flick on the content that the page received as two or three moves read as no speed and sprang back, where the same flick on the grab handle closed the sheet. A flick on the content now closes the sheet as a flick on the grab handle does. This also applies to the sheets that Tokenizer and PowerSearch open. A mouse or pen pull from the content also releases at the speed of the whole pull. No release threshold changes.
6+
7+
@imdreamrunner
Lines changed: 218 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,218 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file BottomSheetBodyPull.interaction.browser.spec.ts
5+
* @input The built "Hug height" and "Tall Sheet" BottomSheet stories, real
6+
* touch input (Chromium CDP / WebKit's input command) and the interaction
7+
* harness
8+
* @output Interaction proofs for a finger's pull down on a sheet's content.
9+
* The element under the finger keeps the pointer's capture until the lift.
10+
* The sheet never steps back toward rest while the finger moves down. A
11+
* pull that starts past the drag's slop follows the finger. A fast flick of
12+
* three moves closes the sheet.
13+
* @position Real-engine proof for the body paths of useSheetGestures. An
14+
* engine orders a finger's pointer and touch events, and moves the
15+
* pointer's implicit capture, itself. jsdom shows neither.
16+
*/
17+
18+
import {
19+
expect,
20+
test,
21+
type Locator,
22+
type Page,
23+
} from '@astryxdesign/a11y-spec/playwright';
24+
import {
25+
frames,
26+
openStory,
27+
path,
28+
toTouchEvents,
29+
touchDriver,
30+
waitForRest,
31+
withStorybook,
32+
type TouchDriver,
33+
type TouchScript,
34+
} from '@astryxdesign/interaction-harness';
35+
36+
const storybook = withStorybook(test);
37+
38+
// How far the finger pulls down, px. Under the distance that closes the sheet.
39+
const PULL_PX = 120;
40+
41+
// One entry per pointer-capture change and pointer lift, in order: the event
42+
// and whether its target is the sheet's scrolling body or its content.
43+
type CaptureLog = ReadonlyArray<string>;
44+
45+
interface OpenSheet {
46+
readonly driver: TouchDriver;
47+
readonly dialog: Locator;
48+
readonly sheet: Locator;
49+
}
50+
51+
async function openSheet(
52+
page: Page,
53+
story: string,
54+
triggerName: string,
55+
sheetName: string,
56+
): Promise<OpenSheet> {
57+
await openStory(page, storybook(), story);
58+
const driver = await touchDriver(page);
59+
const trigger = await page
60+
.getByRole('button', {name: triggerName})
61+
.boundingBox();
62+
if (trigger == null) {
63+
throw new Error('no trigger');
64+
}
65+
await driver.tap({
66+
x: trigger.x + trigger.width / 2,
67+
y: trigger.y + trigger.height / 2,
68+
});
69+
const dialog = page.getByRole('dialog', {name: sheetName});
70+
const sheet = dialog.locator('.astryx-bottom-sheet').first();
71+
await expect(sheet).toBeVisible();
72+
await waitForRest(sheet);
73+
return {driver, dialog, sheet};
74+
}
75+
76+
/** A sheet whose content fits, with a log of its pointer-capture changes. */
77+
async function openFittingSheet(page: Page) {
78+
const {driver, dialog, sheet} = await openSheet(
79+
page,
80+
'core-bottomsheet--hug-height',
81+
'Share page',
82+
'Share page',
83+
);
84+
const heading = await dialog
85+
.getByRole('heading', {name: 'Share page'})
86+
.boundingBox();
87+
if (heading == null) {
88+
throw new Error('no heading');
89+
}
90+
await sheet.evaluate(panel => {
91+
const log: string[] = [];
92+
(window as unknown as {captureLog: string[]}).captureLog = log;
93+
const body = panel.children[1];
94+
for (const type of [
95+
'gotpointercapture',
96+
'lostpointercapture',
97+
'pointerup',
98+
]) {
99+
window.addEventListener(
100+
type,
101+
event => {
102+
log.push(`${type}:${event.target === body ? 'body' : 'content'}`);
103+
},
104+
{capture: true},
105+
);
106+
}
107+
});
108+
return {
109+
driver,
110+
sheet,
111+
start: {
112+
x: Math.round(heading.x + heading.width / 2),
113+
y: Math.round(heading.y + heading.height / 2),
114+
},
115+
};
116+
}
117+
118+
const sheetTop = async (sheet: Locator) =>
119+
sheet.evaluate(element => element.getBoundingClientRect().y);
120+
121+
const captureLog = async (page: Page): Promise<CaptureLog> =>
122+
page.evaluate(() => (window as unknown as {captureLog: string[]}).captureLog);
123+
124+
for (const pull of [
125+
// The first moves stay under the drag's 8 px slop, as a finger's do.
126+
{name: 'a slow start', stepPx: 3},
127+
// The first move passes the slop.
128+
{name: 'a start past the slop', stepPx: 15},
129+
]) {
130+
test(`a pull down on the content with ${pull.name} keeps the pointer's capture under the finger and never steps the sheet back`, async ({
131+
page,
132+
}) => {
133+
const {driver, sheet, start} = await openFittingSheet(page);
134+
const restTop = await sheetTop(sheet);
135+
const steps = PULL_PX / pull.stepPx;
136+
const script = path(
137+
progress => ({
138+
x: start.x,
139+
y: start.y + Math.round(progress * steps) * pull.stepPx,
140+
}),
141+
{durationMs: steps * (1000 / 60), hz: 60, restMs: 150},
142+
);
143+
const lastMove =
144+
toTouchEvents(script).filter(step => step.type === 'touchMove').length -
145+
1;
146+
const tops: number[] = [];
147+
let moves = 0;
148+
await driver.play(script, {
149+
releaseUnread: true,
150+
onEvent: async step => {
151+
if (step.type !== 'touchMove') {
152+
return;
153+
}
154+
tops.push(await sheetTop(sheet));
155+
// The finger rests 150 ms before the lift: read the last move again
156+
// once the page has presented it.
157+
if (moves++ === lastMove) {
158+
await frames(page, 1);
159+
tops.push(await sheetTop(sheet));
160+
}
161+
},
162+
});
163+
164+
// Under a finger that only moves down, the sheet never moves up.
165+
const stepsBack = tops.filter(
166+
(top, index) => index > 0 && top < tops[index - 1] - 0.5,
167+
);
168+
expect(stepsBack).toEqual([]);
169+
if (pull.stepPx > 8) {
170+
expect(Math.max(...tops) - restTop).toBeCloseTo(PULL_PX, 0);
171+
}
172+
173+
// The sheet's body takes no capture, so the element under the finger
174+
// loses the pointer's implicit capture only at the lift.
175+
const log = await captureLog(page);
176+
expect(log.filter(entry => entry === 'gotpointercapture:body')).toEqual([]);
177+
const lift = log.findIndex(entry => entry.startsWith('pointerup'));
178+
const lossBeforeLift = log.filter(
179+
(entry, index) =>
180+
entry.startsWith('lostpointercapture') && (lift === -1 || index < lift),
181+
);
182+
expect(lossBeforeLift).toEqual([]);
183+
});
184+
}
185+
186+
test('a fast flick of three moves on the content closes the sheet', async ({
187+
page,
188+
browserName,
189+
}) => {
190+
test.skip(
191+
browserName !== 'chromium',
192+
'WebKit stamps a touch when it arrives, so a three-move flick has no fixed speed there',
193+
);
194+
const {driver, dialog, sheet} = await openSheet(
195+
page,
196+
'core-bottomsheet--tall-sheet',
197+
'Open nearby places',
198+
'Nearby places',
199+
);
200+
const box = await sheet.boundingBox();
201+
if (box == null) {
202+
throw new Error('no sheet box');
203+
}
204+
// A point on the list, which scrolls and rests at its top.
205+
const x = Math.round(box.x + box.width / 2);
206+
const y = Math.round(box.y + 180);
207+
// 150 px in 48 ms. The last two intervals run at 3,750 and 3,125 px/s, far
208+
// past the speed a release is a flick from.
209+
const flick: TouchScript = [
210+
{t: 0, fingers: [{id: 0, x, y}]},
211+
{t: 16, fingers: [{id: 0, x, y: y + 40}]},
212+
{t: 32, fingers: [{id: 0, x, y: y + 90}]},
213+
{t: 48, fingers: [{id: 0, x, y: y + 150}]},
214+
{t: 52, fingers: []},
215+
];
216+
await driver.play(flick);
217+
await expect(dialog).toBeHidden();
218+
});

‎packages/core/src/BottomSheet/useSheetGestures.test.ts‎

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,20 @@ describe('useSheetGestures', () => {
12771277
expect(hook.result.current.isDragging).toBe(true);
12781278
});
12791279

1280+
it('releases a quick mouse pull from the content at the speed of the whole pull', () => {
1281+
const {hook, onDismiss} = setup();
1282+
const body = makeBody(0);
1283+
bodyDown(hook, 0, 0, body);
1284+
bodyMove(hook, 40, 16, body);
1285+
bodyMove(hook, 100, 32, body);
1286+
act(() =>
1287+
hook.result.current.bodyProps.onPointerUp(pointerEvent(100, 33, body)),
1288+
);
1289+
// 0.2 x 3,750 + 0.8 x 2,500 = 2,750 px/s coasts past the dismiss line,
1290+
// as the same pull does on the grab handle.
1291+
expect(onDismiss).toHaveBeenCalledTimes(1);
1292+
});
1293+
12801294
it('does not hijack scrolling when the body is scrolled down', () => {
12811295
const {hook} = setup({snapHeights: () => [200]});
12821296
const body = makeBody(120); // scrolled, not at the top
@@ -1536,6 +1550,122 @@ describe('useSheetGestures', () => {
15361550
expect(onSnap).toHaveBeenLastCalledWith(200);
15371551
});
15381552

1553+
// A finger raises a pointer event before each of its touch events. The
1554+
// pointer's id is the touch's identifier in WebKit and another number in
1555+
// Chromium.
1556+
function finger(hook: Hook, el: HTMLElement, pointerId: number, id = 7) {
1557+
const pointer = (y: number, t: number) =>
1558+
pointerEvent(y, t, el, pointerId, 0, true, 'touch');
1559+
return {
1560+
down(y: number, t: number) {
1561+
act(() => {
1562+
hook.result.current.bodyProps.onPointerDown(pointer(y, t));
1563+
touch(el, 'touchstart', y, id, 'direct', t);
1564+
});
1565+
},
1566+
move(y: number, t: number): Event {
1567+
let moved!: Event;
1568+
act(() => {
1569+
hook.result.current.bodyProps.onPointerMove(pointer(y, t));
1570+
moved = touch(el, 'touchmove', y, id, 'direct', t);
1571+
});
1572+
return moved;
1573+
},
1574+
up(y: number, t: number) {
1575+
act(() => {
1576+
hook.result.current.bodyProps.onPointerUp(pointer(y, t));
1577+
touch(el, 'touchend', y, id, 'direct', t);
1578+
});
1579+
},
1580+
};
1581+
}
1582+
1583+
it("keeps a finger's pull out of the pointer path, so a pointer capture change cannot cancel it", () => {
1584+
const {hook} = setup();
1585+
const el = makeScroller({
1586+
scrollTop: 0,
1587+
clientHeight: 200,
1588+
scrollHeight: 800,
1589+
});
1590+
el.setPointerCapture = vi.fn();
1591+
act(() => hook.result.current.sheetRef(el));
1592+
act(() => hook.result.current.bodyProps.ref(el));
1593+
const pull = finger(hook, el, 7);
1594+
pull.down(0, 0);
1595+
pull.move(12, 16);
1596+
expect(hook.result.current.isDragging).toBe(true);
1597+
expect(el.setPointerCapture).not.toHaveBeenCalled();
1598+
// The capture a finger's pointer has by default ends on the element
1599+
// under the finger. Its event bubbles through the body.
1600+
act(() => {
1601+
hook.result.current.bodyProps.onLostPointerCapture(
1602+
pointerEvent(12, 16, el, 7),
1603+
);
1604+
});
1605+
expect(hook.result.current.isDragging).toBe(true);
1606+
expect(liveOffset(hook)).toBe(12);
1607+
pull.move(60, 32);
1608+
expect(liveOffset(hook)).toBe(60);
1609+
});
1610+
1611+
it("cancels the native pan of a finger's pull whose pointer id is not its touch id", () => {
1612+
const {hook} = setup();
1613+
const el = makeScroller({
1614+
scrollTop: 0,
1615+
clientHeight: 200,
1616+
scrollHeight: 800,
1617+
});
1618+
act(() => hook.result.current.sheetRef(el));
1619+
act(() => hook.result.current.bodyProps.ref(el));
1620+
const pull = finger(hook, el, 3, 0);
1621+
pull.down(0, 0);
1622+
const first = pull.move(12, 16);
1623+
expect(first.defaultPrevented).toBe(true);
1624+
expect(hook.result.current.isDragging).toBe(true);
1625+
const second = pull.move(60, 32);
1626+
expect(second.defaultPrevented).toBe(true);
1627+
expect(liveOffset(hook)).toBe(60);
1628+
});
1629+
1630+
it('closes on a fast content flick of two moves, counted from the touch-down', () => {
1631+
const {hook, onDismiss} = setup();
1632+
const el = makeScroller({
1633+
scrollTop: 0,
1634+
clientHeight: 200,
1635+
scrollHeight: 800,
1636+
});
1637+
act(() => hook.result.current.sheetRef(el));
1638+
act(() => hook.result.current.bodyProps.ref(el));
1639+
// 30 px then 60 px in 16 ms each: 0.2 x 3,750 + 0.8 x 1,875 = 2,250 px/s.
1640+
act(() => {
1641+
touch(el, 'touchstart', 0, 1, 'direct', 0);
1642+
touch(el, 'touchmove', 30, 1, 'direct', 16);
1643+
touch(el, 'touchmove', 90, 1, 'direct', 32);
1644+
touch(el, 'touchend', 90, 1, 'direct', 33);
1645+
});
1646+
expect(onDismiss).toHaveBeenCalledTimes(1);
1647+
});
1648+
1649+
it('springs back a slow content pull of two moves that stops short of halfway', () => {
1650+
const {hook, onDismiss} = setup();
1651+
const el = makeScroller({
1652+
scrollTop: 0,
1653+
clientHeight: 200,
1654+
scrollHeight: 800,
1655+
});
1656+
act(() => hook.result.current.sheetRef(el));
1657+
act(() => hook.result.current.bodyProps.ref(el));
1658+
// 100 px/s: under the speed a release is projected from.
1659+
act(() => {
1660+
touch(el, 'touchstart', 0, 1, 'direct', 0);
1661+
touch(el, 'touchmove', 30, 1, 'direct', 300);
1662+
touch(el, 'touchmove', 90, 1, 'direct', 900);
1663+
touch(el, 'touchend', 90, 1, 'direct', 910);
1664+
});
1665+
expect(onDismiss).not.toHaveBeenCalled();
1666+
expect(hook.result.current.settledOffset).toBe(0);
1667+
});
1668+
15391669
it('stretches the sheet with a pull up past a list already at its end, at the tallest detent, without claiming the touch', () => {
15401670
const {hook} = setup({
15411671
snapHeights: () => [200],

0 commit comments

Comments
 (0)