@remit/ui 0.0.221 → 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/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,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
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import {
|
|
3
|
-
import { mergeProps
|
|
1
|
+
import { type FullGestureState, useDrag } from "@use-gesture/react";
|
|
2
|
+
import type { HTMLAttributes, PointerEvent } from "react";
|
|
3
|
+
import { mergeProps } from "react-aria";
|
|
4
4
|
|
|
5
5
|
/**
|
|
6
6
|
* Suppression lives on the document, not on the row.
|
|
@@ -27,7 +27,7 @@ const MAX_ARMED_MS = 5_000;
|
|
|
27
27
|
* release disarms too, which is what keeps a keyboard-invoked menu
|
|
28
28
|
* (Context-Menu key / Shift+F10) raised later from inheriting a press that is
|
|
29
29
|
* long over. Deliberately keyed to `pointerup` and not to `pointercancel`: on
|
|
30
|
-
* Android the browser
|
|
30
|
+
* Android the browser can cancel the
|
|
31
31
|
* pointer before the `contextmenu` it raised arrives, which would race the
|
|
32
32
|
* suppression away.
|
|
33
33
|
*/
|
|
@@ -54,74 +54,80 @@ function arm(): void {
|
|
|
54
54
|
armedTimer = setTimeout(disarm, MAX_ARMED_MS);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
|
+
let clickTimer: ReturnType<typeof setTimeout> | undefined;
|
|
58
|
+
|
|
59
|
+
function swallowClick(event: Event): void {
|
|
60
|
+
event.preventDefault();
|
|
61
|
+
event.stopPropagation();
|
|
62
|
+
releaseClick();
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function releaseClick(): void {
|
|
66
|
+
if (clickTimer === undefined) return;
|
|
67
|
+
clearTimeout(clickTimer);
|
|
68
|
+
clickTimer = undefined;
|
|
69
|
+
document.removeEventListener("click", swallowClick, true);
|
|
70
|
+
document.removeEventListener("pointerdown", releaseClick, true);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export function swallowReleaseClick(): void {
|
|
74
|
+
releaseClick();
|
|
75
|
+
document.addEventListener("click", swallowClick, true);
|
|
76
|
+
document.addEventListener("pointerdown", releaseClick, true);
|
|
77
|
+
clickTimer = setTimeout(releaseClick, MAX_ARMED_MS);
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export const LONG_PRESS_DELAY_MS = 500;
|
|
81
|
+
|
|
82
|
+
export const LONG_PRESS_DRIFT_PX = 36;
|
|
83
|
+
|
|
57
84
|
export interface UseLongPressOptions {
|
|
58
|
-
/** Called once the threshold elapses while the press stays over the target. */
|
|
59
85
|
onLongPress: () => void;
|
|
60
|
-
/** Long press is a no-op while true (e.g. a row already in selection mode). */
|
|
61
86
|
isDisabled?: boolean;
|
|
62
|
-
/** @default 500 */
|
|
63
87
|
delayMs?: number;
|
|
64
|
-
/**
|
|
65
|
-
* Announced to assistive technology as the long-press action, e.g.
|
|
66
|
-
* "Select message". TalkBack/VoiceOver have no gesture equivalent for a
|
|
67
|
-
* timed hold, so this description — not the gesture itself — is what
|
|
68
|
-
* makes the action discoverable to a screen reader user.
|
|
69
|
-
*/
|
|
70
88
|
accessibilityDescription?: string;
|
|
71
89
|
}
|
|
72
90
|
|
|
73
91
|
export interface UseLongPressResult {
|
|
74
|
-
|
|
75
|
-
longPressProps: DOMAttributes;
|
|
92
|
+
longPressProps: HTMLAttributes<HTMLElement>;
|
|
76
93
|
}
|
|
77
94
|
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
* the swipeable row with the plain one while the finger is still down, so a
|
|
97
|
-
* handler bound to the pressed node is gone by the time the menu arrives.
|
|
98
|
-
*
|
|
99
|
-
* Single source of truth for the app's long-press threshold — both mobile
|
|
100
|
-
* row consumers (the plain row and the swipeable row) go through this hook
|
|
101
|
-
* so their timing can't drift apart again.
|
|
102
|
-
*/
|
|
95
|
+
export const holdConfig = (delayMs: number) =>
|
|
96
|
+
({
|
|
97
|
+
delay: delayMs,
|
|
98
|
+
threshold: LONG_PRESS_DRIFT_PX,
|
|
99
|
+
triggerAllEvents: true,
|
|
100
|
+
pointer: { keys: false },
|
|
101
|
+
}) as const;
|
|
102
|
+
|
|
103
|
+
export const isHold = (state: FullGestureState<"drag">): boolean =>
|
|
104
|
+
state.event.type === "pointerdown" && state.first;
|
|
105
|
+
|
|
106
|
+
export const touchMenuSuppressionProps = {
|
|
107
|
+
onPointerDown: (event: PointerEvent) => {
|
|
108
|
+
if (event.pointerType !== "touch" && event.pointerType !== "pen") return;
|
|
109
|
+
arm();
|
|
110
|
+
},
|
|
111
|
+
};
|
|
112
|
+
|
|
103
113
|
export function useLongPress({
|
|
104
114
|
onLongPress,
|
|
105
115
|
isDisabled,
|
|
106
|
-
delayMs =
|
|
116
|
+
delayMs = LONG_PRESS_DELAY_MS,
|
|
107
117
|
accessibilityDescription,
|
|
108
118
|
}: UseLongPressOptions): UseLongPressResult {
|
|
109
|
-
const
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
// (Context-Menu key / Shift+F10) that is preceded by no press at all, keep
|
|
118
|
-
// the native menu.
|
|
119
|
-
const onPointerDown = useCallback((event: PointerEvent) => {
|
|
120
|
-
if (event.pointerType !== "touch" && event.pointerType !== "pen") return;
|
|
121
|
-
arm();
|
|
122
|
-
}, []);
|
|
119
|
+
const bind = useDrag(
|
|
120
|
+
(state) => {
|
|
121
|
+
if (!isHold(state)) return;
|
|
122
|
+
swallowReleaseClick();
|
|
123
|
+
onLongPress();
|
|
124
|
+
},
|
|
125
|
+
{ ...holdConfig(delayMs), enabled: !isDisabled },
|
|
126
|
+
);
|
|
123
127
|
|
|
124
128
|
return {
|
|
125
|
-
longPressProps: mergeProps(
|
|
129
|
+
longPressProps: mergeProps(bind(), touchMenuSuppressionProps, {
|
|
130
|
+
"aria-description": accessibilityDescription,
|
|
131
|
+
}),
|
|
126
132
|
};
|
|
127
133
|
}
|