@remit/ui 0.0.220 → 0.0.222
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -1
- package/src/components/calendar-grid.render.test.ts +125 -0
- package/src/components/calendar-grid.stories.tsx +2 -0
- package/src/components/calendar-grid.tsx +54 -1
- package/src/components/isolated-email-frame.tsx +54 -2
- package/src/components/mobile-reading-pane.render.test.ts +2 -2
- package/src/components/mobile-reading-pane.stories.tsx +78 -0
- package/src/components/mobile-reading-pane.tsx +55 -41
- package/src/components/swipeable-row.gesture.test.ts +100 -40
- package/src/components/swipeable-row.stories.tsx +3 -2
- package/src/components/swipeable-row.tsx +65 -144
- package/src/index.ts +0 -4
- package/src/lib/swipe-config.ts +11 -0
- package/src/lib/use-long-press.test.ts +53 -36
- package/src/lib/use-long-press.ts +62 -56
- package/src/lib/use-swipe-navigation.test.ts +162 -0
- package/src/lib/use-swipe-navigation.ts +90 -0
|
@@ -1,23 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SwipeableRow — jsdom gesture tests against the real react-aria long-press
|
|
3
|
-
* wiring interacting with axis arbitration.
|
|
4
|
-
*
|
|
5
|
-
* The one this guards against: react-aria's `useLongPress` dispatches its
|
|
6
|
-
* own synthetic `pointercancel` right before calling `onLongPress` (to
|
|
7
|
-
* preempt other pointer consumers). SwipeableRow's `onPointerCancel` was
|
|
8
|
-
* originally aliased straight to `onPointerUp`, whose "no axis claimed"
|
|
9
|
-
* branch reads as a tap and calls `onOpen`/`onToggleCheck` — so a clean long
|
|
10
|
-
* press would fire onLongPress AND a spurious onOpen in the same gesture.
|
|
11
|
-
* The fix tags SwipeableRow's own axis-abort cancel so it can tell the two
|
|
12
|
-
* apart; these tests exercise both paths against the real hook, not a
|
|
13
|
-
* description of the fix.
|
|
14
|
-
*
|
|
15
|
-
* The second: a finger held on glass drifts, and it drifts past the distance
|
|
16
|
-
* at which the drag starts tracking. Cancelling the press there meant a real
|
|
17
|
-
* hold on a phone never entered selection mode — the row followed the drift
|
|
18
|
-
* and snapped back, and nothing else happened.
|
|
19
|
-
*/
|
|
20
|
-
|
|
21
1
|
import "@remit/test-dom";
|
|
22
2
|
import assert from "node:assert/strict";
|
|
23
3
|
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
@@ -26,7 +6,7 @@ import { createRoot, type Root } from "react-dom/client";
|
|
|
26
6
|
import type { ThreadRowData } from "./app-shell-types.js";
|
|
27
7
|
import { SwipeableRow, type SwipePeek } from "./swipeable-row.js";
|
|
28
8
|
|
|
29
|
-
const THRESHOLD_WAIT = 560;
|
|
9
|
+
const THRESHOLD_WAIT = 560;
|
|
30
10
|
|
|
31
11
|
const thread: ThreadRowData = {
|
|
32
12
|
id: "thread-1",
|
|
@@ -58,6 +38,8 @@ interface Handlers {
|
|
|
58
38
|
onLongPress: () => void;
|
|
59
39
|
onOpen: () => void;
|
|
60
40
|
onPeek: (next: SwipePeek) => void;
|
|
41
|
+
onToggleCheck?: () => void;
|
|
42
|
+
selectionMode?: boolean;
|
|
61
43
|
}
|
|
62
44
|
|
|
63
45
|
function mount(handlers: Handlers) {
|
|
@@ -65,12 +47,12 @@ function mount(handlers: Handlers) {
|
|
|
65
47
|
root.render(
|
|
66
48
|
createElement(SwipeableRow, {
|
|
67
49
|
thread,
|
|
68
|
-
selectionMode: false,
|
|
50
|
+
selectionMode: handlers.selectionMode ?? false,
|
|
69
51
|
checked: false,
|
|
70
52
|
active: false,
|
|
71
53
|
peek: "none",
|
|
72
54
|
onPeek: handlers.onPeek,
|
|
73
|
-
onToggleCheck: () => undefined,
|
|
55
|
+
onToggleCheck: handlers.onToggleCheck ?? (() => undefined),
|
|
74
56
|
onLongPress: handlers.onLongPress,
|
|
75
57
|
onOpen: handlers.onOpen,
|
|
76
58
|
onAct: () => undefined,
|
|
@@ -107,6 +89,7 @@ function pointerDown(row: Element, x = 10, y = 10) {
|
|
|
107
89
|
row.dispatchEvent(
|
|
108
90
|
new PointerEvent("pointerdown", {
|
|
109
91
|
bubbles: true,
|
|
92
|
+
buttons: 1,
|
|
110
93
|
pointerType: "touch",
|
|
111
94
|
pointerId: 1,
|
|
112
95
|
clientX: x,
|
|
@@ -121,6 +104,7 @@ function pointerMove(row: Element, x: number, y: number) {
|
|
|
121
104
|
row.dispatchEvent(
|
|
122
105
|
new PointerEvent("pointermove", {
|
|
123
106
|
bubbles: true,
|
|
107
|
+
buttons: 1,
|
|
124
108
|
pointerType: "touch",
|
|
125
109
|
pointerId: 1,
|
|
126
110
|
clientX: x,
|
|
@@ -130,6 +114,18 @@ function pointerMove(row: Element, x: number, y: number) {
|
|
|
130
114
|
});
|
|
131
115
|
}
|
|
132
116
|
|
|
117
|
+
function pointerCancel(row: Element) {
|
|
118
|
+
act(() => {
|
|
119
|
+
row.dispatchEvent(
|
|
120
|
+
new PointerEvent("pointercancel", {
|
|
121
|
+
bubbles: true,
|
|
122
|
+
pointerType: "touch",
|
|
123
|
+
pointerId: 1,
|
|
124
|
+
}),
|
|
125
|
+
);
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
133
129
|
function pointerUp(row: Element) {
|
|
134
130
|
// Dispatched on the row, not document: SwipeableRow's own onPointerUp is a
|
|
135
131
|
// React prop on the row element, delegated via React's root-container
|
|
@@ -153,8 +149,8 @@ function wait(ms: number) {
|
|
|
153
149
|
return act(() => new Promise((resolve) => setTimeout(resolve, ms)));
|
|
154
150
|
}
|
|
155
151
|
|
|
156
|
-
describe("SwipeableRow gesture wiring
|
|
157
|
-
it("fires onLongPress on an unmoved press, with no
|
|
152
|
+
describe("SwipeableRow gesture wiring", () => {
|
|
153
|
+
it("fires onLongPress on an unmoved press, with no tap action on release", async () => {
|
|
158
154
|
let longPressed = 0;
|
|
159
155
|
let opened = 0;
|
|
160
156
|
const row = mount({
|
|
@@ -168,11 +164,7 @@ describe("SwipeableRow gesture wiring (react-aria long press + axis arbitration)
|
|
|
168
164
|
pointerUp(row);
|
|
169
165
|
|
|
170
166
|
assert.equal(longPressed, 1);
|
|
171
|
-
assert.equal(
|
|
172
|
-
opened,
|
|
173
|
-
0,
|
|
174
|
-
"react-aria's own pointercancel (dispatched right before onLongPress) must not be read as a tap-to-open",
|
|
175
|
-
);
|
|
167
|
+
assert.equal(opened, 0, "a release after the hold is not a tap");
|
|
176
168
|
});
|
|
177
169
|
|
|
178
170
|
it("a horizontal drag past the escape distance cancels the long press and commits a swipe peek, not onOpen", async () => {
|
|
@@ -282,23 +274,91 @@ describe("SwipeableRow gesture wiring (react-aria long press + axis arbitration)
|
|
|
282
274
|
});
|
|
283
275
|
|
|
284
276
|
it("an aborted short drag opens nothing on release", async () => {
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
// unresolved press.
|
|
288
|
-
let clicks = 0;
|
|
277
|
+
let opened = 0;
|
|
278
|
+
const peeks: SwipePeek[] = [];
|
|
289
279
|
const row = mount({
|
|
290
280
|
onLongPress: () => undefined,
|
|
291
|
-
onOpen: () =>
|
|
292
|
-
onPeek: () =>
|
|
281
|
+
onOpen: () => opened++,
|
|
282
|
+
onPeek: (next) => peeks.push(next),
|
|
293
283
|
});
|
|
294
|
-
row.addEventListener("click", () => clicks++);
|
|
295
284
|
|
|
296
285
|
pointerDown(row, 100, 200);
|
|
297
|
-
pointerMove(row, 120, 201);
|
|
286
|
+
pointerMove(row, 120, 201);
|
|
287
|
+
pointerUp(row);
|
|
288
|
+
await wait(200);
|
|
289
|
+
|
|
290
|
+
assert.equal(opened, 0);
|
|
291
|
+
assert.deepEqual(peeks, []);
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
it("a short press opens the message", async () => {
|
|
295
|
+
let opened = 0;
|
|
296
|
+
let longPressed = 0;
|
|
297
|
+
const row = mount({
|
|
298
|
+
onLongPress: () => longPressed++,
|
|
299
|
+
onOpen: () => opened++,
|
|
300
|
+
onPeek: () => undefined,
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
pointerDown(row);
|
|
304
|
+
pointerUp(row);
|
|
305
|
+
await wait(THRESHOLD_WAIT);
|
|
306
|
+
|
|
307
|
+
assert.equal(opened, 1);
|
|
308
|
+
assert.equal(longPressed, 0);
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
it("a short press in selection mode toggles the row and opens nothing", async () => {
|
|
312
|
+
let opened = 0;
|
|
313
|
+
let toggled = 0;
|
|
314
|
+
const row = mount({
|
|
315
|
+
selectionMode: true,
|
|
316
|
+
onLongPress: () => undefined,
|
|
317
|
+
onOpen: () => opened++,
|
|
318
|
+
onToggleCheck: () => toggled++,
|
|
319
|
+
onPeek: () => undefined,
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
pointerDown(row);
|
|
298
323
|
pointerUp(row);
|
|
299
|
-
await wait(200); // react-aria synthesizes its click 80ms after release
|
|
300
324
|
|
|
301
|
-
assert.equal(
|
|
325
|
+
assert.equal(toggled, 1);
|
|
326
|
+
assert.equal(opened, 0);
|
|
327
|
+
});
|
|
328
|
+
|
|
329
|
+
it("a browser pointercancel resets the gesture and fires nothing", async () => {
|
|
330
|
+
let longPressed = 0;
|
|
331
|
+
let opened = 0;
|
|
332
|
+
const peeks: SwipePeek[] = [];
|
|
333
|
+
const row = mount({
|
|
334
|
+
onLongPress: () => longPressed++,
|
|
335
|
+
onOpen: () => opened++,
|
|
336
|
+
onPeek: (next) => peeks.push(next),
|
|
337
|
+
});
|
|
338
|
+
|
|
339
|
+
pointerDown(row);
|
|
340
|
+
pointerMove(row, 50, 10);
|
|
341
|
+
pointerCancel(row);
|
|
342
|
+
await wait(THRESHOLD_WAIT);
|
|
343
|
+
|
|
344
|
+
assert.equal(longPressed, 0);
|
|
345
|
+
assert.equal(opened, 0);
|
|
346
|
+
assert.deepEqual(peeks, []);
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
it("a press cancelled before the hold fires no long press", async () => {
|
|
350
|
+
let longPressed = 0;
|
|
351
|
+
const row = mount({
|
|
352
|
+
onLongPress: () => longPressed++,
|
|
353
|
+
onOpen: () => undefined,
|
|
354
|
+
onPeek: () => undefined,
|
|
355
|
+
});
|
|
356
|
+
|
|
357
|
+
pointerDown(row);
|
|
358
|
+
pointerCancel(row);
|
|
359
|
+
await wait(THRESHOLD_WAIT);
|
|
360
|
+
|
|
361
|
+
assert.equal(longPressed, 0);
|
|
302
362
|
});
|
|
303
363
|
|
|
304
364
|
it("suppresses the native context menu raised over a drifting hold", async () => {
|
|
@@ -132,7 +132,7 @@ const rowElement = (canvasElement: HTMLElement) =>
|
|
|
132
132
|
|
|
133
133
|
/**
|
|
134
134
|
* The long press is the way into multi-select on touch. Press and hold the row
|
|
135
|
-
* (mouse hold works too
|
|
135
|
+
* (a mouse hold works too) and it
|
|
136
136
|
* flips into the selection state the `SelectionChecked` story shows: the
|
|
137
137
|
* leading avatar becomes a filled, ticked checkbox and a tap toggles the row
|
|
138
138
|
* instead of opening it.
|
|
@@ -162,7 +162,7 @@ export const LongPressToSelect: Story = {
|
|
|
162
162
|
* The same gesture with a real finger's drift. A hand holding still on glass
|
|
163
163
|
* wanders several pixels over the 500ms hold — further than the distance at
|
|
164
164
|
* which the drag starts tracking the row, which is why a hold used to nudge
|
|
165
|
-
* the row and then do nothing. The
|
|
165
|
+
* the row and then do nothing. The drag only claims the gesture once it has
|
|
166
166
|
* travelled far enough to commit a peek, so the drifting hold below still ends
|
|
167
167
|
* in selection mode. The play step drives the press, the drift and the hold.
|
|
168
168
|
*/
|
|
@@ -192,6 +192,7 @@ export const LongPressWithDrift: Story = {
|
|
|
192
192
|
row.dispatchEvent(
|
|
193
193
|
new PointerEvent(type, {
|
|
194
194
|
bubbles: true,
|
|
195
|
+
buttons: type === "pointerup" ? 0 : 1,
|
|
195
196
|
pointerType: "touch",
|
|
196
197
|
pointerId: 1,
|
|
197
198
|
clientX,
|
|
@@ -1,8 +1,19 @@
|
|
|
1
|
+
import { type FullGestureState, useDrag } from "@use-gesture/react";
|
|
1
2
|
import { Check, Mail, MailOpen, Trash2 } from "lucide-react";
|
|
2
3
|
import { useRef, useState } from "react";
|
|
3
4
|
import { mergeProps } from "react-aria";
|
|
4
5
|
import { cn } from "../lib/cn.js";
|
|
5
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
SWIPE_AXIS_THRESHOLD,
|
|
8
|
+
SWIPE_DRAG_CONFIG,
|
|
9
|
+
} from "../lib/swipe-config.js";
|
|
10
|
+
import {
|
|
11
|
+
holdConfig,
|
|
12
|
+
isHold,
|
|
13
|
+
LONG_PRESS_DELAY_MS,
|
|
14
|
+
swallowReleaseClick,
|
|
15
|
+
touchMenuSuppressionProps,
|
|
16
|
+
} from "../lib/use-long-press.js";
|
|
6
17
|
import type { ThreadRowData } from "./app-shell-types.js";
|
|
7
18
|
import { Avatar } from "./avatar.js";
|
|
8
19
|
import {
|
|
@@ -15,38 +26,6 @@ export type SwipePeek = "none" | "leading" | "trailing";
|
|
|
15
26
|
/** Width a peeked row settles at to reveal its action; also the drag distance
|
|
16
27
|
* past which a release commits the peek rather than snapping back. */
|
|
17
28
|
const SWIPE_ACTION_WIDTH = 72;
|
|
18
|
-
/** Movement (px) before a pointer drag claims an axis; below this a press is
|
|
19
|
-
* still a tap / long-press and vertical scroll wins. Claiming an axis only
|
|
20
|
-
* decides what the row tracks — it does not decide the long press. */
|
|
21
|
-
const SWIPE_AXIS_THRESHOLD = 10;
|
|
22
|
-
/**
|
|
23
|
-
* Movement (px) along the claimed axis before a drag takes the gesture away
|
|
24
|
-
* from a still-pending long press.
|
|
25
|
-
*
|
|
26
|
-
* A finger resting on glass for the 500ms hold drifts, and it drifts further
|
|
27
|
-
* than the axis threshold — that threshold is calibrated for a drag starting
|
|
28
|
-
* to track, which has to feel immediate. Killing the press at that distance
|
|
29
|
-
* meant a real hold read as a swipe: the row followed the drift, the press
|
|
30
|
-
* died, and on release the drift was too short to commit a peek, so the
|
|
31
|
-
* gesture produced nothing at all.
|
|
32
|
-
*
|
|
33
|
-
* The escape distance is the commit distance, so the swipe only takes the
|
|
34
|
-
* gesture once it has moved far enough to actually produce a peek. Below it a
|
|
35
|
-
* release snaps back, which is to say the swipe was never worth the press.
|
|
36
|
-
*/
|
|
37
|
-
const LONG_PRESS_ESCAPE = SWIPE_ACTION_WIDTH / 2;
|
|
38
|
-
|
|
39
|
-
/**
|
|
40
|
-
* Tags a pointercancel dispatched by this component's own arbitration (see
|
|
41
|
-
* `cancelLongPress` below) so `onPointerCancel` can tell it apart from one
|
|
42
|
-
* react-aria dispatches itself when its own long press fires, or a genuine
|
|
43
|
-
* browser-triggered cancel. Both of those end the gesture and must reset it
|
|
44
|
-
* silently; a tagged one arrives *because* `onPointerMove`/`onPointerUp` is
|
|
45
|
-
* already handling that gesture inline, so `onPointerCancel` must ignore it —
|
|
46
|
-
* otherwise it drops the drag and the release reads as a tap, firing a
|
|
47
|
-
* spurious onOpen/onToggleCheck.
|
|
48
|
-
*/
|
|
49
|
-
const AXIS_CANCEL = "__swipeableRowAxisCancel";
|
|
50
29
|
|
|
51
30
|
function peekOffset(peek: SwipePeek): number {
|
|
52
31
|
if (peek === "leading") return SWIPE_ACTION_WIDTH;
|
|
@@ -88,127 +67,69 @@ export function SwipeableRow({
|
|
|
88
67
|
* "trailing" = delete. */
|
|
89
68
|
onAct: (side: "leading" | "trailing") => void;
|
|
90
69
|
}) {
|
|
91
|
-
const gesture = useRef<{
|
|
92
|
-
startX: number;
|
|
93
|
-
startY: number;
|
|
94
|
-
axis: "none" | "horizontal" | "vertical";
|
|
95
|
-
/** The drag has passed LONG_PRESS_ESCAPE and already cancelled the press. */
|
|
96
|
-
escaped: boolean;
|
|
97
|
-
} | null>(null);
|
|
98
70
|
const [dragX, setDragX] = useState<number | null>(null);
|
|
71
|
+
const dragOffset = useRef<number | null>(null);
|
|
72
|
+
const held = useRef(false);
|
|
99
73
|
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
onLongPress,
|
|
104
|
-
isDisabled: selectionMode,
|
|
105
|
-
accessibilityDescription: "Select message",
|
|
106
|
-
});
|
|
107
|
-
|
|
108
|
-
// react-aria's usePress has no imperative "cancel" — a synthetic
|
|
109
|
-
// pointercancel is the mechanism it uses itself to abort other pointer
|
|
110
|
-
// consumers when its own long press fires. Reused here in reverse, tagged
|
|
111
|
-
// so onPointerCancel below can recognize it as ours (see AXIS_CANCEL).
|
|
112
|
-
const cancelLongPress = (e: React.PointerEvent) => {
|
|
113
|
-
const event = new PointerEvent("pointercancel", { bubbles: true });
|
|
114
|
-
(event as PointerEvent & Record<string, boolean>)[AXIS_CANCEL] = true;
|
|
115
|
-
e.currentTarget.dispatchEvent(event);
|
|
74
|
+
const trackDrag = (offset: number | null) => {
|
|
75
|
+
dragOffset.current = offset;
|
|
76
|
+
setDragX(offset);
|
|
116
77
|
};
|
|
117
78
|
|
|
118
|
-
const
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
if (selectionMode) return;
|
|
129
|
-
const g = gesture.current;
|
|
130
|
-
if (!g) return;
|
|
131
|
-
const dx = e.clientX - g.startX;
|
|
132
|
-
const dy = e.clientY - g.startY;
|
|
133
|
-
if (g.axis === "none") {
|
|
134
|
-
if (Math.abs(dy) > SWIPE_AXIS_THRESHOLD && Math.abs(dy) > Math.abs(dx)) {
|
|
135
|
-
// vertical scroll wins: stop tracking the swipe, let the list scroll
|
|
136
|
-
g.axis = "vertical";
|
|
137
|
-
} else if (
|
|
138
|
-
Math.abs(dx) > SWIPE_AXIS_THRESHOLD &&
|
|
139
|
-
Math.abs(dx) > Math.abs(dy)
|
|
140
|
-
) {
|
|
141
|
-
g.axis = "horizontal";
|
|
142
|
-
e.currentTarget.setPointerCapture(e.pointerId);
|
|
79
|
+
const release = (state: FullGestureState<"drag">) => {
|
|
80
|
+
const offset = dragOffset.current;
|
|
81
|
+
const wasHeld = held.current;
|
|
82
|
+
held.current = false;
|
|
83
|
+
trackDrag(null);
|
|
84
|
+
if (state.event.type.endsWith("cancel") || wasHeld) return;
|
|
85
|
+
if (state.tap) {
|
|
86
|
+
if (selectionMode) {
|
|
87
|
+
onToggleCheck();
|
|
88
|
+
return;
|
|
143
89
|
}
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
const travel = g.axis === "horizontal" ? Math.abs(dx) : Math.abs(dy);
|
|
150
|
-
if (!g.escaped && travel > LONG_PRESS_ESCAPE) {
|
|
151
|
-
g.escaped = true;
|
|
152
|
-
cancelLongPress(e);
|
|
153
|
-
}
|
|
154
|
-
if (g.axis !== "horizontal") return;
|
|
155
|
-
const base = peekOffset(peek);
|
|
156
|
-
const next = Math.max(
|
|
157
|
-
-SWIPE_ACTION_WIDTH,
|
|
158
|
-
Math.min(SWIPE_ACTION_WIDTH, base + dx),
|
|
159
|
-
);
|
|
160
|
-
setDragX(next);
|
|
161
|
-
};
|
|
162
|
-
|
|
163
|
-
const onPointerUp = (e: React.PointerEvent) => {
|
|
164
|
-
const g = gesture.current;
|
|
165
|
-
gesture.current = null;
|
|
166
|
-
const offset = dragX;
|
|
167
|
-
setDragX(null);
|
|
168
|
-
// g is null when the gesture was already consumed (the long press fired,
|
|
169
|
-
// or a real cancel arrived) — those handle themselves, so do nothing.
|
|
170
|
-
if (!g) return;
|
|
171
|
-
// A drag that claimed an axis but never escaped left react-aria's press
|
|
172
|
-
// live, and an unresolved press synthesizes a click on release. The drag
|
|
173
|
-
// was neither a swipe nor a tap, so end the press with it.
|
|
174
|
-
if (g.axis !== "none" && !g.escaped) cancelLongPress(e);
|
|
175
|
-
if (g.axis === "horizontal") {
|
|
176
|
-
if (offset !== null) onPeek(commitPeek(offset));
|
|
177
|
-
return;
|
|
178
|
-
}
|
|
179
|
-
// The list scrolled under the finger; the release is not a tap.
|
|
180
|
-
if (g.axis === "vertical") return;
|
|
181
|
-
// a tap: no axis claimed
|
|
182
|
-
if (selectionMode) {
|
|
183
|
-
onToggleCheck();
|
|
184
|
-
return;
|
|
185
|
-
}
|
|
186
|
-
if (peek !== "none") {
|
|
187
|
-
onPeek("none");
|
|
90
|
+
if (peek !== "none") {
|
|
91
|
+
onPeek("none");
|
|
92
|
+
return;
|
|
93
|
+
}
|
|
94
|
+
onOpen();
|
|
188
95
|
return;
|
|
189
96
|
}
|
|
190
|
-
|
|
97
|
+
if (state.axis !== "x" || selectionMode || offset === null) return;
|
|
98
|
+
onPeek(commitPeek(offset));
|
|
191
99
|
};
|
|
192
100
|
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
101
|
+
const bind = useDrag(
|
|
102
|
+
(state) => {
|
|
103
|
+
if (!state.down) {
|
|
104
|
+
release(state);
|
|
105
|
+
return;
|
|
106
|
+
}
|
|
107
|
+
if (isHold(state)) {
|
|
108
|
+
if (selectionMode) return;
|
|
109
|
+
held.current = true;
|
|
110
|
+
swallowReleaseClick();
|
|
111
|
+
onLongPress();
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
if (!state.intentional) return;
|
|
115
|
+
if (selectionMode || held.current || state.axis !== "x") return;
|
|
116
|
+
const travelled = state.values[0] - state.initial[0];
|
|
117
|
+
trackDrag(
|
|
118
|
+
Math.max(
|
|
119
|
+
-SWIPE_ACTION_WIDTH,
|
|
120
|
+
Math.min(SWIPE_ACTION_WIDTH, peekOffset(peek) + travelled),
|
|
121
|
+
),
|
|
122
|
+
);
|
|
123
|
+
},
|
|
124
|
+
{
|
|
125
|
+
...holdConfig(LONG_PRESS_DELAY_MS),
|
|
126
|
+
tapsThreshold: SWIPE_AXIS_THRESHOLD,
|
|
127
|
+
axisThreshold: SWIPE_DRAG_CONFIG.axisThreshold,
|
|
128
|
+
},
|
|
129
|
+
);
|
|
206
130
|
|
|
207
|
-
const gestureProps = mergeProps(
|
|
208
|
-
|
|
209
|
-
onPointerMove,
|
|
210
|
-
onPointerUp,
|
|
211
|
-
onPointerCancel,
|
|
131
|
+
const gestureProps = mergeProps(bind(), touchMenuSuppressionProps, {
|
|
132
|
+
"aria-description": selectionMode ? undefined : "Select message",
|
|
212
133
|
});
|
|
213
134
|
|
|
214
135
|
const offset = dragX ?? peekOffset(peek);
|
package/src/index.ts
CHANGED
|
@@ -1,7 +1,3 @@
|
|
|
1
|
-
// Re-exported so a consumer can compose useLongPress's longPressProps with its
|
|
2
|
-
// own DOM props (its pressProps include an onClick react-aria uses for its
|
|
3
|
-
// own bookkeeping; a plain object spread silently drops one side's handler
|
|
4
|
-
// instead of chaining them) without importing react-aria directly.
|
|
5
1
|
export { mergeProps } from "react-aria";
|
|
6
2
|
export {
|
|
7
3
|
type BriefCategoryFilter,
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export const SWIPE_AXIS_THRESHOLD = 10;
|
|
2
|
+
|
|
3
|
+
export const SWIPE_DRAG_CONFIG = {
|
|
4
|
+
axisThreshold: {
|
|
5
|
+
mouse: SWIPE_AXIS_THRESHOLD,
|
|
6
|
+
touch: SWIPE_AXIS_THRESHOLD,
|
|
7
|
+
pen: SWIPE_AXIS_THRESHOLD,
|
|
8
|
+
},
|
|
9
|
+
swipe: { distance: 60, velocity: 0.3, duration: 600 },
|
|
10
|
+
pointer: { keys: false, capture: false },
|
|
11
|
+
} as const;
|
|
@@ -1,24 +1,3 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* use-long-press — exercises the real hook (react-aria's `useLongPress`)
|
|
3
|
-
* against a jsdom-mounted element, not a reimplementation of its logic. The
|
|
4
|
-
* hook this replaces (`packages/web-client/src/hooks/useLongPress.ts`) had a
|
|
5
|
-
* decoy test that reimplemented the timer/threshold logic locally and so
|
|
6
|
-
* gave zero regression coverage on the actual hook; these tests dispatch
|
|
7
|
-
* real PointerEvents at a real mounted node and assert on the callback and
|
|
8
|
-
* the DOM side effects react-aria owns (contextmenu suppression).
|
|
9
|
-
*
|
|
10
|
-
* react-aria's pointerdown → threshold timer → onLongPress path, its global
|
|
11
|
-
* pointerup/pointercancel listeners, and its contextmenu suppression all
|
|
12
|
-
* need a real `document`/`window`/`PointerEvent`, which `renderToString`
|
|
13
|
-
* (the pattern used elsewhere in this repo for presentational components)
|
|
14
|
-
* cannot exercise.
|
|
15
|
-
*
|
|
16
|
-
* The clock is mocked. Every timing assertion here is about one boundary —
|
|
17
|
-
* the press crossed the threshold, or it ended first — and racing that
|
|
18
|
-
* boundary against a wall clock on a loaded runner turns a passing test red
|
|
19
|
-
* (#645). Time only moves when `advance` moves it.
|
|
20
|
-
*/
|
|
21
|
-
|
|
22
1
|
import "@remit/test-dom";
|
|
23
2
|
import assert from "node:assert/strict";
|
|
24
3
|
import { afterEach, beforeEach, describe, it, mock } from "node:test";
|
|
@@ -28,8 +7,7 @@ import { useLongPress } from "./use-long-press.js";
|
|
|
28
7
|
|
|
29
8
|
const THRESHOLD = 40;
|
|
30
9
|
|
|
31
|
-
|
|
32
|
-
const AFTER_ARIA_CONTEXTMENU_TEARDOWN = 100;
|
|
10
|
+
const AFTER_RELEASE = 100;
|
|
33
11
|
|
|
34
12
|
let container: HTMLElement;
|
|
35
13
|
let root: Root;
|
|
@@ -69,6 +47,7 @@ function pointerDown(row: Element, pointerType = "touch") {
|
|
|
69
47
|
row.dispatchEvent(
|
|
70
48
|
new PointerEvent("pointerdown", {
|
|
71
49
|
bubbles: true,
|
|
50
|
+
buttons: 1,
|
|
72
51
|
pointerType,
|
|
73
52
|
pointerId: 1,
|
|
74
53
|
clientX: 10,
|
|
@@ -77,6 +56,19 @@ function pointerDown(row: Element, pointerType = "touch") {
|
|
|
77
56
|
);
|
|
78
57
|
}
|
|
79
58
|
|
|
59
|
+
function pointerMove(row: Element, x: number, y: number) {
|
|
60
|
+
row.dispatchEvent(
|
|
61
|
+
new PointerEvent("pointermove", {
|
|
62
|
+
bubbles: true,
|
|
63
|
+
buttons: 1,
|
|
64
|
+
pointerType: "touch",
|
|
65
|
+
pointerId: 1,
|
|
66
|
+
clientX: x,
|
|
67
|
+
clientY: y,
|
|
68
|
+
}),
|
|
69
|
+
);
|
|
70
|
+
}
|
|
71
|
+
|
|
80
72
|
function dispatchContextMenu(row: Element) {
|
|
81
73
|
const event = new MouseEvent("contextmenu", {
|
|
82
74
|
bubbles: true,
|
|
@@ -111,7 +103,13 @@ function pointerUpOn(row: Element) {
|
|
|
111
103
|
}
|
|
112
104
|
|
|
113
105
|
function pointerCancel(row: Element) {
|
|
114
|
-
row.dispatchEvent(
|
|
106
|
+
row.dispatchEvent(
|
|
107
|
+
new PointerEvent("pointercancel", {
|
|
108
|
+
bubbles: true,
|
|
109
|
+
pointerType: "touch",
|
|
110
|
+
pointerId: 1,
|
|
111
|
+
}),
|
|
112
|
+
);
|
|
115
113
|
}
|
|
116
114
|
|
|
117
115
|
function advance(ms: number) {
|
|
@@ -130,17 +128,19 @@ beforeEach(() => {
|
|
|
130
128
|
afterEach(() => {
|
|
131
129
|
// The hook's contextmenu arming is module state bounded by a timer, and a
|
|
132
130
|
// test that leaves the finger down leaves it armed. Release the pointer so
|
|
133
|
-
// the next test starts disarmed rather than inheriting a dead timer id.
|
|
131
|
+
// the next test starts disarmed rather than inheriting a dead timer id. The
|
|
132
|
+
// click swallow armed by a fired hold expires on the same clock.
|
|
134
133
|
act(() => {
|
|
135
134
|
pointerUp();
|
|
136
135
|
});
|
|
136
|
+
mock.timers.tick(10_000);
|
|
137
137
|
mock.timers.reset();
|
|
138
138
|
act(() => {
|
|
139
139
|
root.unmount();
|
|
140
140
|
});
|
|
141
141
|
});
|
|
142
142
|
|
|
143
|
-
describe("useLongPress
|
|
143
|
+
describe("useLongPress", () => {
|
|
144
144
|
it("fires onLongPress after the threshold with no interruption", async () => {
|
|
145
145
|
let fired = 0;
|
|
146
146
|
const row = mount({ onLongPress: () => fired++ });
|
|
@@ -160,16 +160,13 @@ describe("useLongPress (react-aria wrapper)", () => {
|
|
|
160
160
|
|
|
161
161
|
pointerDown(row);
|
|
162
162
|
await advance(THRESHOLD - 1);
|
|
163
|
-
|
|
163
|
+
pointerUpOn(row);
|
|
164
164
|
await advance(THRESHOLD);
|
|
165
165
|
|
|
166
166
|
assert.equal(fired, 0);
|
|
167
167
|
});
|
|
168
168
|
|
|
169
169
|
it("does not fire when cancelled via a pointercancel before the threshold", async () => {
|
|
170
|
-
// This is the mechanism SwipeableRow's axis arbitration relies on: it
|
|
171
|
-
// dispatches a synthetic pointercancel to abort a pending long press
|
|
172
|
-
// once a horizontal or vertical drag claims the gesture.
|
|
173
170
|
let fired = 0;
|
|
174
171
|
const row = mount({ onLongPress: () => fired++ });
|
|
175
172
|
|
|
@@ -181,6 +178,28 @@ describe("useLongPress (react-aria wrapper)", () => {
|
|
|
181
178
|
assert.equal(fired, 0);
|
|
182
179
|
});
|
|
183
180
|
|
|
181
|
+
it("fires when the finger drifts less than the drift tolerance", async () => {
|
|
182
|
+
let fired = 0;
|
|
183
|
+
const row = mount({ onLongPress: () => fired++ });
|
|
184
|
+
|
|
185
|
+
pointerDown(row);
|
|
186
|
+
pointerMove(row, 30, 12);
|
|
187
|
+
await advance(THRESHOLD);
|
|
188
|
+
|
|
189
|
+
assert.equal(fired, 1);
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("does not fire once the finger has moved past the drift tolerance", async () => {
|
|
193
|
+
let fired = 0;
|
|
194
|
+
const row = mount({ onLongPress: () => fired++ });
|
|
195
|
+
|
|
196
|
+
pointerDown(row);
|
|
197
|
+
pointerMove(row, 80, 12);
|
|
198
|
+
await advance(THRESHOLD);
|
|
199
|
+
|
|
200
|
+
assert.equal(fired, 0);
|
|
201
|
+
});
|
|
202
|
+
|
|
184
203
|
it("does not fire while isDisabled", async () => {
|
|
185
204
|
let fired = 0;
|
|
186
205
|
const row = mount({ onLongPress: () => fired++, isDisabled: true });
|
|
@@ -249,7 +268,7 @@ describe("useLongPress (react-aria wrapper)", () => {
|
|
|
249
268
|
"the touch long-press menu is still suppressed",
|
|
250
269
|
);
|
|
251
270
|
pointerUpOn(row);
|
|
252
|
-
await advance(
|
|
271
|
+
await advance(AFTER_RELEASE);
|
|
253
272
|
|
|
254
273
|
assert.equal(
|
|
255
274
|
dispatchContextMenu(row).defaultPrevented,
|
|
@@ -259,15 +278,13 @@ describe("useLongPress (react-aria wrapper)", () => {
|
|
|
259
278
|
});
|
|
260
279
|
|
|
261
280
|
it("does not suppress the keyboard menu after a touch tap that raised no menu", async () => {
|
|
262
|
-
// A tap that lifts without a menu must still disarm suppression
|
|
263
|
-
//
|
|
264
|
-
// listener, which it removes shortly after pointerup — in a browser a
|
|
265
|
-
// keyboard menu arrives long after that, so only this hook's ref decides.
|
|
281
|
+
// A tap that lifts without a menu must still disarm suppression; a
|
|
282
|
+
// keyboard menu arrives long after the release.
|
|
266
283
|
const row = mount({ onLongPress: () => undefined });
|
|
267
284
|
|
|
268
285
|
pointerDown(row, "touch");
|
|
269
286
|
pointerUpOn(row);
|
|
270
|
-
await advance(
|
|
287
|
+
await advance(AFTER_RELEASE);
|
|
271
288
|
|
|
272
289
|
assert.equal(dispatchContextMenu(row).defaultPrevented, false);
|
|
273
290
|
});
|