@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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remit/ui",
3
- "version": "0.0.221",
3
+ "version": "0.0.222",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "src"
@@ -32,6 +32,8 @@
32
32
  "@lexical/table": "0.49.0",
33
33
  "@lexical/utils": "0.49.0",
34
34
  "@react-types/shared": "^3.36.0",
35
+ "@use-gesture/react": "^10.3.1",
36
+ "@use-gesture/vanilla": "^10.3.1",
35
37
  "clsx": "^2",
36
38
  "dompurify": "^3.4.7",
37
39
  "franc-min": "^6.2.0",
@@ -1,4 +1,13 @@
1
- import { useEffect, useMemo, useRef, useState } from "react";
1
+ import { DragGesture } from "@use-gesture/vanilla";
2
+ import { useContext, useEffect, useMemo, useRef, useState } from "react";
3
+ import { SWIPE_DRAG_CONFIG } from "../lib/swipe-config.js";
4
+ import {
5
+ isZoomed,
6
+ releasedSwipe,
7
+ SWIPE_EVENT,
8
+ SwipeSurface,
9
+ subscribeToZoom,
10
+ } from "../lib/use-swipe-navigation.js";
2
11
  import {
3
12
  type AuthorDeclarations,
4
13
  buildEmailSrcDoc,
@@ -207,6 +216,7 @@ export const IsolatedEmailFrame = ({
207
216
  }: IsolatedEmailFrameProps) => {
208
217
  const ref = useRef<HTMLIFrameElement>(null);
209
218
  const [height, setHeight] = useState(0);
219
+ const swipeSurface = useContext(SwipeSurface);
210
220
 
211
221
  const srcDoc = useMemo(
212
222
  () => buildEmailSrcDoc(html, variant, isDark, declares),
@@ -220,6 +230,7 @@ export const IsolatedEmailFrame = ({
220
230
  const measure = () => {
221
231
  const doc = iframe.contentDocument;
222
232
  if (!doc?.body) return;
233
+ syncTouchAction();
223
234
  const root = doc.documentElement;
224
235
  const scale = fitToFrame(doc.body);
225
236
  const next =
@@ -243,8 +254,25 @@ export const IsolatedEmailFrame = ({
243
254
  pendingFrame = requestAnimationFrame(measure);
244
255
  };
245
256
 
257
+ const claimsSideways = (doc: Document): boolean => {
258
+ const root = doc.documentElement;
259
+ return isZoomed() || root.scrollWidth > root.clientWidth + 1;
260
+ };
261
+
262
+ const syncTouchAction = () => {
263
+ const doc = iframe.contentDocument;
264
+ if (!swipeSurface || !doc?.body) return;
265
+ const touchAction = claimsSideways(doc) ? "auto" : "pan-y pinch-zoom";
266
+ doc.documentElement.style.touchAction = touchAction;
267
+ doc.body.style.touchAction = touchAction;
268
+ };
269
+
246
270
  let observer: ResizeObserver | undefined;
247
271
  let keyDoc: Document | undefined;
272
+ let gesture: DragGesture | undefined;
273
+ const unsubscribeZoom = swipeSurface
274
+ ? subscribeToZoom(syncTouchAction)
275
+ : undefined;
248
276
  const handleLoad = () => {
249
277
  measure();
250
278
  // The srcDoc is rebuilt whenever the mail, theme or treatment changes,
@@ -255,8 +283,30 @@ export const IsolatedEmailFrame = ({
255
283
  keyDoc?.removeEventListener("keydown", forwardKeyDown);
256
284
  keyDoc?.removeEventListener("load", measure, true);
257
285
  keyDoc = undefined;
286
+ gesture?.destroy();
287
+ gesture = undefined;
258
288
  const doc = iframe.contentDocument;
259
289
  if (!doc?.body) return;
290
+ syncTouchAction();
291
+ if (swipeSurface)
292
+ gesture = new DragGesture(
293
+ doc.documentElement,
294
+ (state) => {
295
+ const direction = releasedSwipe(state);
296
+ if (!direction) return;
297
+ iframe.dispatchEvent(
298
+ new CustomEvent(SWIPE_EVENT, {
299
+ bubbles: true,
300
+ detail: direction,
301
+ }),
302
+ );
303
+ },
304
+ {
305
+ ...SWIPE_DRAG_CONFIG,
306
+ axis: "x",
307
+ window: doc.defaultView ?? undefined,
308
+ },
309
+ );
260
310
  observer = new ResizeObserver(measureNextFrame);
261
311
  observer.observe(doc.body);
262
312
  if (doc.documentElement) observer.observe(doc.documentElement);
@@ -278,9 +328,11 @@ export const IsolatedEmailFrame = ({
278
328
  keyDoc?.removeEventListener("keydown", forwardKeyDown);
279
329
  keyDoc?.removeEventListener("load", measure, true);
280
330
  observer?.disconnect();
331
+ gesture?.destroy();
332
+ unsubscribeZoom?.();
281
333
  cancelAnimationFrame(pendingFrame);
282
334
  };
283
- }, []);
335
+ }, [swipeSurface]);
284
336
 
285
337
  return (
286
338
  <iframe
@@ -114,12 +114,12 @@ describe("MobileReadingPane", () => {
114
114
  assert.doesNotMatch(html, /title="Reply \(r\)"/);
115
115
  });
116
116
 
117
- it("keeps pinch-zoom enabled on the scroll area when swipe handlers are set", () => {
117
+ it("keeps pinch-zoom enabled on the scroll area when swipe is wired", () => {
118
118
  const html = renderToString(
119
119
  createElement(MobileReadingPane, {
120
120
  thread,
121
121
  onBack: () => undefined,
122
- touchHandlers: { onTouchStart: () => undefined },
122
+ onSwipeNext: () => undefined,
123
123
  }),
124
124
  );
125
125
  assert.match(html, /touch-action:\s*pan-y pinch-zoom/);
@@ -0,0 +1,78 @@
1
+ import type { Meta, StoryObj } from "@storybook/react";
2
+ import { useState } from "react";
3
+ import { expect, waitFor, within } from "storybook/test";
4
+ import { MobileReadingPane } from "./mobile-reading-pane.js";
5
+
6
+ const subjects = ["First message", "Second message", "Third message"];
7
+
8
+ const meta: Meta<typeof MobileReadingPane> = {
9
+ title: "Design System/Primitives/MobileReadingPane",
10
+ component: MobileReadingPane,
11
+ parameters: { layout: "fullscreen" },
12
+ };
13
+ export default meta;
14
+
15
+ type Story = StoryObj<typeof MobileReadingPane>;
16
+
17
+ const SwipeBetweenMessages = () => {
18
+ const [index, setIndex] = useState(1);
19
+ return (
20
+ <div className="h-96 max-w-md">
21
+ <MobileReadingPane
22
+ thread={{ subject: subjects[index] ?? "", messages: [] }}
23
+ onBack={() => undefined}
24
+ onSwipeNext={
25
+ index < subjects.length - 1 ? () => setIndex(index + 1) : undefined
26
+ }
27
+ onSwipePrevious={index > 0 ? () => setIndex(index - 1) : undefined}
28
+ >
29
+ <p className="p-4">Swipe the body left or right.</p>
30
+ </MobileReadingPane>
31
+ </div>
32
+ );
33
+ };
34
+
35
+ const swipe = (area: Element, fromX: number, toX: number) => {
36
+ const touch = (type: string, x: number) =>
37
+ area.dispatchEvent(
38
+ new PointerEvent(type, {
39
+ bubbles: true,
40
+ buttons: type === "pointerup" ? 0 : 1,
41
+ pointerType: "touch",
42
+ pointerId: 1,
43
+ clientX: x,
44
+ clientY: 200,
45
+ }),
46
+ );
47
+ touch("pointerdown", fromX);
48
+ touch("pointermove", (fromX + toX) / 2);
49
+ touch("pointermove", toX);
50
+ touch("pointerup", toX);
51
+ };
52
+
53
+ export const SwipeToOpenNextAndPrevious: Story = {
54
+ name: "Swipe to open next and previous (interactive)",
55
+ render: () => <SwipeBetweenMessages />,
56
+ play: async ({ canvasElement }) => {
57
+ const canvas = within(canvasElement);
58
+ const area = canvasElement.querySelector("article > div.overflow-y-auto");
59
+ if (!area) throw new Error("reading area did not render");
60
+ await expect(canvas.getByRole("heading")).toHaveTextContent(
61
+ "Second message",
62
+ );
63
+
64
+ swipe(area, 300, 100);
65
+ await waitFor(() =>
66
+ expect(canvas.getByRole("heading")).toHaveTextContent("Third message"),
67
+ );
68
+
69
+ swipe(area, 100, 300);
70
+ await waitFor(() =>
71
+ expect(canvas.getByRole("heading")).toHaveTextContent("Second message"),
72
+ );
73
+ swipe(area, 100, 300);
74
+ await waitFor(() =>
75
+ expect(canvas.getByRole("heading")).toHaveTextContent("First message"),
76
+ );
77
+ },
78
+ };
@@ -1,6 +1,10 @@
1
1
  import { ArrowLeft, Info } from "lucide-react";
2
- import { type HTMLAttributes, type ReactNode, useState } from "react";
2
+ import { type ReactNode, useState } from "react";
3
3
  import { cn } from "../lib/cn.js";
4
+ import {
5
+ SwipeSurface,
6
+ useSwipeNavigation,
7
+ } from "../lib/use-swipe-navigation.js";
4
8
  import type { ThreadData } from "./app-shell-types.js";
5
9
  import { Button } from "./button.js";
6
10
  import { Dialog } from "./dialog.js";
@@ -49,8 +53,10 @@ export interface MobileReadingPaneProps {
49
53
  * still drives the top bar.
50
54
  */
51
55
  children?: ReactNode;
52
- /** Touch handlers attached to the scroll area (swipe-between-messages). */
53
- touchHandlers?: HTMLAttributes<HTMLDivElement>;
56
+ /** Swipe left on the reading area: open the next message. */
57
+ onSwipeNext?: () => void;
58
+ /** Swipe right on the reading area: open the previous message. */
59
+ onSwipePrevious?: () => void;
54
60
  }
55
61
 
56
62
  /**
@@ -71,8 +77,13 @@ export function MobileReadingPane({
71
77
  onToggleIntelligence,
72
78
  actions,
73
79
  children,
74
- touchHandlers,
80
+ onSwipeNext,
81
+ onSwipePrevious,
75
82
  }: MobileReadingPaneProps) {
83
+ const swipe = useSwipeNavigation({
84
+ onSwipeLeft: onSwipeNext,
85
+ onSwipeRight: onSwipePrevious,
86
+ });
76
87
  const [localIntelligenceOpen, setLocalIntelligenceOpen] = useState(false);
77
88
  const controlled = onToggleIntelligence !== undefined;
78
89
  const isOpen = controlled
@@ -129,49 +140,52 @@ export function MobileReadingPane({
129
140
  )}
130
141
  </header>
131
142
 
132
- <div
133
- className="flex-1 overflow-y-auto"
134
- style={touchHandlers ? { touchAction: "pan-y pinch-zoom" } : undefined}
135
- {...touchHandlers}
136
- >
137
- {/* Newest first, as on the wide pane. */}
138
- {children ??
139
- [...thread.messages].reverse().map((message) => {
140
- const bind = (handler?: (id: string) => void) =>
141
- handler ? () => handler(message.id) : undefined;
143
+ <SwipeSurface.Provider value={swipe.enabled}>
144
+ <div
145
+ ref={swipe.ref}
146
+ className="flex-1 overflow-y-auto"
147
+ style={{ touchAction: swipe.touchAction }}
148
+ {...swipe.bind()}
149
+ >
150
+ {/* Newest first, as on the wide pane. */}
151
+ {children ??
152
+ [...thread.messages].reverse().map((message) => {
153
+ const bind = (handler?: (id: string) => void) =>
154
+ handler ? () => handler(message.id) : undefined;
155
+
156
+ if (!message.expanded) {
157
+ return (
158
+ <CollapsedMessage
159
+ key={message.id}
160
+ message={message}
161
+ onClick={bind(actions?.onToggleExpand)}
162
+ />
163
+ );
164
+ }
142
165
 
143
- if (!message.expanded) {
144
166
  return (
145
- <CollapsedMessage
167
+ <ExpandedMessage
146
168
  key={message.id}
147
169
  message={message}
148
- onClick={bind(actions?.onToggleExpand)}
170
+ warning={thread.warning}
171
+ onHeaderClick={bind(actions?.onToggleExpand)}
172
+ actionBar={
173
+ <MobileMessageActionBar
174
+ hasThread
175
+ onReply={bind(actions?.onReply)}
176
+ onReplyAll={bind(actions?.onReplyAll)}
177
+ onForward={bind(actions?.onForward)}
178
+ onToggleStar={bind(actions?.onToggleStar)}
179
+ onDelete={bind(actions?.onDelete)}
180
+ onToggleRead={bind(actions?.onToggleRead)}
181
+ moveSlot={actions?.moveSlot?.(message.id)}
182
+ />
183
+ }
149
184
  />
150
185
  );
151
- }
152
-
153
- return (
154
- <ExpandedMessage
155
- key={message.id}
156
- message={message}
157
- warning={thread.warning}
158
- onHeaderClick={bind(actions?.onToggleExpand)}
159
- actionBar={
160
- <MobileMessageActionBar
161
- hasThread
162
- onReply={bind(actions?.onReply)}
163
- onReplyAll={bind(actions?.onReplyAll)}
164
- onForward={bind(actions?.onForward)}
165
- onToggleStar={bind(actions?.onToggleStar)}
166
- onDelete={bind(actions?.onDelete)}
167
- onToggleRead={bind(actions?.onToggleRead)}
168
- moveSlot={actions?.moveSlot?.(message.id)}
169
- />
170
- }
171
- />
172
- );
173
- })}
174
- </div>
186
+ })}
187
+ </div>
188
+ </SwipeSurface.Provider>
175
189
 
176
190
  {intelligence && !controlled && (
177
191
  <Dialog
@@ -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; // default react-aria threshold (500ms) + margin
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 (react-aria long press + axis arbitration)", () => {
157
- it("fires onLongPress on an unmoved press, with no spurious onOpen", async () => {
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
- // The drag claimed the axis but never reached the escape distance, so the
286
- // press was still live, and react-aria synthesizes a click for an
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: () => undefined,
292
- onPeek: () => undefined,
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); // dx=20 — past the axis threshold, under the escape
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(clicks, 0);
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 — react-aria fires the long press for both) and it
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 swipe only takes the gesture once it has
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,