@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.
@@ -1,6 +1,6 @@
1
- import type { DOMAttributes } from "@react-types/shared";
2
- import { type PointerEvent, useCallback } from "react";
3
- import { mergeProps, useLongPress as useAriaLongPress } from "react-aria";
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 — and react-aria's own long-press timer — can cancel the
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
- /** Spread onto the pressable element (anchor, button, or row container). */
75
- longPressProps: DOMAttributes;
92
+ longPressProps: HTMLAttributes<HTMLElement>;
76
93
  }
77
94
 
78
- /**
79
- * Long-press detection backed by react-aria's `useLongPress`. Owns
80
- * `contextmenu` suppression and iOS text-selection suppression, and treats
81
- * `<a href>` targets specially so link navigation and middle-click survive
82
- * outside the press. It does not, and cannot, suppress iOS's native callout
83
- * (share sheet) on an anchor — that still requires
84
- * `-webkit-touch-callout: none` in CSS at the call site, since iOS fires no
85
- * cancelable event for it.
86
- *
87
- * The `contextmenu` suppression is armed by a touch or pen `pointerdown` and
88
- * runs on the document in the capture phase: a touch press suppresses the menu
89
- * Android Chrome and iOS Safari raise on a long press over a link, while a mouse
90
- * right-click is left alone so the desktop context menu keeps working. Two
91
- * reasons it is neither react-aria's own suppression nor a handler on the row.
92
- * react-aria's listener is transient — added on press start, scoped to the
93
- * touched node, torn down shortly after pointerup — so a press ended early by
94
- * the swipe gesture's axis arbitration slips past it. And the row itself does
95
- * not survive the press: the long press enters selection mode, which replaces
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 = 500,
116
+ delayMs = LONG_PRESS_DELAY_MS,
107
117
  accessibilityDescription,
108
118
  }: UseLongPressOptions): UseLongPressResult {
109
- const { longPressProps } = useAriaLongPress({
110
- isDisabled,
111
- threshold: delayMs,
112
- accessibilityDescription,
113
- onLongPress,
114
- });
115
-
116
- // Only a touch or pen press arms it: a mouse right-click, and a keyboard menu
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(longPressProps, { onPointerDown }),
129
+ longPressProps: mergeProps(bind(), touchMenuSuppressionProps, {
130
+ "aria-description": accessibilityDescription,
131
+ }),
126
132
  };
127
133
  }
@@ -0,0 +1,162 @@
1
+ import "@remit/test-dom";
2
+ import assert from "node:assert/strict";
3
+ import { afterEach, beforeEach, describe, it } from "node:test";
4
+ import { act, createElement } from "react";
5
+ import { createRoot, type Root } from "react-dom/client";
6
+ import { MobileReadingPane } from "../components/mobile-reading-pane.js";
7
+ import { SWIPE_EVENT } from "./use-swipe-navigation.js";
8
+
9
+ let container: HTMLElement;
10
+ let root: Root;
11
+
12
+ beforeEach(() => {
13
+ container = document.getElementById("root") as unknown as HTMLElement;
14
+ container.innerHTML = "";
15
+ root = createRoot(container);
16
+ });
17
+
18
+ afterEach(() => {
19
+ act(() => {
20
+ root.unmount();
21
+ });
22
+ Object.defineProperty(window, "visualViewport", {
23
+ value: undefined,
24
+ configurable: true,
25
+ });
26
+ });
27
+
28
+ interface Navigation {
29
+ next: number;
30
+ previous: number;
31
+ }
32
+
33
+ function mount(): { area: HTMLElement; navigation: Navigation } {
34
+ const navigation: Navigation = { next: 0, previous: 0 };
35
+ act(() => {
36
+ root.render(
37
+ createElement(
38
+ MobileReadingPane,
39
+ {
40
+ thread: { subject: "Subject", messages: [] },
41
+ onBack: () => undefined,
42
+ onSwipeNext: () => navigation.next++,
43
+ onSwipePrevious: () => navigation.previous++,
44
+ },
45
+ createElement("p", { id: "body" }, "body"),
46
+ ),
47
+ );
48
+ });
49
+ const body = document.getElementById("body");
50
+ assert.ok(body?.parentElement, "reading area did not mount");
51
+ return { area: body.parentElement, navigation };
52
+ }
53
+
54
+ function touch(
55
+ target: Element,
56
+ type: "pointerdown" | "pointermove" | "pointerup",
57
+ x: number,
58
+ y: number,
59
+ pointerType = "touch",
60
+ ) {
61
+ act(() => {
62
+ target.dispatchEvent(
63
+ new PointerEvent(type, {
64
+ bubbles: true,
65
+ buttons: type === "pointerup" ? 0 : 1,
66
+ pointerType,
67
+ pointerId: 1,
68
+ clientX: x,
69
+ clientY: y,
70
+ }),
71
+ );
72
+ });
73
+ }
74
+
75
+ function drag(
76
+ target: Element,
77
+ from: [number, number],
78
+ to: [number, number],
79
+ pointerType = "touch",
80
+ ) {
81
+ touch(target, "pointerdown", from[0], from[1], pointerType);
82
+ touch(target, "pointermove", to[0] / 2 + from[0] / 2, from[1], pointerType);
83
+ touch(target, "pointermove", to[0], to[1], pointerType);
84
+ touch(target, "pointerup", to[0], to[1], pointerType);
85
+ }
86
+
87
+ describe("reading pane swipe", () => {
88
+ it("opens the next message on a swipe left", () => {
89
+ const { area, navigation } = mount();
90
+
91
+ drag(area, [300, 200], [100, 205]);
92
+
93
+ assert.deepEqual(navigation, { next: 1, previous: 0 });
94
+ });
95
+
96
+ it("opens the previous message on a swipe right", () => {
97
+ const { area, navigation } = mount();
98
+
99
+ drag(area, [100, 200], [300, 195]);
100
+
101
+ assert.deepEqual(navigation, { next: 0, previous: 1 });
102
+ });
103
+
104
+ it("ignores a drag shorter than the swipe distance", () => {
105
+ const { area, navigation } = mount();
106
+
107
+ drag(area, [200, 200], [170, 200]);
108
+
109
+ assert.deepEqual(navigation, { next: 0, previous: 0 });
110
+ });
111
+
112
+ it("ignores a mostly vertical drag, which is a scroll", () => {
113
+ const { area, navigation } = mount();
114
+
115
+ drag(area, [200, 100], [140, 500]);
116
+
117
+ assert.deepEqual(navigation, { next: 0, previous: 0 });
118
+ });
119
+
120
+ it("ignores a mouse drag", () => {
121
+ const { area, navigation } = mount();
122
+
123
+ drag(area, [300, 200], [100, 200], "mouse");
124
+
125
+ assert.deepEqual(navigation, { next: 0, previous: 0 });
126
+ });
127
+
128
+ it("opens the next message on a swipe raised inside the message frame", () => {
129
+ const { area, navigation } = mount();
130
+
131
+ act(() => {
132
+ area.dispatchEvent(
133
+ new CustomEvent(SWIPE_EVENT, { bubbles: true, detail: "left" }),
134
+ );
135
+ });
136
+
137
+ assert.deepEqual(navigation, { next: 1, previous: 0 });
138
+ });
139
+
140
+ it("leaves the browser to pan and pinch while the page is zoomed in", () => {
141
+ Object.defineProperty(window, "visualViewport", {
142
+ value: {
143
+ scale: 2,
144
+ addEventListener: () => undefined,
145
+ removeEventListener: () => undefined,
146
+ },
147
+ configurable: true,
148
+ });
149
+ const { area, navigation } = mount();
150
+
151
+ drag(area, [300, 200], [100, 200]);
152
+
153
+ assert.deepEqual(navigation, { next: 0, previous: 0 });
154
+ assert.equal(area.style.touchAction, "auto");
155
+ });
156
+
157
+ it("keeps native pinch-zoom enabled at rest", () => {
158
+ const { area } = mount();
159
+
160
+ assert.equal(area.style.touchAction, "pan-y pinch-zoom");
161
+ });
162
+ });
@@ -0,0 +1,90 @@
1
+ import { useDrag } from "@use-gesture/react";
2
+ import type { FullGestureState } from "@use-gesture/vanilla";
3
+ import {
4
+ createContext,
5
+ type RefObject,
6
+ useEffect,
7
+ useRef,
8
+ useSyncExternalStore,
9
+ } from "react";
10
+ import { SWIPE_DRAG_CONFIG } from "./swipe-config.js";
11
+
12
+ export type SwipeDirection = "left" | "right";
13
+
14
+ export const SWIPE_EVENT = "remit:swipe";
15
+
16
+ export const SwipeSurface = createContext(false);
17
+
18
+ const ZOOMED_SCALE = 1.01;
19
+
20
+ export const releasedSwipe = (
21
+ state: FullGestureState<"drag">,
22
+ ): SwipeDirection | null => {
23
+ const { event } = state;
24
+ if ("pointerType" in event && event.pointerType !== "touch") return null;
25
+ if (event.type !== "pointerup") return null;
26
+ const sign = state.swipe[0];
27
+ if (sign < 0) return "left";
28
+ if (sign > 0) return "right";
29
+ return null;
30
+ };
31
+
32
+ export const subscribeToZoom = (notify: () => void): (() => void) => {
33
+ const viewport = window.visualViewport;
34
+ viewport?.addEventListener("resize", notify);
35
+ return () => viewport?.removeEventListener("resize", notify);
36
+ };
37
+
38
+ export const isZoomed = (): boolean =>
39
+ (window.visualViewport?.scale ?? 1) > ZOOMED_SCALE;
40
+
41
+ interface UseSwipeNavigationOptions {
42
+ onSwipeLeft?: () => void;
43
+ onSwipeRight?: () => void;
44
+ }
45
+
46
+ interface SwipeNavigation {
47
+ ref: RefObject<HTMLDivElement | null>;
48
+ bind: ReturnType<typeof useDrag>;
49
+ touchAction: "pan-y pinch-zoom" | "auto";
50
+ enabled: boolean;
51
+ }
52
+
53
+ export const useSwipeNavigation = ({
54
+ onSwipeLeft,
55
+ onSwipeRight,
56
+ }: UseSwipeNavigationOptions): SwipeNavigation => {
57
+ const ref = useRef<HTMLDivElement>(null);
58
+ const swipeEnabled = onSwipeLeft !== undefined || onSwipeRight !== undefined;
59
+ const zoomed = useSyncExternalStore(subscribeToZoom, isZoomed, () => false);
60
+
61
+ const go = (direction: SwipeDirection | null): void => {
62
+ if (direction === "left") onSwipeLeft?.();
63
+ if (direction === "right") onSwipeRight?.();
64
+ };
65
+
66
+ const bind = useDrag((state) => go(releasedSwipe(state)), {
67
+ ...SWIPE_DRAG_CONFIG,
68
+ axis: "x",
69
+ enabled: swipeEnabled && !zoomed,
70
+ });
71
+
72
+ useEffect(() => {
73
+ const element = ref.current;
74
+ if (!element || !swipeEnabled || zoomed) return;
75
+ const onFrameSwipe = (event: Event) => {
76
+ if (!(event instanceof CustomEvent)) return;
77
+ const { detail } = event;
78
+ if (detail === "left" || detail === "right") go(detail);
79
+ };
80
+ element.addEventListener(SWIPE_EVENT, onFrameSwipe);
81
+ return () => element.removeEventListener(SWIPE_EVENT, onFrameSwipe);
82
+ });
83
+
84
+ return {
85
+ ref,
86
+ bind,
87
+ touchAction: swipeEnabled && !zoomed ? "pan-y pinch-zoom" : "auto",
88
+ enabled: swipeEnabled,
89
+ };
90
+ };