@msobiecki/react-marauders-path 1.29.0 → 1.30.0
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/README.md +99 -16
- package/dist/index.d.ts +98 -8
- package/dist/index.js +39 -29
- package/dist/index.js.map +1 -1
- package/dist/use-double-tap/event-guards.js +7 -0
- package/dist/use-double-tap/event-guards.js.map +1 -0
- package/dist/use-double-tap/use-double-tap.js +32 -26
- package/dist/use-double-tap/use-double-tap.js.map +1 -1
- package/dist/use-drag/event-guards.js +7 -0
- package/dist/use-drag/event-guards.js.map +1 -0
- package/dist/use-drag/use-drag.js +72 -53
- package/dist/use-drag/use-drag.js.map +1 -1
- package/dist/use-drag/use-drag.types.js.map +1 -1
- package/dist/use-key/event-guards.js +1 -1
- package/dist/use-key/event-guards.js.map +1 -1
- package/dist/use-key/use-key.js +48 -51
- package/dist/use-key/use-key.js.map +1 -1
- package/dist/use-key/use-key.types.js.map +1 -1
- package/dist/use-mouse/use-mouse.js +100 -0
- package/dist/use-mouse/use-mouse.js.map +1 -0
- package/dist/use-mouse/use-mouse.types.js +23 -0
- package/dist/use-mouse/use-mouse.types.js.map +1 -0
- package/dist/use-pinch/event-guards.js +7 -0
- package/dist/use-pinch/event-guards.js.map +1 -0
- package/dist/use-pinch/use-pinch.js +93 -70
- package/dist/use-pinch/use-pinch.js.map +1 -1
- package/dist/use-pinch/use-pinch.types.js.map +1 -1
- package/dist/use-pointer/event-guards.js +7 -0
- package/dist/use-pointer/event-guards.js.map +1 -0
- package/dist/use-pointer/invoke-pointer-action.js +7 -0
- package/dist/use-pointer/invoke-pointer-action.js.map +1 -0
- package/dist/use-pointer/use-pointer.js +74 -0
- package/dist/use-pointer/use-pointer.js.map +1 -0
- package/dist/use-pointer/use-pointer.types.js +19 -0
- package/dist/use-pointer/use-pointer.types.js.map +1 -0
- package/dist/use-press/event-guards.js +7 -0
- package/dist/use-press/event-guards.js.map +1 -0
- package/dist/use-press/use-press.js +83 -57
- package/dist/use-press/use-press.js.map +1 -1
- package/dist/use-swipe/event-guards.js +7 -0
- package/dist/use-swipe/event-guards.js.map +1 -0
- package/dist/use-swipe/use-swipe.js +81 -62
- package/dist/use-swipe/use-swipe.js.map +1 -1
- package/dist/use-swipe/use-swipe.types.js.map +1 -1
- package/dist/use-tap/event-guards.js +7 -0
- package/dist/use-tap/event-guards.js.map +1 -0
- package/dist/use-tap/use-tap.js +66 -48
- package/dist/use-tap/use-tap.js.map +1 -1
- package/dist/use-wheel/use-wheel.js +1 -1
- package/dist/use-wheel/use-wheel.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,98 +1,124 @@
|
|
|
1
|
-
import { useRef as
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
import { useRef as u, useCallback as s, useEffect as M } from "react";
|
|
2
|
+
import { PressEventPointerTypes as P } from "./use-press.types.js";
|
|
3
|
+
import { invokePressAction as O } from "./invoke-press-action.js";
|
|
4
|
+
import { shouldHandleEvent as p } from "./event-guards.js";
|
|
5
|
+
const X = {
|
|
6
|
+
eventPointerTypes: [
|
|
7
|
+
P.Touch,
|
|
8
|
+
P.Mouse,
|
|
9
|
+
P.Pen
|
|
10
|
+
],
|
|
5
11
|
eventCapture: !1,
|
|
6
12
|
eventOnce: !1,
|
|
7
13
|
eventStopImmediatePropagation: !1,
|
|
8
14
|
delay: 500,
|
|
9
15
|
threshold: 8,
|
|
10
16
|
container: { current: null }
|
|
11
|
-
},
|
|
17
|
+
}, D = (v, R = {}) => {
|
|
12
18
|
const {
|
|
13
|
-
eventPointerTypes:
|
|
14
|
-
eventCapture:
|
|
15
|
-
eventOnce:
|
|
16
|
-
eventStopImmediatePropagation:
|
|
17
|
-
delay:
|
|
18
|
-
threshold:
|
|
19
|
-
container:
|
|
20
|
-
} = { ...
|
|
19
|
+
eventPointerTypes: r,
|
|
20
|
+
eventCapture: c,
|
|
21
|
+
eventOnce: h,
|
|
22
|
+
eventStopImmediatePropagation: E,
|
|
23
|
+
delay: y,
|
|
24
|
+
threshold: L,
|
|
25
|
+
container: T
|
|
26
|
+
} = { ...X, ...R }, i = u(null), a = u(null), f = u({
|
|
21
27
|
x: 0,
|
|
22
28
|
y: 0,
|
|
23
29
|
active: !1
|
|
24
|
-
}),
|
|
25
|
-
|
|
26
|
-
}, []),
|
|
30
|
+
}), l = u(null), n = s(() => {
|
|
31
|
+
l.current !== null && (clearTimeout(l.current), l.current = null), f.current.active = !1;
|
|
32
|
+
}, []), C = s(
|
|
27
33
|
(e) => {
|
|
28
|
-
|
|
34
|
+
p(e, {
|
|
35
|
+
eventPointerTypes: r
|
|
36
|
+
}) && (f.current = {
|
|
29
37
|
x: e.clientX,
|
|
30
38
|
y: e.clientY,
|
|
31
39
|
active: !0
|
|
32
|
-
},
|
|
33
|
-
const
|
|
40
|
+
}, l.current = setTimeout(() => {
|
|
41
|
+
const o = {
|
|
34
42
|
x: e.clientX,
|
|
35
43
|
y: e.clientY
|
|
36
44
|
};
|
|
37
|
-
|
|
38
|
-
stopImmediate:
|
|
39
|
-
once:
|
|
45
|
+
O(e, o, v, {
|
|
46
|
+
stopImmediate: E,
|
|
47
|
+
once: h,
|
|
40
48
|
onOnce: () => {
|
|
41
|
-
|
|
49
|
+
a.current?.abort();
|
|
42
50
|
}
|
|
43
|
-
}),
|
|
44
|
-
},
|
|
51
|
+
}), n();
|
|
52
|
+
}, y));
|
|
45
53
|
},
|
|
46
54
|
[
|
|
47
|
-
|
|
55
|
+
r,
|
|
48
56
|
h,
|
|
49
|
-
|
|
50
|
-
|
|
57
|
+
E,
|
|
58
|
+
y,
|
|
51
59
|
v,
|
|
52
|
-
|
|
60
|
+
n
|
|
53
61
|
]
|
|
54
|
-
),
|
|
62
|
+
), b = s(
|
|
55
63
|
(e) => {
|
|
56
|
-
|
|
57
|
-
|
|
64
|
+
if (!p(e, {
|
|
65
|
+
eventPointerTypes: r
|
|
66
|
+
}))
|
|
67
|
+
return;
|
|
68
|
+
const o = f.current;
|
|
69
|
+
if (!o.active)
|
|
58
70
|
return;
|
|
59
|
-
const
|
|
60
|
-
Math.hypot(
|
|
71
|
+
const d = e.clientX - o.x, m = e.clientY - o.y;
|
|
72
|
+
Math.hypot(d, m) > L && n();
|
|
61
73
|
},
|
|
62
|
-
[
|
|
63
|
-
),
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
74
|
+
[r, L, n]
|
|
75
|
+
), g = s(
|
|
76
|
+
(e) => {
|
|
77
|
+
p(e, {
|
|
78
|
+
eventPointerTypes: r
|
|
79
|
+
}) && n();
|
|
80
|
+
},
|
|
81
|
+
[r, n]
|
|
82
|
+
), x = s(
|
|
83
|
+
(e) => {
|
|
84
|
+
p(e, {
|
|
85
|
+
eventPointerTypes: r
|
|
86
|
+
}) && n();
|
|
87
|
+
},
|
|
88
|
+
[r, n]
|
|
89
|
+
);
|
|
90
|
+
M(() => {
|
|
91
|
+
i.current = T?.current ?? globalThis, a.current = new AbortController();
|
|
92
|
+
const { signal: e } = a.current, o = (t) => t instanceof PointerEvent && C(t), d = (t) => t instanceof PointerEvent && b(t), m = (t) => t instanceof PointerEvent && g(t), w = (t) => t instanceof PointerEvent && x(t);
|
|
93
|
+
return i.current.addEventListener(
|
|
68
94
|
"pointerdown",
|
|
69
|
-
|
|
70
|
-
{ capture:
|
|
71
|
-
),
|
|
95
|
+
o,
|
|
96
|
+
{ capture: c, signal: e }
|
|
97
|
+
), i.current.addEventListener(
|
|
72
98
|
"pointermove",
|
|
73
|
-
|
|
74
|
-
{ capture:
|
|
75
|
-
),
|
|
76
|
-
capture:
|
|
99
|
+
d,
|
|
100
|
+
{ capture: c, signal: e }
|
|
101
|
+
), i.current.addEventListener("pointerup", m, {
|
|
102
|
+
capture: c,
|
|
77
103
|
signal: e
|
|
78
|
-
}),
|
|
104
|
+
}), i.current.addEventListener(
|
|
79
105
|
"pointercancel",
|
|
80
|
-
|
|
81
|
-
{ capture:
|
|
106
|
+
w,
|
|
107
|
+
{ capture: c, signal: e }
|
|
82
108
|
), () => {
|
|
83
|
-
|
|
109
|
+
a.current?.abort(), n();
|
|
84
110
|
};
|
|
85
111
|
}, [
|
|
86
|
-
E,
|
|
87
|
-
o,
|
|
88
|
-
L,
|
|
89
|
-
C,
|
|
90
112
|
T,
|
|
113
|
+
c,
|
|
114
|
+
C,
|
|
91
115
|
b,
|
|
92
|
-
|
|
116
|
+
g,
|
|
117
|
+
x,
|
|
118
|
+
n
|
|
93
119
|
]);
|
|
94
120
|
};
|
|
95
121
|
export {
|
|
96
|
-
|
|
122
|
+
D as default
|
|
97
123
|
};
|
|
98
124
|
//# sourceMappingURL=use-press.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-press.js","sources":["../../src/use-press/use-press.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from \"react\";\nimport {\n PressOptions,\n
|
|
1
|
+
{"version":3,"file":"use-press.js","sources":["../../src/use-press/use-press.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from \"react\";\nimport {\n PressOptions,\n PressData,\n PressEventPointerTypes,\n UsePressCallback,\n UsePressOptions,\n} from \"./use-press.types\";\nimport { invokePressAction } from \"./invoke-press-action\";\nimport { shouldHandleEvent } from \"./event-guards\";\n\nconst defaultOptions: PressOptions = {\n eventPointerTypes: [\n PressEventPointerTypes.Touch,\n PressEventPointerTypes.Mouse,\n PressEventPointerTypes.Pen,\n ],\n eventCapture: false,\n eventOnce: false,\n eventStopImmediatePropagation: false,\n delay: 500,\n threshold: 8,\n container: { current: null },\n};\n\nconst usePress = (\n pressCallback: UsePressCallback,\n options: UsePressOptions = {},\n) => {\n const {\n eventPointerTypes,\n eventCapture,\n eventOnce,\n eventStopImmediatePropagation,\n delay,\n threshold,\n container,\n } = { ...defaultOptions, ...options };\n\n const targetReference = useRef<EventTarget | null>(null);\n const abortControllerReference = useRef<AbortController | null>(null);\n\n const pressStateReference = useRef<{\n x: number;\n y: number;\n active: boolean;\n }>({\n x: 0,\n y: 0,\n active: false,\n });\n\n const timerReference = useRef<ReturnType<typeof setTimeout> | null>(null);\n\n const clearPress = useCallback(() => {\n if (timerReference.current !== null) {\n clearTimeout(timerReference.current);\n timerReference.current = null;\n }\n pressStateReference.current.active = false;\n }, []);\n\n const handlePointerDown = useCallback(\n (event: PointerEvent) => {\n if (\n !shouldHandleEvent(event, {\n eventPointerTypes,\n })\n ) {\n return;\n }\n\n pressStateReference.current = {\n x: event.clientX,\n y: event.clientY,\n active: true,\n };\n\n timerReference.current = setTimeout(() => {\n const data: PressData = {\n x: event.clientX,\n y: event.clientY,\n };\n invokePressAction(event, data, pressCallback, {\n stopImmediate: eventStopImmediatePropagation,\n once: eventOnce,\n onOnce: () => {\n abortControllerReference.current?.abort();\n },\n });\n\n clearPress();\n }, delay);\n },\n [\n eventPointerTypes,\n eventOnce,\n eventStopImmediatePropagation,\n delay,\n pressCallback,\n clearPress,\n ],\n );\n\n const handlePointerMove = useCallback(\n (event: PointerEvent) => {\n if (\n !shouldHandleEvent(event, {\n eventPointerTypes,\n })\n ) {\n return;\n }\n\n const state = pressStateReference.current;\n if (!state.active) {\n return;\n }\n\n const deltaX = event.clientX - state.x;\n const deltaY = event.clientY - state.y;\n\n if (Math.hypot(deltaX, deltaY) > threshold) {\n clearPress();\n }\n },\n [eventPointerTypes, threshold, clearPress],\n );\n\n const handlePointerUp = useCallback(\n (event: PointerEvent) => {\n if (\n !shouldHandleEvent(event, {\n eventPointerTypes,\n })\n ) {\n return;\n }\n\n clearPress();\n },\n\n [eventPointerTypes, clearPress],\n );\n\n const handlePointerCancel = useCallback(\n (event: PointerEvent) => {\n if (\n !shouldHandleEvent(event, {\n eventPointerTypes,\n })\n ) {\n return;\n }\n\n clearPress();\n },\n [eventPointerTypes, clearPress],\n );\n\n useEffect(() => {\n targetReference.current = container?.current ?? globalThis;\n abortControllerReference.current = new AbortController();\n const { signal } = abortControllerReference.current;\n\n const pointerDownListener = (event: Event) =>\n event instanceof PointerEvent && handlePointerDown(event);\n\n const pointerMoveListener = (event: Event) =>\n event instanceof PointerEvent && handlePointerMove(event);\n\n const pointerUpListener = (event: Event) =>\n event instanceof PointerEvent && handlePointerUp(event);\n\n const pointerCancelListener = (event: Event) =>\n event instanceof PointerEvent && handlePointerCancel(event);\n\n targetReference.current.addEventListener(\n \"pointerdown\",\n pointerDownListener,\n { capture: eventCapture, signal },\n );\n\n targetReference.current.addEventListener(\n \"pointermove\",\n pointerMoveListener,\n { capture: eventCapture, signal },\n );\n\n targetReference.current.addEventListener(\"pointerup\", pointerUpListener, {\n capture: eventCapture,\n signal,\n });\n\n targetReference.current.addEventListener(\n \"pointercancel\",\n pointerCancelListener,\n { capture: eventCapture, signal },\n );\n\n return () => {\n abortControllerReference.current?.abort();\n clearPress();\n };\n }, [\n container,\n eventCapture,\n handlePointerDown,\n handlePointerMove,\n handlePointerUp,\n handlePointerCancel,\n clearPress,\n ]);\n};\n\nexport default usePress;\n"],"names":["defaultOptions","PressEventPointerTypes","usePress","pressCallback","options","eventPointerTypes","eventCapture","eventOnce","eventStopImmediatePropagation","delay","threshold","container","targetReference","useRef","abortControllerReference","pressStateReference","timerReference","clearPress","useCallback","handlePointerDown","event","shouldHandleEvent","data","invokePressAction","handlePointerMove","state","deltaX","deltaY","handlePointerUp","handlePointerCancel","useEffect","signal","pointerDownListener","pointerMoveListener","pointerUpListener","pointerCancelListener"],"mappings":";;;;AAWA,MAAMA,IAA+B;AAAA,EACnC,mBAAmB;AAAA,IACjBC,EAAuB;AAAA,IACvBA,EAAuB;AAAA,IACvBA,EAAuB;AAAA,EAAA;AAAA,EAEzB,cAAc;AAAA,EACd,WAAW;AAAA,EACX,+BAA+B;AAAA,EAC/B,OAAO;AAAA,EACP,WAAW;AAAA,EACX,WAAW,EAAE,SAAS,KAAA;AACxB,GAEMC,IAAW,CACfC,GACAC,IAA2B,OACxB;AACH,QAAM;AAAA,IACJ,mBAAAC;AAAA,IACA,cAAAC;AAAA,IACA,WAAAC;AAAA,IACA,+B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mB;AAAA,MACvE,SAAS5B;AAAA,MACT,QAAAyB;AAAA,IAAA,CACD,GAEDnB,EAAgB,QAAQ;AAAA,MACtB;AAAA,MACAuB;AAAA,MACA,EAAE,SAAS7B,GAAc,QAAAyB,EAAA;AAAA,IAAO,GAG3B,MAAM;AACX,MAAAjB,EAAyB,SAAS,MAAA,GAClCG,EAAA;AAAA,IACF;AAAA,EACF,GAAG;AAAA,IACDN;AAAA,IACAL;AAAA,IACAa;AAAA,IACAK;AAAA,IACAI;AAAA,IACAC;AAAA,IACAZ;AAAA,EAAA,CACD;AACH;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-guards.js","sources":["../../src/use-swipe/event-guards.ts"],"sourcesContent":["import { SwipeEventPointerType } from \"./use-swipe.types\";\n\n/**\n * Determines whether a pointer event should be handled based on event options.\n *\n * @param {PointerEvent} event - The pointer event to evaluate\n * @param {Object} options - Event handling options\n * @param {SwipeEventPointerType[]} options.eventPointerTypes - Allowed pointer types for the event\n * @returns {boolean} True if the event should be handled, false otherwise\n *\n * @example\n * const shouldHandle = shouldHandleEvent(event, {\n * eventPointerTypes: [SwipeEventPointerTypes.Touch]\n * });\n */\nexport const shouldHandleEvent = (\n event: PointerEvent,\n options: {\n eventPointerTypes: SwipeEventPointerType[];\n },\n) => {\n if (!event.isPrimary) {\n return false;\n }\n if (\n !options.eventPointerTypes.includes(\n event.pointerType as SwipeEventPointerType,\n )\n ) {\n return false;\n }\n\n return true;\n};\n"],"names":["shouldHandleEvent","event","options"],"mappings":"AAeO,MAAMA,IAAoB,CAC/BC,GACAC,MAII,GAACD,EAAM,aAIT,CAACC,EAAQ,kBAAkB;AAAA,EACzBD,EAAM;AAAA;"}
|
|
@@ -1,98 +1,117 @@
|
|
|
1
|
-
import { useRef as
|
|
2
|
-
import { SwipeDirections as
|
|
3
|
-
import { parseSwipeDirection as
|
|
4
|
-
import { invokeSwipeAction as
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { useRef as p, useCallback as s, useEffect as O } from "react";
|
|
2
|
+
import { SwipeDirections as r, SwipeEventPointerTypes as m } from "./use-swipe.types.js";
|
|
3
|
+
import { parseSwipeDirection as I } from "./parse-swipe-direction.js";
|
|
4
|
+
import { invokeSwipeAction as A } from "./invoke-swipe-action.js";
|
|
5
|
+
import { shouldHandleEvent as d } from "./event-guards.js";
|
|
6
|
+
const H = {
|
|
7
|
+
eventPointerTypes: [
|
|
8
|
+
m.Touch,
|
|
9
|
+
m.Mouse,
|
|
10
|
+
m.Pen
|
|
11
|
+
],
|
|
7
12
|
eventCapture: !1,
|
|
8
13
|
eventOnce: !1,
|
|
9
14
|
eventStopImmediatePropagation: !1,
|
|
10
15
|
threshold: 50,
|
|
11
16
|
velocity: 0.3,
|
|
12
17
|
container: { current: null }
|
|
13
|
-
},
|
|
18
|
+
}, q = (h, v, U = {}) => {
|
|
14
19
|
const {
|
|
15
|
-
eventPointerTypes:
|
|
20
|
+
eventPointerTypes: o,
|
|
16
21
|
eventCapture: a,
|
|
17
|
-
eventOnce:
|
|
18
|
-
eventStopImmediatePropagation:
|
|
19
|
-
threshold:
|
|
20
|
-
velocity:
|
|
21
|
-
container:
|
|
22
|
-
} = { ...
|
|
22
|
+
eventOnce: w,
|
|
23
|
+
eventStopImmediatePropagation: P,
|
|
24
|
+
threshold: D,
|
|
25
|
+
velocity: S,
|
|
26
|
+
container: b
|
|
27
|
+
} = { ...H, ...U }, E = p([]), u = p(null), l = p(null), f = p({
|
|
23
28
|
startX: 0,
|
|
24
29
|
startY: 0,
|
|
25
30
|
startTime: 0,
|
|
26
31
|
active: !1
|
|
27
|
-
}),
|
|
32
|
+
}), y = s(
|
|
28
33
|
(t, e) => {
|
|
29
|
-
const
|
|
30
|
-
return
|
|
34
|
+
const c = Math.abs(t), i = Math.abs(e);
|
|
35
|
+
return c > i ? t > 0 ? r.Right : r.Left : e > 0 ? r.Down : r.Up;
|
|
31
36
|
},
|
|
32
37
|
[]
|
|
33
|
-
),
|
|
34
|
-
const e =
|
|
35
|
-
return e.includes(
|
|
36
|
-
}, []),
|
|
38
|
+
), L = s((t) => {
|
|
39
|
+
const e = E.current;
|
|
40
|
+
return e.includes(r.Both) || e.includes(r.Horizontal) && (t === r.Left || t === r.Right) || e.includes(r.Vertical) && (t === r.Up || t === r.Down) ? !0 : e.includes(t);
|
|
41
|
+
}, []), T = s(
|
|
37
42
|
(t) => {
|
|
38
|
-
|
|
43
|
+
d(t, {
|
|
44
|
+
eventPointerTypes: o
|
|
45
|
+
}) && (f.current = {
|
|
39
46
|
startX: t.clientX,
|
|
40
47
|
startY: t.clientY,
|
|
41
48
|
startTime: Date.now(),
|
|
42
49
|
active: !0
|
|
43
50
|
});
|
|
44
51
|
},
|
|
45
|
-
[
|
|
46
|
-
),
|
|
52
|
+
[o]
|
|
53
|
+
), g = s(
|
|
47
54
|
(t) => {
|
|
48
|
-
|
|
55
|
+
if (!d(t, {
|
|
56
|
+
eventPointerTypes: o
|
|
57
|
+
}))
|
|
58
|
+
return;
|
|
59
|
+
const e = f.current;
|
|
49
60
|
if (!e.active)
|
|
50
61
|
return;
|
|
51
62
|
e.active = !1;
|
|
52
|
-
const
|
|
53
|
-
if (
|
|
63
|
+
const c = Date.now() - e.startTime;
|
|
64
|
+
if (c === 0)
|
|
54
65
|
return;
|
|
55
|
-
const
|
|
56
|
-
if (
|
|
66
|
+
const i = t.clientX - e.startX, n = t.clientY - e.startY, X = Math.hypot(i, n), Y = X / c;
|
|
67
|
+
if (X < D || Y < S)
|
|
57
68
|
return;
|
|
58
|
-
const
|
|
59
|
-
if (!
|
|
69
|
+
const M = y(i, n);
|
|
70
|
+
if (!L(M))
|
|
60
71
|
return;
|
|
61
|
-
|
|
62
|
-
deltaX:
|
|
63
|
-
deltaY:
|
|
64
|
-
velocity:
|
|
65
|
-
duration:
|
|
66
|
-
},
|
|
67
|
-
stopImmediate:
|
|
68
|
-
once:
|
|
72
|
+
A(t, M, {
|
|
73
|
+
deltaX: i,
|
|
74
|
+
deltaY: n,
|
|
75
|
+
velocity: Y,
|
|
76
|
+
duration: c
|
|
77
|
+
}, v, {
|
|
78
|
+
stopImmediate: P,
|
|
79
|
+
once: w,
|
|
69
80
|
onOnce: () => {
|
|
70
81
|
l.current?.abort();
|
|
71
82
|
}
|
|
72
83
|
});
|
|
73
84
|
},
|
|
74
85
|
[
|
|
86
|
+
o,
|
|
75
87
|
w,
|
|
76
|
-
|
|
77
|
-
|
|
88
|
+
P,
|
|
89
|
+
D,
|
|
78
90
|
S,
|
|
79
|
-
|
|
80
|
-
|
|
91
|
+
y,
|
|
92
|
+
L,
|
|
81
93
|
v
|
|
82
94
|
]
|
|
83
|
-
),
|
|
95
|
+
), R = s(
|
|
96
|
+
(t) => {
|
|
97
|
+
d(t, {
|
|
98
|
+
eventPointerTypes: o
|
|
99
|
+
}) && g(t);
|
|
100
|
+
},
|
|
101
|
+
[o, g]
|
|
102
|
+
), C = s(
|
|
84
103
|
(t) => {
|
|
85
|
-
|
|
104
|
+
d(t, {
|
|
105
|
+
eventPointerTypes: o
|
|
106
|
+
}) && (f.current.active = !1);
|
|
86
107
|
},
|
|
87
|
-
[
|
|
88
|
-
)
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
u.current = P?.current ?? globalThis, l.current = new AbortController();
|
|
95
|
-
const { signal: t } = l.current, e = (r) => r instanceof PointerEvent && L(r), o = (r) => r instanceof PointerEvent && g(r), c = () => R();
|
|
108
|
+
[o]
|
|
109
|
+
);
|
|
110
|
+
O(() => {
|
|
111
|
+
E.current = I(h);
|
|
112
|
+
}, [h]), O(() => {
|
|
113
|
+
u.current = b?.current ?? globalThis, l.current = new AbortController();
|
|
114
|
+
const { signal: t } = l.current, e = (n) => n instanceof PointerEvent && T(n), c = (n) => n instanceof PointerEvent && R(n), i = (n) => n instanceof PointerEvent && C(n);
|
|
96
115
|
return u.current.addEventListener(
|
|
97
116
|
"pointerdown",
|
|
98
117
|
e,
|
|
@@ -100,28 +119,28 @@ const M = {
|
|
|
100
119
|
capture: a,
|
|
101
120
|
signal: t
|
|
102
121
|
}
|
|
103
|
-
), u.current.addEventListener("pointerup",
|
|
122
|
+
), u.current.addEventListener("pointerup", c, {
|
|
104
123
|
capture: a,
|
|
105
124
|
signal: t
|
|
106
125
|
}), u.current.addEventListener(
|
|
107
126
|
"pointercancel",
|
|
108
|
-
|
|
127
|
+
i,
|
|
109
128
|
{
|
|
110
129
|
capture: a,
|
|
111
130
|
signal: t
|
|
112
131
|
}
|
|
113
132
|
), () => {
|
|
114
|
-
l.current?.abort(),
|
|
133
|
+
l.current?.abort(), f.current.active = !1;
|
|
115
134
|
};
|
|
116
135
|
}, [
|
|
117
|
-
|
|
136
|
+
b,
|
|
118
137
|
a,
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
138
|
+
T,
|
|
139
|
+
R,
|
|
140
|
+
C
|
|
122
141
|
]);
|
|
123
142
|
};
|
|
124
143
|
export {
|
|
125
|
-
|
|
144
|
+
q as default
|
|
126
145
|
};
|
|
127
146
|
//# sourceMappingURL=use-swipe.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-swipe.js","sources":["../../src/use-swipe/use-swipe.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from \"react\";\nimport {\n SwipeDirection,\n SwipeState,\n SwipeData,\n UseSwipeSchema,\n UseSwipeOptions,\n UseSwipeCallback,\n SwipeOptions,\n SwipeDirections,\n SwipeEventPointerType,\n} from \"./use-swipe.types\";\nimport { parseSwipeDirection } from \"./parse-swipe-direction\";\nimport { invokeSwipeAction } from \"./invoke-swipe-action\";\n\nconst defaultOptions: SwipeOptions = {\n eventPointerTypes: [\"touch\", \"mouse\", \"pen\"],\n eventCapture: false,\n eventOnce: false,\n eventStopImmediatePropagation: false,\n threshold: 50,\n velocity: 0.3,\n container: { current: null },\n};\n\nconst useSwipe = (\n swipe: UseSwipeSchema,\n swipeCallback: UseSwipeCallback,\n options: UseSwipeOptions = {},\n) => {\n const {\n eventPointerTypes,\n eventCapture,\n eventOnce,\n eventStopImmediatePropagation,\n threshold,\n velocity,\n container,\n } = { ...defaultOptions, ...options };\n\n const allowedDirectionsReference = useRef<SwipeDirection[]>([]);\n const targetReference = useRef<EventTarget | null>(null);\n const abortControllerReference = useRef<AbortController | null>(null);\n\n const swipeStateReference = useRef<SwipeState>({\n startX: 0,\n startY: 0,\n startTime: 0,\n active: false,\n });\n\n const resolveDirection = useCallback(\n (deltaX: number, deltaY: number): SwipeDirection => {\n const absX = Math.abs(deltaX);\n const absY = Math.abs(deltaY);\n\n if (absX > absY) {\n return deltaX > 0 ? SwipeDirections.Right : SwipeDirections.Left;\n }\n\n return deltaY > 0 ? SwipeDirections.Down : SwipeDirections.Up;\n },\n [],\n );\n\n const matchesSchema = useCallback((direction: SwipeDirection): boolean => {\n const allowed = allowedDirectionsReference.current;\n\n if (allowed.includes(SwipeDirections.Both)) {\n return true;\n }\n\n if (\n allowed.includes(SwipeDirections.Horizontal) &&\n (direction === SwipeDirections.Left ||\n direction === SwipeDirections.Right)\n ) {\n return true;\n }\n\n if (\n allowed.includes(SwipeDirections.Vertical) &&\n (direction === SwipeDirections.Up || direction === SwipeDirections.Down)\n ) {\n return true;\n }\n\n return allowed.includes(direction);\n }, []);\n\n const handlePointerDown = useCallback(\n (event: PointerEvent) => {\n if (!event.isPrimary) {\n return;\n }\n\n if (\n !eventPointerTypes.includes(event.pointerType as SwipeEventPointerType)\n ) {\n return;\n }\n\n swipeStateReference.current = {\n startX: event.clientX,\n startY: event.clientY,\n startTime: Date.now(),\n active: true,\n };\n },\n [eventPointerTypes],\n );\n\n const endSwipe = useCallback(\n (event: PointerEvent) => {\n const state = swipeStateReference.current;\n if (!state.active) {\n return;\n }\n\n state.active = false;\n\n const duration = Date.now() - state.startTime;\n\n if (duration === 0) {\n return;\n }\n\n const deltaX = event.clientX - state.startX;\n const deltaY = event.clientY - state.startY;\n\n const distance = Math.hypot(deltaX, deltaY);\n\n const computedVelocity = distance / duration;\n\n if (distance < threshold) {\n return;\n }\n if (computedVelocity < velocity) {\n return;\n }\n\n const direction = resolveDirection(deltaX, deltaY);\n if (!matchesSchema(direction)) {\n return;\n }\n\n const data: SwipeData = {\n deltaX,\n deltaY,\n velocity: computedVelocity,\n duration,\n };\n\n invokeSwipeAction(event, direction, data, swipeCallback, {\n stopImmediate: eventStopImmediatePropagation,\n once: eventOnce,\n onOnce: () => {\n abortControllerReference.current?.abort();\n },\n });\n },\n [\n threshold,\n velocity,\n resolveDirection,\n matchesSchema,\n swipeCallback,\n eventOnce,\n eventStopImmediatePropagation,\n ],\n );\n\n const handlePointerUp = useCallback(\n (event: PointerEvent) => {\n if (!event.isPrimary) {\n return;\n }\n if (\n !eventPointerTypes.includes(event.pointerType as SwipeEventPointerType)\n ) {\n return;\n }\n endSwipe(event);\n },\n [endSwipe, eventPointerTypes],\n );\n\n const handlePointerCancel = useCallback(() => {\n swipeStateReference.current.active = false;\n }, []);\n\n useEffect(() => {\n allowedDirectionsReference.current = parseSwipeDirection(swipe);\n }, [swipe]);\n\n useEffect(() => {\n targetReference.current = container?.current ?? globalThis;\n abortControllerReference.current = new AbortController();\n const { signal } = abortControllerReference.current;\n\n const pointerDownListener = (event: Event) =>\n event instanceof PointerEvent && handlePointerDown(event);\n\n const pointerUpListener = (event: Event) =>\n event instanceof PointerEvent && handlePointerUp(event);\n\n const pointerCancelListener = () => handlePointerCancel();\n\n targetReference.current.addEventListener(\n \"pointerdown\",\n pointerDownListener,\n {\n capture: eventCapture,\n signal,\n },\n );\n\n targetReference.current.addEventListener(\"pointerup\", pointerUpListener, {\n capture: eventCapture,\n signal,\n });\n\n targetReference.current.addEventListener(\n \"pointercancel\",\n pointerCancelListener,\n {\n capture: eventCapture,\n signal,\n },\n );\n\n return () => {\n abortControllerReference.current?.abort();\n\n swipeStateReference.current.active = false;\n };\n }, [\n container,\n eventCapture,\n handlePointerDown,\n handlePointerUp,\n handlePointerCancel,\n ]);\n};\n\nexport default useSwipe;\n"],"names":["defaultOptions","useSwipe","swipe","swipeCallback","options","eventPointerTypes","eventCapture","eventOnce","eventStopImmediatePropagation","threshold","velocity","container","allowedDirectionsReference","useRef","targetReference","abortControllerReference","swipeStateReference","resolveDirection","useCallback","deltaX","deltaY","absX","absY","SwipeDirections","matchesSchema","direction","allowed","handlePointerDown","event","endSwipe","state","duration","distance","computedVelocity","invokeSwipeAction","handlePointerUp","handlePointerCancel","useEffect","parseSwipeDirection","signal","pointerDownListener","pointerUpListener","pointerCancelListener"],"mappings":";;;;AAeA,MAAMA,IAA+B;AAAA,EACnC,mBAAmB,CAAC,SAAS,SAAS,KAAK;AAAA,EAC3C,cAAc;AAAA,EACd,WAAW;AAAA,EACX,+BAA+B;AAAA,EAC/B,WAAW;AAAA,EACX,UAAU;AAAA,EACV,WAAW,EAAE,SAAS,KAAA;AACxB,GAEMC,IAAW,CACfC,GACAC,GACAC,IAA2B,CAAA,MACxB;AACH,QAAM;AAAA,IACJ,mBAAAC;AAAA,IACA,cAAAC;AAAA,IACA,WAAAC;AAAA,IACA,+BAAAC;AAAA,IACA,WAAAC;AAAA,IACA,UAAAC;AAAA,IACA,WAAAC;AAAA,EAAA,IACE,EAAE,GAAGX,GAAgB,GAAGI,EAAA,GAEtBQ,IAA6BC,EAAyB,EAAE,GACxDC,IAAkBD,EAA2B,IAAI,GACjDE,IAA2BF,EAA+B,IAAI,GAE9DG,IAAsBH,EAAmB;AAAA,IAC7C,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,WAAW;AAAA,IACX,QAAQ;AAAA,EAAA,CACT,GAEKI,IAAmBC;AAAA,IACvB,CAACC,GAAgBC,MAAmC;AAClD,YAAMC,IAAO,KAAK,IAAIF,CAAM,GACtBG,IAAO,KAAK,IAAIF,CAAM;AAE5B,aAAIC,IAAOC,IACFH,IAAS,IAAII,EAAgB,QAAQA,EAAgB,OAGvDH,IAAS,IAAIG,EAAgB,OAAOA,EAAgB;AAAA,I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QACT,QAAAiC;AAAA,MAAA;AAAA,IACF,GAGK,MAAM;AACX,MAAAxB,EAAyB,SAAS,MAAA,GAElCC,EAAoB,QAAQ,SAAS;AAAA,IACvC;AAAA,EACF,GAAG;AAAA,IACDL;AAAA,IACAL;AAAA,IACAqB;AAAA,IACAQ;AAAA,IACAC;AAAA,EAAA,CACD;AACH;"}
|
|
1
|
+
{"version":3,"file":"use-swipe.js","sources":["../../src/use-swipe/use-swipe.ts"],"sourcesContent":["import { useCallback, useEffect, useRef } from \"react\";\nimport {\n SwipeDirection,\n SwipeState,\n SwipeDirections,\n SwipeData,\n SwipeOptions,\n SwipeEventPointerTypes,\n UseSwipeSchema,\n UseSwipeOptions,\n UseSwipeCallback,\n} from \"./use-swipe.types\";\nimport { parseSwipeDirection } from \"./parse-swipe-direction\";\nimport { invokeSwipeAction } from \"./invoke-swipe-action\";\nimport { shouldHandleEvent } from \"./event-guards\";\n\nconst defaultOptions: SwipeOptions = {\n eventPointerTypes: [\n SwipeEventPointerTypes.Touch,\n SwipeEventPointerTypes.Mouse,\n SwipeEventPointerTypes.Pen,\n ],\n eventCapture: false,\n eventOnce: false,\n eventStopImmediatePropagation: false,\n threshold: 50,\n velocity: 0.3,\n container: { current: null },\n};\n\nconst useSwipe = (\n swipe: UseSwipeSchema,\n swipeCallback: UseSwipeCallback,\n options: UseSwipeOptions = {},\n) => {\n const {\n eventPointerTypes,\n eventCapture,\n eventOnce,\n eventStopImmediatePropagation,\n threshold,\n velocity,\n container,\n } = { ...defaultOptions, ...options };\n\n const allowedDirectionsReference = useRef<SwipeDirection[]>([]);\n const targetReference = useRef<EventTarget | null>(null);\n const abortControllerReference = useRef<AbortController | null>(null);\n\n const swipeStateReference = useRef<SwipeState>({\n startX: 0,\n startY: 0,\n startTime: 0,\n active: false,\n });\n\n const resolveDirection = useCallback(\n (deltaX: number, deltaY: number): SwipeDirection => {\n const absX = Math.abs(deltaX);\n const absY = Math.abs(deltaY);\n\n if (absX > absY) {\n return deltaX > 0 ? SwipeDirections.Right : SwipeDirections.Left;\n }\n\n return deltaY > 0 ? SwipeDirections.Down : SwipeDirections.Up;\n },\n [],\n );\n\n const matchesSchema = useCallback((direction: SwipeDirection): boolean => {\n const allowed = allowedDirectionsReference.current;\n\n if (allowed.includes(SwipeDirections.Both)) {\n return true;\n }\n\n if (\n allowed.includes(SwipeDirections.Horizontal) &&\n (direction === SwipeDirections.Left ||\n direction === SwipeDirections.Right)\n ) {\n return true;\n }\n\n if (\n allowed.includes(SwipeDirections.Vertical) &&\n (direction === SwipeDirections.Up || direction === SwipeDirections.Down)\n ) {\n return true;\n }\n\n return allowed.includes(direction);\n }, []);\n\n const handlePointerDown = useCallback(\n (event: PointerEvent) => {\n if (\n !shouldHandleEvent(event, {\n eventPointerTypes,\n })\n ) {\n return;\n }\n\n swipeStateReference.current = {\n startX: event.clientX,\n startY: event.clientY,\n startTime: Date.now(),\n active: true,\n };\n },\n [eventPointerTypes],\n );\n\n const endSwipe = useCallback(\n (event: PointerEvent) => {\n if (\n !shouldHandleEvent(event, {\n eventPointerTypes,\n })\n ) {\n return;\n }\n\n const state = swipeStateReference.current;\n if (!state.active) {\n return;\n }\n\n state.active = false;\n\n const duration = Date.now() - state.startTime;\n\n if (duration === 0) {\n return;\n }\n\n const deltaX = event.clientX - state.startX;\n const deltaY = event.clientY - state.startY;\n\n const distance = Math.hypot(deltaX, deltaY);\n\n const computedVelocity = distance / duration;\n\n if (distance < threshold) {\n return;\n }\n if (computedVelocity < velocity) {\n return;\n }\n\n const direction = resolveDirection(deltaX, deltaY);\n if (!matchesSchema(direction)) {\n return;\n }\n\n const data: SwipeData = {\n deltaX,\n deltaY,\n velocity: computedVelocity,\n duration,\n };\n\n invokeSwipeAction(event, direction, data, swipeCallback, {\n stopImmediate: eventStopImmediatePropagation,\n once: eventOnce,\n onOnce: () => {\n abortControllerReference.current?.abort();\n },\n });\n },\n [\n eventPointerTypes,\n eventOnce,\n eventStopImmediatePropagation,\n threshold,\n velocity,\n resolveDirection,\n matchesSchema,\n swipeCallback,\n ],\n );\n\n const handlePointerUp = useCallback(\n (event: PointerEvent) => {\n if (\n !shouldHandleEvent(event, {\n eventPointerTypes,\n })\n ) {\n return;\n }\n\n endSwipe(event);\n },\n [eventPointerTypes, endSwipe],\n );\n\n const handlePointerCancel = useCallback(\n (event: PointerEvent) => {\n if (\n !shouldHandleEvent(event, {\n eventPointerTypes,\n })\n ) {\n return;\n }\n\n swipeStateReference.current.active = false;\n },\n [eventPointerTypes],\n );\n\n useEffect(() => {\n allowedDirectionsReference.current = parseSwipeDirection(swipe);\n }, [swipe]);\n\n useEffect(() => {\n targetReference.current = container?.current ?? globalThis;\n abortControllerReference.current = new AbortController();\n const { signal } = abortControllerReference.current;\n\n const pointerDownListener = (event: Event) =>\n event instanceof PointerEvent && handlePointerDown(event);\n\n const pointerUpListener = (event: Event) =>\n event instanceof PointerEvent && handlePointerUp(event);\n\n const pointerCancelListener = (event: Event) =>\n event instanceof PointerEvent && handlePointerCancel(event);\n\n targetReference.current.addEventListener(\n \"pointerdown\",\n pointerDownListener,\n {\n capture: eventCapture,\n signal,\n },\n );\n\n targetReference.current.addEventListener(\"pointerup\", pointerUpListener, {\n capture: eventCapture,\n signal,\n });\n\n targetReference.current.addEventListener(\n \"pointercancel\",\n pointerCancelListener,\n {\n capture: eventCapture,\n signal,\n },\n );\n\n return () => {\n abortControllerReference.current?.abort();\n\n swipeStateReference.current.active = false;\n };\n }, [\n container,\n eventCapture,\n handlePointerDown,\n handlePointerUp,\n handlePointerCancel,\n ]);\n};\n\nexport default useSwipe;\n"],"names":["defaultOptions","SwipeEventPointerTypes","useSwipe","swipe","swipeCallback","options","eventPointerTypes","eventCapture","eventOnce","eventStopImmediatePropagation","threshold","velocity","container","allowedDirectionsReference","useRef","targetReference","abortControllerReference","swipeStateReference","resolveDirection","useCallback","deltaX","deltaY","absX","absY","SwipeDirections","matchesSchema","direction","allowed","handlePointerDown","event","shouldHandleEvent","endSwipe","state","duration","distance","computedVelocity","invokeSwipeAction","handlePointerUp","handlePointerCancel","useEffect","parseSwipeDirection","signal","pointerDownListener","pointerUpListener","pointerCancelListener"],"mappings":";;;;;AAgBA,MAAMA,IAA+B;AAAA,EACnC,mBAAmB;AAAA,IACjBC,EAAuB;AAAA,IACvBA,EAAuB;AAAA,IACvBA,EAAuB;AAAA,EAAA;AAAA,EAEzB,cAAc;AAAA,EACd,WAAW;AAAA,EACX,+BAA+B;AAAA,EAC/B,WAAW;AAAA,EACX,UAAU;AAAA,EACV,WAAW,EAAE,SAAS,KAAA;AACxB,GAEMC,IAAW,CACfC,GACAC,GACAC,IAA2B,CAAA,MACxB;AACH,QAAM;AAAA,IACJ,mBAAAC;AAAA,IACA,cAAAC;AAAA,IACA,WAAAC;AAAA,IACA,+BAAAC;AAAA,IACA,WAAAC;AAAA,IACA,UAAAC;AAAA,IACA,WAAAC;AAAA,EAAA,IACE,EAAE,GAAGZ,GAAgB,GAAGK,EAAA,GAEtBQ,IAA6BC,EAAyB,EAAE,GACxDC,IAAkBD,EAA2B,IAAI,GACjDE,IAA2BF,EAA+B,IAAI,GAE9DG,IAAsBH,EAAmB;AAAA,IAC7C,QAAQ;AAAA,IACR,QAAQ;AAAA,IACR,WAAW;AAAA,IACX,QAAQ;AAAA,EAAA,CACT,GAEKI,IAAmBC;AAAA,IACvB,CAACC,GAAgBC,MAAmC;AAClD,YAAMC,IAAO,KAAK,IAAIF,CAAM,GACtBG,IAAO,KAAK,IAAIF,CAAM;AAE5B,aAAIC,IAAOC,IACFH,IAAS,IAAII,EAAgB,QAAQA,EAAgB,OAGvDH,IAAS,IAAIG,EAAgB,OAAOA,EAAgB;AAAA,IAC7D;AAAA,IACA,CAAA;AAAA,EAAC,GAGGC,IAAgBN,EAAY,CAACO,MAAuC;AACxE,UAAMC,IAAUd,EAA2B;AAc3C,WAZIc,EAAQ,SAASH,EAAgB,IAAI,KAKvCG,EAAQ,SAASH,EAAgB,UAAU,MAC1CE,MAAcF,EAAgB,QAC7B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;AAAA,QACE,SAASrC;AAAA,QACT,QAAAkC;AAAA,MAAA;AAAA,IACF,GAGK,MAAM;AACX,MAAAzB,EAAyB,SAAS,MAAA,GAElCC,EAAoB,QAAQ,SAAS;AAAA,IACvC;AAAA,EACF,GAAG;AAAA,IACDL;AAAA,IACAL;AAAA,IACAqB;AAAA,IACAS;AAAA,IACAC;AAAA,EAAA,CACD;AACH;"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-swipe.types.js","sources":["../../src/use-swipe/use-swipe.types.ts"],"sourcesContent":["export const SwipeDirections = {\n Left: \"left\",\n Right: \"right\",\n Up: \"up\",\n Down: \"down\",\n Horizontal: \"horizontal\",\n Vertical: \"vertical\",\n Both: \"both\",\n} as const;\n\nexport type SwipeDirection =\n (typeof SwipeDirections)[keyof typeof SwipeDirections];\n\nexport interface SwipeState {\n startX: number;\n startY: number;\n startTime: number;\n active: boolean;\n}\n\nexport const SwipeEventPointerTypes = {\n Touch: \"touch\",\n Mouse: \"mouse\",\n Pen: \"pen\",\n} as const;\n\nexport type SwipeEventPointerType =\n (typeof SwipeEventPointerTypes)[keyof typeof SwipeEventPointerTypes];\n\nexport interface SwipeOptions {\n eventPointerTypes: SwipeEventPointerType[];\n eventCapture: boolean;\n eventOnce: boolean;\n eventStopImmediatePropagation: boolean;\n threshold: number;\n velocity: number;\n container: { current:
|
|
1
|
+
{"version":3,"file":"use-swipe.types.js","sources":["../../src/use-swipe/use-swipe.types.ts"],"sourcesContent":["export const SwipeDirections = {\n Left: \"left\",\n Right: \"right\",\n Up: \"up\",\n Down: \"down\",\n Horizontal: \"horizontal\",\n Vertical: \"vertical\",\n Both: \"both\",\n} as const;\n\nexport type SwipeDirection =\n (typeof SwipeDirections)[keyof typeof SwipeDirections];\n\nexport interface SwipeState {\n startX: number;\n startY: number;\n startTime: number;\n active: boolean;\n}\n\nexport const SwipeEventPointerTypes = {\n Touch: \"touch\",\n Mouse: \"mouse\",\n Pen: \"pen\",\n} as const;\n\nexport type SwipeEventPointerType =\n (typeof SwipeEventPointerTypes)[keyof typeof SwipeEventPointerTypes];\n\nexport interface SwipeOptions {\n eventPointerTypes: SwipeEventPointerType[];\n eventCapture: boolean;\n eventOnce: boolean;\n eventStopImmediatePropagation: boolean;\n threshold: number;\n velocity: number;\n container: { current: EventTarget | null };\n}\n\nexport interface SwipeData {\n deltaX: number;\n deltaY: number;\n velocity: number;\n duration: number;\n}\n\nexport type UseSwipeSchema = SwipeDirection | SwipeDirection[];\n\nexport type UseSwipeCallback =\n | ((\n event: PointerEvent,\n direction: SwipeDirection,\n data: SwipeData,\n ...properties: unknown[]\n ) => boolean)\n | ((\n event: PointerEvent,\n direction: SwipeDirection,\n data: SwipeData,\n ...properties: unknown[]\n ) => void);\n\nexport type UseSwipeOptions = Partial<SwipeOptions>;\n"],"names":["SwipeDirections","SwipeEventPointerTypes"],"mappings":"AAAO,MAAMA,IAAkB;AAAA,EAC7B,MAAM;AAAA,EACN,OAAO;AAAA,EACP,IAAI;AAAA,EACJ,MAAM;AAAA,EACN,YAAY;AAAA,EACZ,UAAU;AAAA,EACV,MAAM;AACR,GAYaC,IAAyB;AAAA,EACpC,OAAO;AAAA,EACP,OAAO;AAAA,EACP,KAAK;AACP;"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"event-guards.js","sources":["../../src/use-tap/event-guards.ts"],"sourcesContent":["import { TapEventPointerType } from \"./use-tap.types\";\n\n/**\n * Determines whether a pointer event should be handled based on event options.\n *\n * @param {PointerEvent} event - The pointer event to evaluate\n * @param {Object} options - Event handling options\n * @param {TapEventPointerType[]} options.eventPointerTypes - Allowed pointer types for the event\n * @returns {boolean} True if the event should be handled, false otherwise\n *\n * @example\n * const shouldHandle = shouldHandleEvent(event, {\n * eventPointerTypes: [TapEventPointerTypes.Touch]\n * });\n */\nexport const shouldHandleEvent = (\n event: PointerEvent,\n options: {\n eventPointerTypes: TapEventPointerType[];\n },\n) => {\n if (!event.isPrimary) {\n return false;\n }\n if (\n !options.eventPointerTypes.includes(\n event.pointerType as TapEventPointerType,\n )\n ) {\n return false;\n }\n\n return true;\n};\n"],"names":["shouldHandleEvent","event","options"],"mappings":"AAeO,MAAMA,IAAoB,CAC/BC,GACAC,MAII,GAACD,EAAM,aAIT,CAACC,EAAQ,kBAAkB;AAAA,EACzBD,EAAM;AAAA;"}
|