@remit/ui 0.0.215 → 0.0.216
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
|
@@ -0,0 +1,98 @@
|
|
|
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 {
|
|
7
|
+
CalendarSuggestionDeck,
|
|
8
|
+
type CalendarSuggestionDeckProps,
|
|
9
|
+
} from "./calendar-suggestion-deck.js";
|
|
10
|
+
|
|
11
|
+
let container: HTMLElement;
|
|
12
|
+
let root: Root;
|
|
13
|
+
let captured: number[];
|
|
14
|
+
let confirmed: number;
|
|
15
|
+
let pressed: number;
|
|
16
|
+
|
|
17
|
+
const originalSet = HTMLElement.prototype.setPointerCapture;
|
|
18
|
+
const originalHas = HTMLElement.prototype.hasPointerCapture;
|
|
19
|
+
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
captured = [];
|
|
22
|
+
confirmed = 0;
|
|
23
|
+
pressed = 0;
|
|
24
|
+
HTMLElement.prototype.setPointerCapture = (pointerId: number) => {
|
|
25
|
+
captured.push(pointerId);
|
|
26
|
+
};
|
|
27
|
+
HTMLElement.prototype.hasPointerCapture = (pointerId: number) =>
|
|
28
|
+
captured.includes(pointerId);
|
|
29
|
+
const deckProps: CalendarSuggestionDeckProps = {
|
|
30
|
+
hasCard: true,
|
|
31
|
+
remaining: 1,
|
|
32
|
+
blocked: false,
|
|
33
|
+
blockedReason: "",
|
|
34
|
+
onConfirm: () => {
|
|
35
|
+
confirmed += 1;
|
|
36
|
+
},
|
|
37
|
+
onReject: () => undefined,
|
|
38
|
+
children: createElement(
|
|
39
|
+
"button",
|
|
40
|
+
{
|
|
41
|
+
type: "button",
|
|
42
|
+
onClick: () => {
|
|
43
|
+
pressed += 1;
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
"Change first",
|
|
47
|
+
),
|
|
48
|
+
};
|
|
49
|
+
container = document.createElement("div");
|
|
50
|
+
document.body.append(container);
|
|
51
|
+
root = createRoot(container);
|
|
52
|
+
act(() => {
|
|
53
|
+
root.render(createElement(CalendarSuggestionDeck, deckProps));
|
|
54
|
+
});
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
afterEach(() => {
|
|
58
|
+
act(() => root.unmount());
|
|
59
|
+
container.remove();
|
|
60
|
+
HTMLElement.prototype.setPointerCapture = originalSet;
|
|
61
|
+
HTMLElement.prototype.hasPointerCapture = originalHas;
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const pointer = (target: Element, type: string, clientX: number) => {
|
|
65
|
+
act(() => {
|
|
66
|
+
target.dispatchEvent(
|
|
67
|
+
new PointerEvent(type, { bubbles: true, clientX, pointerId: 1 }),
|
|
68
|
+
);
|
|
69
|
+
});
|
|
70
|
+
};
|
|
71
|
+
|
|
72
|
+
const button = (): HTMLButtonElement => {
|
|
73
|
+
const found = container.querySelector("button");
|
|
74
|
+
if (!found) throw new Error("the card's button is not drawn");
|
|
75
|
+
return found;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
describe("pressing a button on the top card", () => {
|
|
79
|
+
it("leaves the pointer to the button, so the press reaches it", () => {
|
|
80
|
+
pointer(button(), "pointerdown", 100);
|
|
81
|
+
pointer(button(), "pointerup", 100);
|
|
82
|
+
act(() => button().click());
|
|
83
|
+
|
|
84
|
+
assert.deepEqual(captured, []);
|
|
85
|
+
assert.equal(pressed, 1);
|
|
86
|
+
assert.equal(confirmed, 0);
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
it("still takes the pointer once the card is dragged, and a long drag confirms", () => {
|
|
90
|
+
pointer(button(), "pointerdown", 100);
|
|
91
|
+
pointer(button(), "pointermove", 150);
|
|
92
|
+
pointer(button(), "pointermove", 220);
|
|
93
|
+
pointer(button(), "pointerup", 220);
|
|
94
|
+
|
|
95
|
+
assert.deepEqual(captured, [1]);
|
|
96
|
+
assert.equal(confirmed, 1);
|
|
97
|
+
});
|
|
98
|
+
});
|
|
@@ -9,6 +9,7 @@ import { cn } from "../lib/cn.js";
|
|
|
9
9
|
*/
|
|
10
10
|
|
|
11
11
|
const COMMIT_DISTANCE = 96;
|
|
12
|
+
const DRAG_SLOP = 6;
|
|
12
13
|
|
|
13
14
|
export interface CalendarSuggestionDeckProps {
|
|
14
15
|
children: ReactNode;
|
|
@@ -68,12 +69,17 @@ export function CalendarSuggestionDeck({
|
|
|
68
69
|
transition: from === null ? "transform 180ms ease-out" : "none",
|
|
69
70
|
}}
|
|
70
71
|
onPointerDown={(e) => {
|
|
71
|
-
e.currentTarget.setPointerCapture(e.pointerId);
|
|
72
72
|
setFrom(e.clientX);
|
|
73
73
|
}}
|
|
74
74
|
onPointerMove={(e) => {
|
|
75
75
|
if (from === null) return;
|
|
76
|
-
|
|
76
|
+
const moved = e.clientX - from;
|
|
77
|
+
if (
|
|
78
|
+
Math.abs(moved) > DRAG_SLOP &&
|
|
79
|
+
!e.currentTarget.hasPointerCapture(e.pointerId)
|
|
80
|
+
)
|
|
81
|
+
e.currentTarget.setPointerCapture(e.pointerId);
|
|
82
|
+
setDrag(moved);
|
|
77
83
|
}}
|
|
78
84
|
onPointerUp={() => {
|
|
79
85
|
if (Math.abs(drag) > COMMIT_DISTANCE) {
|