@seed-design/stackflow 1.1.20 → 1.1.22
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/lib/_virtual/_rolldown/runtime.cjs +34 -0
- package/lib/_virtual/_rolldown/runtime.js +14 -0
- package/lib/components/AppBar/AppBar.cjs +86 -102
- package/lib/components/AppBar/AppBar.js +84 -98
- package/lib/components/AppBar/AppBar.namespace.cjs +20 -16
- package/lib/components/AppBar/AppBar.namespace.js +15 -1
- package/lib/components/AppBar/index.cjs +17 -18
- package/lib/components/AppBar/index.js +3 -3
- package/lib/components/AppScreen/AppScreen.cjs +46 -61
- package/lib/components/AppScreen/AppScreen.js +45 -57
- package/lib/components/AppScreen/AppScreen.namespace.cjs +17 -13
- package/lib/components/AppScreen/AppScreen.namespace.js +12 -1
- package/lib/components/AppScreen/index.cjs +14 -15
- package/lib/components/AppScreen/index.js +3 -3
- package/lib/components/index.cjs +30 -26
- package/lib/components/index.js +5 -6
- package/lib/hooks/index.cjs +3 -9
- package/lib/hooks/index.js +2 -1
- package/lib/hooks/useActivityZIndexBase.cjs +12 -11
- package/lib/hooks/useActivityZIndexBase.js +11 -7
- package/lib/index.cjs +41 -32
- package/lib/index.js +8 -10
- package/lib/plugin.cjs +16 -16
- package/lib/plugin.js +14 -12
- package/lib/primitive/AppBar/AppBar.cjs +41 -33
- package/lib/primitive/AppBar/AppBar.js +40 -29
- package/lib/primitive/AppBar/AppBar.namespace.cjs +16 -12
- package/lib/primitive/AppBar/AppBar.namespace.js +11 -1
- package/lib/primitive/AppBar/anatomy.cjs +18 -0
- package/lib/primitive/AppBar/anatomy.d.ts +15 -0
- package/lib/primitive/AppBar/anatomy.js +18 -0
- package/lib/primitive/AppBar/index.cjs +13 -14
- package/lib/primitive/AppBar/index.js +3 -3
- package/lib/primitive/AppBar/useAppBar.cjs +29 -36
- package/lib/primitive/AppBar/useAppBar.js +28 -32
- package/lib/primitive/AppBar/useAppBarContext.cjs +11 -18
- package/lib/primitive/AppBar/useAppBarContext.js +10 -14
- package/lib/primitive/AppScreen/AppScreen.cjs +49 -52
- package/lib/primitive/AppScreen/AppScreen.js +48 -48
- package/lib/primitive/AppScreen/AppScreen.namespace.cjs +16 -12
- package/lib/primitive/AppScreen/AppScreen.namespace.js +11 -1
- package/lib/primitive/AppScreen/anatomy.cjs +17 -0
- package/lib/primitive/AppScreen/anatomy.d.ts +14 -0
- package/lib/primitive/AppScreen/anatomy.js +17 -0
- package/lib/primitive/AppScreen/index.cjs +15 -16
- package/lib/primitive/AppScreen/index.js +4 -4
- package/lib/primitive/AppScreen/useAppScreen.cjs +60 -63
- package/lib/primitive/AppScreen/useAppScreen.js +59 -59
- package/lib/primitive/AppScreen/useAppScreenContext.cjs +11 -18
- package/lib/primitive/AppScreen/useAppScreenContext.js +10 -14
- package/lib/primitive/GlobalInteraction/GlobalInteraction.cjs +21 -26
- package/lib/primitive/GlobalInteraction/GlobalInteraction.js +20 -22
- package/lib/primitive/GlobalInteraction/animation.cjs +303 -0
- package/lib/primitive/GlobalInteraction/animation.d.ts +16 -0
- package/lib/primitive/GlobalInteraction/animation.js +299 -0
- package/lib/primitive/GlobalInteraction/constants.cjs +9 -0
- package/lib/primitive/GlobalInteraction/constants.d.ts +19 -0
- package/lib/primitive/GlobalInteraction/constants.js +8 -0
- package/lib/primitive/GlobalInteraction/dom.cjs +147 -0
- package/lib/primitive/GlobalInteraction/dom.d.ts +44 -0
- package/lib/primitive/GlobalInteraction/dom.js +140 -0
- package/lib/primitive/GlobalInteraction/index.cjs +5 -11
- package/lib/primitive/GlobalInteraction/index.js +3 -2
- package/lib/primitive/GlobalInteraction/useGlobalInteraction.cjs +220 -133
- package/lib/primitive/GlobalInteraction/useGlobalInteraction.d.ts +10 -26
- package/lib/primitive/GlobalInteraction/useGlobalInteraction.js +219 -129
- package/lib/primitive/GlobalInteraction/useGlobalInteractionContext.cjs +11 -18
- package/lib/primitive/GlobalInteraction/useGlobalInteractionContext.js +10 -14
- package/lib/primitive/GlobalInteraction/useSwipeBack.cjs +71 -55
- package/lib/primitive/GlobalInteraction/useSwipeBack.d.ts +19 -2
- package/lib/primitive/GlobalInteraction/useSwipeBack.js +70 -51
- package/lib/primitive/index.cjs +55 -27
- package/lib/primitive/index.js +26 -9
- package/lib/primitive/private/useElementOffset.cjs +29 -51
- package/lib/primitive/private/useElementOffset.js +27 -28
- package/lib/primitive/private/useTopActivity.cjs +22 -31
- package/lib/primitive/private/useTopActivity.js +21 -27
- package/lib/utils/createStyleContext.cjs +91 -74
- package/lib/utils/createStyleContext.js +89 -70
- package/lib/utils/styled.cjs +13 -20
- package/lib/utils/styled.js +12 -16
- package/package.json +5 -5
- package/src/components/AppBar/AppBar.tsx +9 -3
- package/src/primitive/AppBar/AppBar.tsx +4 -1
- package/src/primitive/AppBar/anatomy.ts +16 -0
- package/src/primitive/AppBar/useAppBar.tsx +2 -1
- package/src/primitive/AppScreen/anatomy.ts +15 -0
- package/src/primitive/AppScreen/useAppScreen.tsx +5 -4
- package/src/primitive/GlobalInteraction/animation.ts +447 -0
- package/src/primitive/GlobalInteraction/constants.ts +24 -0
- package/src/primitive/GlobalInteraction/dom.ts +211 -0
- package/src/primitive/GlobalInteraction/useGlobalInteraction.ts +302 -129
- package/src/primitive/GlobalInteraction/useSwipeBack.ts +64 -26
|
@@ -1,132 +1,222 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
|
|
1
|
+
"use client";
|
|
2
|
+
import { useTopActivity } from "../private/useTopActivity.js";
|
|
3
|
+
import { applySwipeStyles, clearAllStyles, findTransitionTargets, readTransitionStyle, setIdlePositions, setPostExitPositions } from "./dom.js";
|
|
4
|
+
import { animateSwipeCancel, animateSwipeComplete, animateTransition, cancelAll, scrubAppBarBackground } from "./animation.js";
|
|
5
|
+
import { useCallback, useEffect, useLayoutEffect, useMemo, useRef } from "react";
|
|
6
|
+
//#region src/primitive/GlobalInteraction/useGlobalInteraction.ts
|
|
7
|
+
var IDLE_CONTEXT = Object.freeze({
|
|
8
|
+
x0: 0,
|
|
9
|
+
t0: 0,
|
|
10
|
+
displacement: 0,
|
|
11
|
+
displacementRatio: 0,
|
|
12
|
+
velocity: 0
|
|
13
|
+
});
|
|
14
|
+
var DEFAULT_DISPLACEMENT_RATIO_THRESHOLD = .4;
|
|
15
|
+
var DEFAULT_VELOCITY_THRESHOLD = 1;
|
|
6
16
|
function useGlobalInteraction() {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
17
|
+
const swipeBackStateRef = useRef("idle");
|
|
18
|
+
const swipeBackContextRef = useRef({ ...IDLE_CONTEXT });
|
|
19
|
+
const stackRef = useRef(null);
|
|
20
|
+
const cachedTargetsRef = useRef(null);
|
|
21
|
+
const runningAnimsRef = useRef([]);
|
|
22
|
+
const appBarBgScrubAnimRef = useRef(null);
|
|
23
|
+
const swipeStyleRef = useRef(null);
|
|
24
|
+
const pendingPushRAFRef = useRef(null);
|
|
25
|
+
const skipNextExitRef = useRef(false);
|
|
26
|
+
/** Cancel any running WAAPI animations and clear the ref. */
|
|
27
|
+
const stopRunningAnims = useCallback(() => {
|
|
28
|
+
cancelAll(runningAnimsRef.current);
|
|
29
|
+
runningAnimsRef.current = [];
|
|
30
|
+
}, []);
|
|
31
|
+
/** Cancel the in-place scrub animation on the AppBar background. */
|
|
32
|
+
const stopAppBarBgScrub = useCallback(() => {
|
|
33
|
+
appBarBgScrubAnimRef.current?.cancel();
|
|
34
|
+
appBarBgScrubAnimRef.current = null;
|
|
35
|
+
}, []);
|
|
36
|
+
/** Cancel a pending deferred push animation, if any. */
|
|
37
|
+
const cancelPendingPushRAF = useCallback(() => {
|
|
38
|
+
if (pendingPushRAFRef.current !== null) {
|
|
39
|
+
cancelAnimationFrame(pendingPushRAFRef.current);
|
|
40
|
+
pendingPushRAFRef.current = null;
|
|
41
|
+
}
|
|
42
|
+
}, []);
|
|
43
|
+
/** Update swipe-back-state attribute on the stack DOM element. */
|
|
44
|
+
const setSwipeBackState = useCallback((state) => {
|
|
45
|
+
swipeBackStateRef.current = state;
|
|
46
|
+
if (stackRef.current) stackRef.current.dataset["swipeBackState"] = state;
|
|
47
|
+
}, []);
|
|
48
|
+
const startSwipeBack = useCallback(({ x0, t0 }) => {
|
|
49
|
+
cancelPendingPushRAF();
|
|
50
|
+
stopRunningAnims();
|
|
51
|
+
stopAppBarBgScrub();
|
|
52
|
+
swipeBackContextRef.current = {
|
|
53
|
+
...IDLE_CONTEXT,
|
|
54
|
+
x0,
|
|
55
|
+
t0
|
|
56
|
+
};
|
|
57
|
+
if (stackRef.current) {
|
|
58
|
+
cachedTargetsRef.current = findTransitionTargets(stackRef.current);
|
|
59
|
+
swipeStyleRef.current = readTransitionStyle(stackRef.current);
|
|
60
|
+
}
|
|
61
|
+
setSwipeBackState("swiping");
|
|
62
|
+
}, [
|
|
63
|
+
cancelPendingPushRAF,
|
|
64
|
+
stopRunningAnims,
|
|
65
|
+
stopAppBarBgScrub,
|
|
66
|
+
setSwipeBackState
|
|
67
|
+
]);
|
|
68
|
+
const moveSwipeBack = useCallback(({ x, t }) => {
|
|
69
|
+
const ctx = swipeBackContextRef.current;
|
|
70
|
+
const displacement = Math.max(0, x - ctx.x0);
|
|
71
|
+
const displacementRatio = displacement / window.innerWidth;
|
|
72
|
+
const velocity = displacement / (t - ctx.t0);
|
|
73
|
+
ctx.displacement = displacement;
|
|
74
|
+
ctx.displacementRatio = displacementRatio;
|
|
75
|
+
ctx.velocity = velocity;
|
|
76
|
+
const targets = cachedTargetsRef.current;
|
|
77
|
+
if (targets && swipeStyleRef.current === "slideFromRightIOS") {
|
|
78
|
+
applySwipeStyles(targets, displacement, displacementRatio);
|
|
79
|
+
appBarBgScrubAnimRef.current = scrubAppBarBackground(targets.topAppBarBackground, `translate3d(${displacement}px, 0, 0)`, appBarBgScrubAnimRef.current);
|
|
80
|
+
}
|
|
81
|
+
return ctx;
|
|
82
|
+
}, []);
|
|
83
|
+
const endSwipeBack = useCallback((thresholds = {}) => {
|
|
84
|
+
const { displacementRatioThreshold = DEFAULT_DISPLACEMENT_RATIO_THRESHOLD, velocityThreshold = DEFAULT_VELOCITY_THRESHOLD } = thresholds;
|
|
85
|
+
const ctx = swipeBackContextRef.current;
|
|
86
|
+
const swiped = ctx.displacementRatio > displacementRatioThreshold || ctx.velocity > velocityThreshold;
|
|
87
|
+
const targets = cachedTargetsRef.current;
|
|
88
|
+
if (!targets) {
|
|
89
|
+
setSwipeBackState("idle");
|
|
90
|
+
return swiped;
|
|
91
|
+
}
|
|
92
|
+
if (swipeStyleRef.current !== "slideFromRightIOS") {
|
|
93
|
+
cachedTargetsRef.current = null;
|
|
94
|
+
swipeStyleRef.current = null;
|
|
95
|
+
setSwipeBackState("idle");
|
|
96
|
+
return swiped;
|
|
97
|
+
}
|
|
98
|
+
stopAppBarBgScrub();
|
|
99
|
+
const onFinish = (animations, pin) => {
|
|
100
|
+
pin();
|
|
101
|
+
cancelAll(animations);
|
|
102
|
+
if (runningAnimsRef.current === animations) {
|
|
103
|
+
runningAnimsRef.current = [];
|
|
104
|
+
cachedTargetsRef.current = null;
|
|
105
|
+
swipeStyleRef.current = null;
|
|
106
|
+
setSwipeBackState("idle");
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
if (swiped) {
|
|
110
|
+
setSwipeBackState("completing");
|
|
111
|
+
skipNextExitRef.current = true;
|
|
112
|
+
const { animations, finished } = animateSwipeComplete(targets, ctx.displacement);
|
|
113
|
+
runningAnimsRef.current = animations;
|
|
114
|
+
finished.then(() => onFinish(animations, () => setPostExitPositions(targets)));
|
|
115
|
+
} else {
|
|
116
|
+
setSwipeBackState("canceling");
|
|
117
|
+
const { animations, finished } = animateSwipeCancel(targets, ctx.displacement);
|
|
118
|
+
runningAnimsRef.current = animations;
|
|
119
|
+
finished.then(() => onFinish(animations, () => setIdlePositions(targets)));
|
|
120
|
+
}
|
|
121
|
+
return swiped;
|
|
122
|
+
}, [setSwipeBackState, stopAppBarBgScrub]);
|
|
123
|
+
const resetSwipeBack = useCallback(() => {
|
|
124
|
+
stopRunningAnims();
|
|
125
|
+
stopAppBarBgScrub();
|
|
126
|
+
if (cachedTargetsRef.current) clearAllStyles(cachedTargetsRef.current);
|
|
127
|
+
cachedTargetsRef.current = null;
|
|
128
|
+
swipeStyleRef.current = null;
|
|
129
|
+
swipeBackContextRef.current = { ...IDLE_CONTEXT };
|
|
130
|
+
setSwipeBackState("idle");
|
|
131
|
+
}, [
|
|
132
|
+
stopRunningAnims,
|
|
133
|
+
stopAppBarBgScrub,
|
|
134
|
+
setSwipeBackState
|
|
135
|
+
]);
|
|
136
|
+
const topActivity = useTopActivity();
|
|
137
|
+
const prevTransitionStateRef = useRef(topActivity.transitionState);
|
|
138
|
+
useLayoutEffect(() => {
|
|
139
|
+
const prev = prevTransitionStateRef.current;
|
|
140
|
+
const next = topActivity.transitionState;
|
|
141
|
+
prevTransitionStateRef.current = next;
|
|
142
|
+
const stackEl = stackRef.current;
|
|
143
|
+
if (!stackEl) return;
|
|
144
|
+
const swipeState = swipeBackStateRef.current;
|
|
145
|
+
if (next === "enter-active" && prev !== "enter-active") {
|
|
146
|
+
if (swipeState !== "idle") return;
|
|
147
|
+
stopRunningAnims();
|
|
148
|
+
cancelPendingPushRAF();
|
|
149
|
+
pendingPushRAFRef.current = requestAnimationFrame(() => {
|
|
150
|
+
pendingPushRAFRef.current = null;
|
|
151
|
+
const style = readTransitionStyle(stackEl);
|
|
152
|
+
const targets = findTransitionTargets(stackEl);
|
|
153
|
+
const { animations, finished } = animateTransition(targets, "push", style);
|
|
154
|
+
runningAnimsRef.current = animations;
|
|
155
|
+
finished.then(() => {
|
|
156
|
+
setIdlePositions(targets, style);
|
|
157
|
+
cancelAll(animations);
|
|
158
|
+
if (runningAnimsRef.current === animations) runningAnimsRef.current = [];
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
if (next === "exit-active" && prev !== "exit-active") {
|
|
163
|
+
if (skipNextExitRef.current) {
|
|
164
|
+
skipNextExitRef.current = false;
|
|
165
|
+
return;
|
|
166
|
+
}
|
|
167
|
+
if (swipeState !== "idle") return;
|
|
168
|
+
cancelPendingPushRAF();
|
|
169
|
+
stopRunningAnims();
|
|
170
|
+
const style = readTransitionStyle(stackEl);
|
|
171
|
+
const targets = findTransitionTargets(stackEl);
|
|
172
|
+
const { animations, finished } = animateTransition(targets, "pop", style);
|
|
173
|
+
runningAnimsRef.current = animations;
|
|
174
|
+
finished.then(() => {
|
|
175
|
+
setPostExitPositions(targets, style);
|
|
176
|
+
cancelAll(animations);
|
|
177
|
+
if (runningAnimsRef.current === animations) runningAnimsRef.current = [];
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
}, [
|
|
181
|
+
topActivity.transitionState,
|
|
182
|
+
stopRunningAnims,
|
|
183
|
+
cancelPendingPushRAF
|
|
184
|
+
]);
|
|
185
|
+
useEffect(() => {
|
|
186
|
+
return () => {
|
|
187
|
+
cancelPendingPushRAF();
|
|
188
|
+
stopRunningAnims();
|
|
189
|
+
stopAppBarBgScrub();
|
|
190
|
+
};
|
|
191
|
+
}, [
|
|
192
|
+
cancelPendingPushRAF,
|
|
193
|
+
stopRunningAnims,
|
|
194
|
+
stopAppBarBgScrub
|
|
195
|
+
]);
|
|
196
|
+
const stackProps = useMemo(() => ({
|
|
197
|
+
"data-swipe-back-state": swipeBackStateRef.current,
|
|
198
|
+
"data-global-transition-state": topActivity.transitionState,
|
|
199
|
+
"data-top-activity-type": topActivity.activityType,
|
|
200
|
+
"data-top-transition-style": topActivity.transitionStyle
|
|
201
|
+
}), [
|
|
202
|
+
topActivity.transitionState,
|
|
203
|
+
topActivity.activityType,
|
|
204
|
+
topActivity.transitionStyle
|
|
205
|
+
]);
|
|
206
|
+
return useMemo(() => ({
|
|
207
|
+
stackRef,
|
|
208
|
+
startSwipeBack,
|
|
209
|
+
moveSwipeBack,
|
|
210
|
+
endSwipeBack,
|
|
211
|
+
resetSwipeBack,
|
|
212
|
+
stackProps
|
|
213
|
+
}), [
|
|
214
|
+
startSwipeBack,
|
|
215
|
+
moveSwipeBack,
|
|
216
|
+
endSwipeBack,
|
|
217
|
+
resetSwipeBack,
|
|
218
|
+
stackProps
|
|
219
|
+
]);
|
|
130
220
|
}
|
|
131
|
-
|
|
221
|
+
//#endregion
|
|
132
222
|
export { useGlobalInteraction };
|
|
@@ -1,21 +1,14 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
const
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
strict = true
|
|
12
|
-
} = {}) {
|
|
13
|
-
const context = React.useContext(GlobalInteractionContext);
|
|
14
|
-
if (!context && strict) {
|
|
15
|
-
throw new Error("useGlobalInteractionContext must be used within a GlobalInteractionProvider");
|
|
16
|
-
}
|
|
17
|
-
return context;
|
|
1
|
+
"use client";
|
|
2
|
+
require("../../_virtual/_rolldown/runtime.cjs");
|
|
3
|
+
let react = require("react");
|
|
4
|
+
//#region src/primitive/GlobalInteraction/useGlobalInteractionContext.tsx
|
|
5
|
+
var GlobalInteractionContext = (0, react.createContext)(null);
|
|
6
|
+
var GlobalInteractionProvider = GlobalInteractionContext.Provider;
|
|
7
|
+
function useGlobalInteractionContext({ strict = true } = {}) {
|
|
8
|
+
const context = (0, react.useContext)(GlobalInteractionContext);
|
|
9
|
+
if (!context && strict) throw new Error("useGlobalInteractionContext must be used within a GlobalInteractionProvider");
|
|
10
|
+
return context;
|
|
18
11
|
}
|
|
19
|
-
|
|
12
|
+
//#endregion
|
|
20
13
|
exports.GlobalInteractionProvider = GlobalInteractionProvider;
|
|
21
14
|
exports.useGlobalInteractionContext = useGlobalInteractionContext;
|
|
@@ -1,16 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
import { createContext, useContext } from
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
function useGlobalInteractionContext({
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (!context && strict) {
|
|
11
|
-
throw new Error("useGlobalInteractionContext must be used within a GlobalInteractionProvider");
|
|
12
|
-
}
|
|
13
|
-
return context;
|
|
1
|
+
"use client";
|
|
2
|
+
import { createContext, useContext } from "react";
|
|
3
|
+
//#region src/primitive/GlobalInteraction/useGlobalInteractionContext.tsx
|
|
4
|
+
var GlobalInteractionContext = createContext(null);
|
|
5
|
+
var GlobalInteractionProvider = GlobalInteractionContext.Provider;
|
|
6
|
+
function useGlobalInteractionContext({ strict = true } = {}) {
|
|
7
|
+
const context = useContext(GlobalInteractionContext);
|
|
8
|
+
if (!context && strict) throw new Error("useGlobalInteractionContext must be used within a GlobalInteractionProvider");
|
|
9
|
+
return context;
|
|
14
10
|
}
|
|
15
|
-
|
|
11
|
+
//#endregion
|
|
16
12
|
export { GlobalInteractionProvider, useGlobalInteractionContext };
|
|
@@ -1,58 +1,74 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
const useGlobalInteractionContext = require('./useGlobalInteractionContext.cjs');
|
|
8
|
-
|
|
1
|
+
"use client";
|
|
2
|
+
require("../../_virtual/_rolldown/runtime.cjs");
|
|
3
|
+
const require_useGlobalInteractionContext = require("./useGlobalInteractionContext.cjs");
|
|
4
|
+
let react = require("react");
|
|
5
|
+
let _radix_ui_react_use_callback_ref = require("@radix-ui/react-use-callback-ref");
|
|
6
|
+
//#region src/primitive/GlobalInteraction/useSwipeBack.ts
|
|
9
7
|
function useSwipeBack(props) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
8
|
+
const { swipeBackDisplacementRatioThreshold: displacementRatioThreshold, swipeBackVelocityThreshold: velocityThreshold } = props;
|
|
9
|
+
const onSwipeStart = (0, _radix_ui_react_use_callback_ref.useCallbackRef)(props.onSwipeBackStart);
|
|
10
|
+
const onSwipeMove = (0, _radix_ui_react_use_callback_ref.useCallbackRef)(props.onSwipeBackMove);
|
|
11
|
+
const onSwipeEnd = (0, _radix_ui_react_use_callback_ref.useCallbackRef)(props.onSwipeBackEnd);
|
|
12
|
+
const { startSwipeBack, moveSwipeBack, endSwipeBack, resetSwipeBack } = require_useGlobalInteractionContext.useGlobalInteractionContext();
|
|
13
|
+
const rAFLockRef = (0, react.useRef)(false);
|
|
14
|
+
(0, react.useEffect)(() => {
|
|
15
|
+
return () => {
|
|
16
|
+
resetSwipeBack();
|
|
17
|
+
};
|
|
18
|
+
}, [resetSwipeBack]);
|
|
19
|
+
return (0, react.useMemo)(() => ({
|
|
20
|
+
activityProps: { "data-swipe-back": "" },
|
|
21
|
+
layerProps: {},
|
|
22
|
+
edgeProps: {
|
|
23
|
+
tabIndex: -1,
|
|
24
|
+
onTouchStart: (e) => {
|
|
25
|
+
const x0 = e.touches[0].clientX;
|
|
26
|
+
startSwipeBack({
|
|
27
|
+
x0,
|
|
28
|
+
t0: Date.now()
|
|
29
|
+
});
|
|
30
|
+
onSwipeStart();
|
|
31
|
+
},
|
|
32
|
+
onTouchMove: (e) => {
|
|
33
|
+
if (rAFLockRef.current) return;
|
|
34
|
+
rAFLockRef.current = true;
|
|
35
|
+
const x = e.touches[0].clientX;
|
|
36
|
+
const t = Date.now();
|
|
37
|
+
requestAnimationFrame(() => {
|
|
38
|
+
const ctx = moveSwipeBack({
|
|
39
|
+
x,
|
|
40
|
+
t
|
|
41
|
+
});
|
|
42
|
+
rAFLockRef.current = false;
|
|
43
|
+
onSwipeMove({
|
|
44
|
+
displacement: ctx.displacement,
|
|
45
|
+
displacementRatio: ctx.displacementRatio
|
|
46
|
+
});
|
|
47
|
+
});
|
|
48
|
+
},
|
|
49
|
+
onTouchEnd: () => {
|
|
50
|
+
onSwipeEnd({ swiped: endSwipeBack({
|
|
51
|
+
displacementRatioThreshold,
|
|
52
|
+
velocityThreshold
|
|
53
|
+
}) });
|
|
54
|
+
},
|
|
55
|
+
onTouchCancel: () => {
|
|
56
|
+
onSwipeEnd({ swiped: endSwipeBack({
|
|
57
|
+
displacementRatioThreshold,
|
|
58
|
+
velocityThreshold
|
|
59
|
+
}) });
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}), [
|
|
63
|
+
startSwipeBack,
|
|
64
|
+
moveSwipeBack,
|
|
65
|
+
endSwipeBack,
|
|
66
|
+
onSwipeStart,
|
|
67
|
+
onSwipeMove,
|
|
68
|
+
onSwipeEnd,
|
|
69
|
+
displacementRatioThreshold,
|
|
70
|
+
velocityThreshold
|
|
71
|
+
]);
|
|
56
72
|
}
|
|
57
|
-
|
|
73
|
+
//#endregion
|
|
58
74
|
exports.useSwipeBack = useSwipeBack;
|
|
@@ -1,5 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export interface UseSwipeBackProps {
|
|
2
|
+
/**
|
|
3
|
+
* The threshold to determine whether the swipe back is intentional, by displacement ratio.
|
|
4
|
+
* @default 0.4
|
|
5
|
+
*/
|
|
6
|
+
swipeBackDisplacementRatioThreshold?: number;
|
|
7
|
+
/**
|
|
8
|
+
* The threshold to determine whether the swipe back is intentional, by velocity.
|
|
9
|
+
* @default 1
|
|
10
|
+
*/
|
|
11
|
+
swipeBackVelocityThreshold?: number;
|
|
12
|
+
onSwipeBackStart?: () => void;
|
|
13
|
+
onSwipeBackMove?: (props: {
|
|
14
|
+
displacement: number;
|
|
15
|
+
displacementRatio: number;
|
|
16
|
+
}) => void;
|
|
17
|
+
onSwipeBackEnd?: (props: {
|
|
18
|
+
swiped: boolean;
|
|
19
|
+
}) => void;
|
|
3
20
|
}
|
|
4
21
|
export declare function useSwipeBack(props: UseSwipeBackProps): {
|
|
5
22
|
activityProps: React.HTMLAttributes<HTMLDivElement>;
|