Skip to content

Commit fa92cbe

Browse files
committed
refactor(core): useLongPress runs its long press through a detector with no React
The long-press detection moves out of useLongPressDetector's refs and callbacks into an internal factory, longPressDetector: a hand start from a point, the touch handlers on the events' clock, the movement tolerance, and the options it reads at each start, move and fire. useLongPressDetector creates one detector for the component's life, hands it the committed render's options after each commit, cancels it on unmount, and returns its methods as the handlers. A press model that runs without React can then share the one detector. No behavior change: the existing useLongPress cases pass unchanged. A new case keeps a suspended transition's options away from the press. The detector's own cases run with no React. A [chore] changeset. The core API report also gains the row for the optional event that useLongPress's onTouchEnd and onTouchCancel take, which the change below this one adds.
1 parent b377b21 commit fa92cbe

7 files changed

Lines changed: 515 additions & 241 deletions

File tree

‎.changeset/long-press-detector.md‎

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+
[chore] Internal: `useLongPress` runs its long-press detection through an internal detector with no React, so a press model that runs without React can share it. The hook and its handlers behave as before. Not exported from the package entry point.
6+
7+
@vjeux

‎packages/core/api-report.md‎

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 1 addition & 1 deletion
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/ ^packages/core/src/hooks/useLongPress ^apps/storybook/stories/TableRowExpansion
2+
// @pr-a11y scope ^packages/core/src/Table/ ^packages/core/src/ContextMenu/ ^packages/core/src/hooks/(useLongPress|longPressDetector) ^apps/storybook/stories/TableRowExpansion
33

44
/**
55
* @file TableCellMenus.a11y.browser.spec.ts
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
// Copyright (c) Meta Platforms, Inc. and affiliates.
2+
3+
/**
4+
* @file longPressDetector.test.ts
5+
* @input vitest, longPressDetector
6+
* @output Tests the long-press detector with no React: a hand start, a touch
7+
* press on the events' clock, a second finger, a late timer, a start handled
8+
* late, the movement tolerance, disabled, the latest options, and a touch
9+
* with no time
10+
* @position Unit coverage for longPressDetector.ts; useLongPress.test.tsx
11+
* covers the hook that wraps it
12+
*/
13+
14+
import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest';
15+
import {longPressDetector, type LongPressTouchEvent} from './longPressDetector';
16+
17+
// A window whose clock reads `at` now and moves with the fake timers.
18+
function windowAt(at: number) {
19+
const offset = at - Date.now();
20+
return {performance: {now: () => Date.now() + offset}};
21+
}
22+
23+
// A window whose clock reads only what the test sets: a busy page.
24+
function busyWindow(at: number) {
25+
const view = {at, performance: {now: () => view.at}};
26+
return view;
27+
}
28+
29+
function touch(
30+
view: unknown,
31+
timeStamp: number,
32+
points: {clientX: number; clientY: number}[] = [],
33+
): LongPressTouchEvent {
34+
return {touches: points, timeStamp, view};
35+
}
36+
37+
describe('longPressDetector', () => {
38+
beforeEach(() => {
39+
vi.useFakeTimers();
40+
});
41+
42+
afterEach(() => {
43+
vi.useRealTimers();
44+
});
45+
46+
it('fires at the start point after the delay when started by hand, and not after a cancel', () => {
47+
const onLongPress = vi.fn();
48+
const detector = longPressDetector({onLongPress});
49+
detector.start({x: 3, y: 4});
50+
vi.advanceTimersByTime(499);
51+
expect(onLongPress).not.toHaveBeenCalled();
52+
vi.advanceTimersByTime(1);
53+
expect(onLongPress).toHaveBeenCalledWith({x: 3, y: 4});
54+
detector.start({x: 0, y: 0});
55+
detector.cancel();
56+
vi.advanceTimersByTime(1000);
57+
expect(onLongPress).toHaveBeenCalledTimes(1);
58+
});
59+
60+
it('reports a touch press first when its lift is stamped past the deadline, with no timer run', () => {
61+
const page = windowAt(1000);
62+
const onLongPress = vi.fn();
63+
const detector = longPressDetector({onLongPress});
64+
detector.touchStart(touch(page, 1000, [{clientX: 10, clientY: 20}]));
65+
detector.touchEnd(touch(page, 1800));
66+
expect(onLongPress).toHaveBeenCalledWith({x: 10, y: 20});
67+
expect(detector.isPending()).toBe(false);
68+
});
69+
70+
it('ends the press on a second finger, and reports it first when that touch is stamped past the deadline', () => {
71+
const finger = {clientX: 5, clientY: 6};
72+
const early = vi.fn();
73+
const before = longPressDetector({onLongPress: early});
74+
const page = windowAt(1000);
75+
before.touchStart(touch(page, 1000, [finger]));
76+
before.touchStart(touch(page, 1200, [finger, finger]));
77+
expect(before.isPending()).toBe(false);
78+
vi.advanceTimersByTime(1000);
79+
expect(early).not.toHaveBeenCalled();
80+
81+
const late = vi.fn();
82+
const after = longPressDetector({onLongPress: late});
83+
const busy = busyWindow(1000);
84+
after.touchStart(touch(busy, 1000, [finger]));
85+
after.touchStart(touch(busy, 1600, [finger, finger]));
86+
expect(late).toHaveBeenCalledWith({x: 5, y: 6});
87+
expect(after.isPending()).toBe(false);
88+
});
89+
90+
it('lets a lift stamped before the deadline cancel the press when its timer runs late', () => {
91+
const onLongPress = vi.fn();
92+
const detector = longPressDetector({onLongPress});
93+
const busy = busyWindow(1000);
94+
detector.touchStart(touch(busy, 1000, [{clientX: 0, clientY: 0}]));
95+
// The page is busy past the deadline, so the timer runs late and yields
96+
// once to the queued lift.
97+
busy.at = 1700;
98+
vi.advanceTimersByTime(500);
99+
expect(onLongPress).not.toHaveBeenCalled();
100+
detector.touchEnd(touch(busy, 1400));
101+
vi.advanceTimersByTime(1000);
102+
expect(onLongPress).not.toHaveBeenCalled();
103+
104+
detector.touchStart(touch(busy, 1700, [{clientX: 0, clientY: 0}]));
105+
busy.at = 2400;
106+
vi.advanceTimersByTime(500);
107+
expect(onLongPress).not.toHaveBeenCalled();
108+
vi.advanceTimersByTime(1);
109+
expect(onLongPress).toHaveBeenCalledTimes(1);
110+
});
111+
112+
it('waits only for the rest of the hold when its start is handled late', () => {
113+
const onLongPress = vi.fn();
114+
const detector = longPressDetector({onLongPress});
115+
detector.touchStart(
116+
touch(windowAt(1300), 1000, [{clientX: 0, clientY: 0}]),
117+
);
118+
vi.advanceTimersByTime(199);
119+
expect(onLongPress).not.toHaveBeenCalled();
120+
vi.advanceTimersByTime(1);
121+
expect(onLongPress).toHaveBeenCalledTimes(1);
122+
});
123+
124+
it('drops the press past the movement tolerance, or past a per-press tolerance', () => {
125+
const onLongPress = vi.fn();
126+
const detector = longPressDetector({onLongPress});
127+
detector.start({x: 0, y: 0});
128+
expect(detector.moveTo({x: 10, y: 0})).toBe(true);
129+
expect(detector.moveTo({x: 11, y: 0})).toBe(false);
130+
detector.start({x: 0, y: 0}, {moveCancelPx: 13});
131+
expect(detector.moveTo({x: 13, y: 0})).toBe(true);
132+
vi.advanceTimersByTime(500);
133+
expect(onLongPress).toHaveBeenCalledTimes(1);
134+
});
135+
136+
it('starts nothing while disabled, and reads the latest options at each start and fire', () => {
137+
const first = vi.fn();
138+
const latest = vi.fn();
139+
const detector = longPressDetector({onLongPress: first, disabled: true});
140+
detector.start({x: 0, y: 0});
141+
expect(detector.isPending()).toBe(false);
142+
detector.setOptions({onLongPress: first, delayMs: 200});
143+
detector.start({x: 1, y: 1});
144+
detector.setOptions({onLongPress: latest, delayMs: 200});
145+
vi.advanceTimersByTime(200);
146+
expect(first).not.toHaveBeenCalled();
147+
expect(latest).toHaveBeenCalledWith({x: 1, y: 1});
148+
});
149+
150+
it('waits the full delay for a touch that carries no time (a stamp of 0)', () => {
151+
const onLongPress = vi.fn();
152+
const detector = longPressDetector({onLongPress});
153+
detector.touchStart(touch(windowAt(10_000), 0, [{clientX: 0, clientY: 0}]));
154+
vi.advanceTimersByTime(499);
155+
expect(onLongPress).not.toHaveBeenCalled();
156+
vi.advanceTimersByTime(1);
157+
expect(onLongPress).toHaveBeenCalledTimes(1);
158+
});
159+
});

0 commit comments

Comments
 (0)