@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.
@@ -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
+ };