Skip to content

Commit 4372170

Browse files
committed
fix(core): a long press held through a busy main thread still reports, on the touch events' clock
useLongPress timed a hold with a page timer set at touchstart and cleared by touchend. When the main thread was busy through the hold, the page ran the queued touchend before the overdue timer, and the long press was lost: a Table cell's ContextMenu opened nothing after an 800 ms hold. spec:AST-094 FR16 asks a gesture to keep the event clock: a blocked main thread delays the report and never the moment reported. - The hold is a deadline on the touch events' own timestamps (touchstart.timeStamp + delay). A move, a second finger, or a lift stamped at or past it reports the pending press first, at its start point, then acts. - The page timer waits only for what is left of the hold, so a touch the page handled late does not lengthen it. A timer that fires late yields once to queued input, so a lift stamped before the deadline still cancels the press: the gesture arena's rule (gestures/GestureArena.ts). - The clock is the touch's own window's, (e.view ?? window).performance, captured at touchstart: a same-origin iframe or a Picture-in-Picture window has its own time origin. Epoch stamps (jsdom) come onto it through its time origin, and the timer never waits past the delay. - onTouchEnd and onTouchCancel take the touch event, optionally; ContextMenu passes it. Tests: useLongPress's event-clock cases (six fail on main), among them a touch on a window clock whose origin is later than the page's, and a TableCellMenus browser case whose first long press is held through a 1,200 ms busy main thread (fails on main). A [fix] changeset.
1 parent 12610cb commit 4372170

6 files changed

Lines changed: 398 additions & 21 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] `useLongPress` times a hold on the touch events' own timestamps (`spec:AST-094` FR16). When a busy page handles the lift late, a finger held for the full delay still reports its long press before the lift acts, instead of losing it, so a ContextMenu's long press opens on a loaded page too. A touch the page handled late no longer lengthens the hold, and a lift stamped before the deadline still cancels the press when its timer ran late. `onTouchEnd` and `onTouchCancel` take the touch event, optionally; spreading the handlers passes it.
6+
7+
@vjeux

‎packages/core/src/ContextMenu/ContextMenu.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,14 +712,14 @@ export function ContextMenu({
712712
);
713713
const handleTriggerTouchEnd = useCallback(
714714
(event: React.TouchEvent<HTMLElement>) => {
715-
handleLongPressTouchEnd();
715+
handleLongPressTouchEnd(event);
716716
onTouchEndProp?.(event);
717717
},
718718
[handleLongPressTouchEnd, onTouchEndProp],
719719
);
720720
const handleTriggerTouchCancel = useCallback(
721721
(event: React.TouchEvent<HTMLElement>) => {
722-
handleLongPressTouchCancel();
722+
handleLongPressTouchCancel(event);
723723
onTouchCancelProp?.(event);
724724
},
725725
[handleLongPressTouchCancel, onTouchCancelProp],

‎packages/core/src/Table/__tests__/TableCellMenus.a11y.browser.spec.ts‎

Lines changed: 46 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Copyright (c) Meta Platforms, Inc. and affiliates.
2-
// @pr-a11y scope ^packages/core/src/Table/ ^packages/core/src/ContextMenu/ ^apps/storybook/stories/TableRowExpansion
2+
// @pr-a11y scope ^packages/core/src/Table/ ^packages/core/src/ContextMenu/ ^packages/core/src/hooks/useLongPress ^apps/storybook/stories/TableRowExpansion
33

44
/**
55
* @file TableCellMenus.a11y.browser.spec.ts
@@ -9,7 +9,9 @@
99
* cell), and that the first right-click, keyboard open and long press still
1010
* open it as before: at the pointer, at the trigger's corner, or at the
1111
* finger, focus moving into it and back, and its close and a second open
12-
* unchanged
12+
* unchanged; and that a long press held through a busy main thread still
13+
* opens it (spec:AST-094 FR16: the hold is timed on the touch events' own
14+
* clock, so a late lift reports the press first)
1315
* @position Regression proof for ContextMenu mounting its layer at its first
1416
* open (row expansion gives every body cell a context menu)
1517
*/
@@ -184,4 +186,46 @@ test.describe('on a touch screen', () => {
184186
expect(Math.abs((anchor?.x ?? 0) - point.x)).toBeLessThanOrEqual(1);
185187
expect(Math.abs((anchor?.y ?? 0) - point.y)).toBeLessThanOrEqual(1);
186188
});
189+
190+
test('a long press held through a busy main thread still opens the menu at the finger (spec:AST-094 FR16)', async ({
191+
page,
192+
}) => {
193+
await mount(page, 'gliding-rows');
194+
const cell = nameCell(page);
195+
const box = await cell.boundingBox();
196+
if (box == null) {
197+
throw new Error('the cell has no rendered bounds');
198+
}
199+
const point = {x: Math.round(box.x) + 14, y: Math.round(box.y) + 10};
200+
// The page's main thread is busy for 1,200 ms from just after the touch
201+
// starts, through the 800 ms hold: a listener on the window, which runs
202+
// after the cell's own touch handlers, so the long press is timed by then.
203+
// The lift and the press's timer both wait behind that task.
204+
await page.evaluate(() => {
205+
window.addEventListener(
206+
'touchstart',
207+
() => {
208+
const end = performance.now() + 1200;
209+
while (performance.now() < end) {
210+
// Busy.
211+
}
212+
},
213+
{once: true, passive: true},
214+
);
215+
});
216+
const driver = await touchDriver(page);
217+
await driver.play(tap(point, {holdMs: 800}));
218+
// The menu is there and painted before anything is measured on it.
219+
const menu = page.getByRole('menu');
220+
await expect(menu).toHaveCount(1);
221+
await expect(menu).toBeVisible();
222+
const menuBox = await menu.boundingBox();
223+
expect(menuBox?.width ?? 0).toBeGreaterThan(0);
224+
expect(menuBox?.height ?? 0).toBeGreaterThan(0);
225+
const anchor = await cell
226+
.locator('span[aria-hidden="true"][style*="anchor-name"]')
227+
.boundingBox();
228+
expect(Math.abs((anchor?.x ?? 0) - point.x)).toBeLessThanOrEqual(1);
229+
expect(Math.abs((anchor?.y ?? 0) - point.y)).toBeLessThanOrEqual(1);
230+
});
187231
});

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export const docs = {
5959
],
6060
usage: {
6161
description:
62-
'Detects a single-finger press-and-hold and reports where it happened, so touch users can reach affordances that a pointer gets from right-click. iOS Safari never synthesizes a contextmenu event on long-press, which makes this the only touch route into a cursor-positioned surface such as ContextMenu. The press cancels on movement, lift, multi-touch or unmount.',
62+
'Detects a single-finger press-and-hold and reports where it happened, so touch users can reach affordances that a pointer gets from right-click. iOS Safari never synthesizes a contextmenu event on long-press, which makes this the only touch route into a cursor-positioned surface such as ContextMenu. The press cancels on movement, lift, multi-touch or unmount. The hold is timed on the touch events\u2019 own timestamps: when a busy page handles the lift late, a finger held for the full delay still reports its long press, before the lift acts.',
6363
bestPractices: [
6464
{
6565
guidance: true,
@@ -102,7 +102,7 @@ export const docsDense = {
102102
},
103103
usage: {
104104
description:
105-
'Touch press-and-hold for affordances a pointer reaches by right-click (e.g. ContextMenu). Reports the point so the surface opens under the finger.',
105+
"Touch press-and-hold for affordances a pointer reaches by right-click (e.g. ContextMenu). Reports the point so the surface opens under the finger. Timed on the touch events' timestamps: a busy page delays the report, never drops it.",
106106
bestPractices: [
107107
{
108108
guidance: true,

‎packages/core/src/hooks/useLongPress.test.tsx‎

Lines changed: 210 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,25 @@ function touchEvent(touches: {clientX: number; clientY: number}[]) {
99
return {touches} as unknown as React.TouchEvent;
1010
}
1111

12+
// A window's clock (`performance`): `now()` reads `at` now and moves with
13+
// the fake timers. A same-origin iframe or Picture-in-Picture window has a
14+
// time origin of its own.
15+
const EPOCH_ORIGIN = 1_700_000_000_000;
16+
function windowClock(at: number, timeOrigin = EPOCH_ORIGIN) {
17+
const offset = at - Date.now();
18+
return {performance: {now: () => Date.now() + offset, timeOrigin}};
19+
}
20+
21+
// The same stand-in, dispatched in `view` and stamped on its clock
22+
// (`event.timeStamp`).
23+
function stamped(
24+
view: ReturnType<typeof windowClock>,
25+
timeStamp: number,
26+
touches: {clientX: number; clientY: number}[] = [],
27+
) {
28+
return {touches, timeStamp, view} as unknown as React.TouchEvent;
29+
}
30+
1231
describe('useLongPress', () => {
1332
beforeEach(() => {
1433
vi.useFakeTimers();
@@ -188,3 +207,194 @@ describe('useLongPress', () => {
188207
expect(onLongPress).not.toHaveBeenCalled();
189208
});
190209
});
210+
211+
describe('useLongPress on the events\u2019 clock (spec:AST-094 FR16)', () => {
212+
beforeEach(() => {
213+
vi.useFakeTimers();
214+
});
215+
216+
afterEach(() => {
217+
vi.restoreAllMocks();
218+
vi.useRealTimers();
219+
});
220+
221+
it('reports the press first when the lift is stamped past the deadline, though the page timer has not run (a blocked main thread)', () => {
222+
const page = windowClock(1000);
223+
const onLongPress = vi.fn();
224+
const {result} = renderHook(() => useLongPress({onLongPress}));
225+
act(() => {
226+
result.current.onTouchStart(
227+
stamped(page, 1000, [{clientX: 10, clientY: 20}]),
228+
);
229+
result.current.onTouchEnd(stamped(page, 1800));
230+
});
231+
expect(onLongPress).toHaveBeenCalledTimes(1);
232+
expect(onLongPress).toHaveBeenCalledWith({x: 10, y: 20});
233+
act(() => {
234+
vi.advanceTimersByTime(1000);
235+
});
236+
expect(onLongPress).toHaveBeenCalledTimes(1);
237+
});
238+
239+
it('reports the press first when a move within the allowance is stamped past the deadline', () => {
240+
const page = windowClock(1000);
241+
const onLongPress = vi.fn();
242+
const {result} = renderHook(() => useLongPress({onLongPress}));
243+
act(() => {
244+
result.current.onTouchStart(
245+
stamped(page, 1000, [{clientX: 10, clientY: 20}]),
246+
);
247+
result.current.onTouchMove(
248+
stamped(page, 1600, [{clientX: 12, clientY: 21}]),
249+
);
250+
});
251+
expect(onLongPress).toHaveBeenCalledTimes(1);
252+
expect(onLongPress).toHaveBeenCalledWith({x: 10, y: 20});
253+
});
254+
255+
it('reports the press first when a second finger lands after the deadline, then ends it', () => {
256+
const page = windowClock(1000);
257+
const onLongPress = vi.fn();
258+
const {result} = renderHook(() => useLongPress({onLongPress}));
259+
act(() => {
260+
result.current.onTouchStart(
261+
stamped(page, 1000, [{clientX: 10, clientY: 20}]),
262+
);
263+
result.current.onTouchStart(
264+
stamped(page, 1700, [
265+
{clientX: 10, clientY: 20},
266+
{clientX: 90, clientY: 20},
267+
]),
268+
);
269+
vi.advanceTimersByTime(1000);
270+
});
271+
expect(onLongPress).toHaveBeenCalledTimes(1);
272+
});
273+
274+
it('reports the press when a cancel is stamped past the deadline, and drops it when stamped before', () => {
275+
const page = windowClock(1000);
276+
const onLongPress = vi.fn();
277+
const {result} = renderHook(() => useLongPress({onLongPress}));
278+
act(() => {
279+
result.current.onTouchStart(
280+
stamped(page, 1000, [{clientX: 0, clientY: 0}]),
281+
);
282+
result.current.onTouchCancel(stamped(page, 1300));
283+
vi.advanceTimersByTime(1000);
284+
});
285+
expect(onLongPress).not.toHaveBeenCalled();
286+
const later = windowClock(5000);
287+
act(() => {
288+
result.current.onTouchStart(
289+
stamped(later, 5000, [{clientX: 0, clientY: 0}]),
290+
);
291+
result.current.onTouchCancel(stamped(later, 5600));
292+
});
293+
expect(onLongPress).toHaveBeenCalledTimes(1);
294+
});
295+
296+
it('does not lengthen the hold of a touch the page handled late: the timer waits only for the rest', () => {
297+
const page = windowClock(1300);
298+
const onLongPress = vi.fn();
299+
const {result} = renderHook(() => useLongPress({onLongPress}));
300+
act(() => {
301+
result.current.onTouchStart(
302+
stamped(page, 1000, [{clientX: 0, clientY: 0}]),
303+
);
304+
vi.advanceTimersByTime(199);
305+
});
306+
expect(onLongPress).not.toHaveBeenCalled();
307+
act(() => {
308+
vi.advanceTimersByTime(1);
309+
});
310+
expect(onLongPress).toHaveBeenCalledTimes(1);
311+
});
312+
313+
it('lets a lift stamped before the deadline cancel the press, though its timer ran late', () => {
314+
const page = windowClock(2000);
315+
const onLongPress = vi.fn();
316+
const {result} = renderHook(() => useLongPress({onLongPress}));
317+
act(() => {
318+
// Handled 1,000 ms late: the press was due 500 ms ago, so its timer
319+
// runs at once, finds itself late, and yields to queued input once.
320+
result.current.onTouchStart(
321+
stamped(page, 1000, [{clientX: 0, clientY: 0}]),
322+
);
323+
vi.advanceTimersByTime(0);
324+
});
325+
expect(onLongPress).not.toHaveBeenCalled();
326+
act(() => {
327+
// The lift queued behind the long task: 300 ms after the touch.
328+
result.current.onTouchEnd(stamped(page, 1300));
329+
vi.advanceTimersByTime(100);
330+
});
331+
expect(onLongPress).not.toHaveBeenCalled();
332+
});
333+
334+
it('reads a touch in a same-origin iframe or Picture-in-Picture window on that window\u2019s clock, whose origin is later than the page\u2019s', () => {
335+
// The page's own clock reads far ahead of the iframe's.
336+
vi.spyOn(performance, 'now').mockImplementation(() => Date.now() + 5000);
337+
const frame = windowClock(100);
338+
const onLongPress = vi.fn();
339+
const {result} = renderHook(() => useLongPress({onLongPress}));
340+
// A tap of 120 ms in the iframe opens nothing.
341+
act(() => {
342+
result.current.onTouchStart(
343+
stamped(frame, 100, [{clientX: 0, clientY: 0}]),
344+
);
345+
vi.advanceTimersByTime(120);
346+
result.current.onTouchEnd(stamped(frame, 220));
347+
vi.advanceTimersByTime(1000);
348+
});
349+
expect(onLongPress).not.toHaveBeenCalled();
350+
// A finger held there for the delay opens it at the delay, not before.
351+
const held = windowClock(2000);
352+
act(() => {
353+
result.current.onTouchStart(
354+
stamped(held, 2000, [{clientX: 4, clientY: 6}]),
355+
);
356+
vi.advanceTimersByTime(499);
357+
});
358+
expect(onLongPress).not.toHaveBeenCalled();
359+
act(() => {
360+
vi.advanceTimersByTime(1);
361+
});
362+
expect(onLongPress).toHaveBeenCalledWith({x: 4, y: 6});
363+
});
364+
365+
it('brings an event stamped in epoch milliseconds (jsdom) onto its window\u2019s clock through the time origin', () => {
366+
// Handled 300 ms late on a clock whose events arrive in epoch ms: on the
367+
// window's clock the press is due in 200 ms, not the full delay.
368+
const page = windowClock(1300);
369+
const onLongPress = vi.fn();
370+
const {result} = renderHook(() => useLongPress({onLongPress}));
371+
act(() => {
372+
result.current.onTouchStart(
373+
stamped(page, EPOCH_ORIGIN + 1000, [{clientX: 0, clientY: 0}]),
374+
);
375+
vi.advanceTimersByTime(199);
376+
});
377+
expect(onLongPress).not.toHaveBeenCalled();
378+
act(() => {
379+
vi.advanceTimersByTime(1);
380+
});
381+
expect(onLongPress).toHaveBeenCalledTimes(1);
382+
});
383+
384+
it('never waits longer than the delay when a touch is stamped ahead of its clock', () => {
385+
const page = windowClock(1000);
386+
const onLongPress = vi.fn();
387+
const {result} = renderHook(() => useLongPress({onLongPress}));
388+
act(() => {
389+
result.current.onTouchStart(
390+
stamped(page, 1400, [{clientX: 0, clientY: 0}]),
391+
);
392+
vi.advanceTimersByTime(499);
393+
});
394+
expect(onLongPress).not.toHaveBeenCalled();
395+
act(() => {
396+
vi.advanceTimersByTime(1);
397+
});
398+
expect(onLongPress).toHaveBeenCalledTimes(1);
399+
});
400+
});

0 commit comments

Comments
 (0)