@seed-design/stackflow 1.1.20 → 1.1.21

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.
Files changed (93) hide show
  1. package/lib/_virtual/_rolldown/runtime.cjs +34 -0
  2. package/lib/_virtual/_rolldown/runtime.js +14 -0
  3. package/lib/components/AppBar/AppBar.cjs +86 -102
  4. package/lib/components/AppBar/AppBar.js +84 -98
  5. package/lib/components/AppBar/AppBar.namespace.cjs +20 -16
  6. package/lib/components/AppBar/AppBar.namespace.js +15 -1
  7. package/lib/components/AppBar/index.cjs +17 -18
  8. package/lib/components/AppBar/index.js +3 -3
  9. package/lib/components/AppScreen/AppScreen.cjs +46 -61
  10. package/lib/components/AppScreen/AppScreen.js +45 -57
  11. package/lib/components/AppScreen/AppScreen.namespace.cjs +17 -13
  12. package/lib/components/AppScreen/AppScreen.namespace.js +12 -1
  13. package/lib/components/AppScreen/index.cjs +14 -15
  14. package/lib/components/AppScreen/index.js +3 -3
  15. package/lib/components/index.cjs +30 -26
  16. package/lib/components/index.js +5 -6
  17. package/lib/hooks/index.cjs +3 -9
  18. package/lib/hooks/index.js +2 -1
  19. package/lib/hooks/useActivityZIndexBase.cjs +12 -11
  20. package/lib/hooks/useActivityZIndexBase.js +11 -7
  21. package/lib/index.cjs +41 -32
  22. package/lib/index.js +8 -10
  23. package/lib/plugin.cjs +16 -16
  24. package/lib/plugin.js +14 -12
  25. package/lib/primitive/AppBar/AppBar.cjs +41 -33
  26. package/lib/primitive/AppBar/AppBar.js +40 -29
  27. package/lib/primitive/AppBar/AppBar.namespace.cjs +16 -12
  28. package/lib/primitive/AppBar/AppBar.namespace.js +11 -1
  29. package/lib/primitive/AppBar/anatomy.cjs +18 -0
  30. package/lib/primitive/AppBar/anatomy.d.ts +15 -0
  31. package/lib/primitive/AppBar/anatomy.js +18 -0
  32. package/lib/primitive/AppBar/index.cjs +13 -14
  33. package/lib/primitive/AppBar/index.js +3 -3
  34. package/lib/primitive/AppBar/useAppBar.cjs +29 -36
  35. package/lib/primitive/AppBar/useAppBar.js +28 -32
  36. package/lib/primitive/AppBar/useAppBarContext.cjs +11 -18
  37. package/lib/primitive/AppBar/useAppBarContext.js +10 -14
  38. package/lib/primitive/AppScreen/AppScreen.cjs +49 -52
  39. package/lib/primitive/AppScreen/AppScreen.js +48 -48
  40. package/lib/primitive/AppScreen/AppScreen.namespace.cjs +16 -12
  41. package/lib/primitive/AppScreen/AppScreen.namespace.js +11 -1
  42. package/lib/primitive/AppScreen/anatomy.cjs +17 -0
  43. package/lib/primitive/AppScreen/anatomy.d.ts +14 -0
  44. package/lib/primitive/AppScreen/anatomy.js +17 -0
  45. package/lib/primitive/AppScreen/index.cjs +15 -16
  46. package/lib/primitive/AppScreen/index.js +4 -4
  47. package/lib/primitive/AppScreen/useAppScreen.cjs +60 -63
  48. package/lib/primitive/AppScreen/useAppScreen.js +59 -59
  49. package/lib/primitive/AppScreen/useAppScreenContext.cjs +11 -18
  50. package/lib/primitive/AppScreen/useAppScreenContext.js +10 -14
  51. package/lib/primitive/GlobalInteraction/GlobalInteraction.cjs +21 -26
  52. package/lib/primitive/GlobalInteraction/GlobalInteraction.js +20 -22
  53. package/lib/primitive/GlobalInteraction/animation.cjs +303 -0
  54. package/lib/primitive/GlobalInteraction/animation.d.ts +16 -0
  55. package/lib/primitive/GlobalInteraction/animation.js +299 -0
  56. package/lib/primitive/GlobalInteraction/constants.cjs +9 -0
  57. package/lib/primitive/GlobalInteraction/constants.d.ts +19 -0
  58. package/lib/primitive/GlobalInteraction/constants.js +8 -0
  59. package/lib/primitive/GlobalInteraction/dom.cjs +147 -0
  60. package/lib/primitive/GlobalInteraction/dom.d.ts +44 -0
  61. package/lib/primitive/GlobalInteraction/dom.js +140 -0
  62. package/lib/primitive/GlobalInteraction/index.cjs +5 -11
  63. package/lib/primitive/GlobalInteraction/index.js +3 -2
  64. package/lib/primitive/GlobalInteraction/useGlobalInteraction.cjs +220 -133
  65. package/lib/primitive/GlobalInteraction/useGlobalInteraction.d.ts +10 -26
  66. package/lib/primitive/GlobalInteraction/useGlobalInteraction.js +219 -129
  67. package/lib/primitive/GlobalInteraction/useGlobalInteractionContext.cjs +11 -18
  68. package/lib/primitive/GlobalInteraction/useGlobalInteractionContext.js +10 -14
  69. package/lib/primitive/GlobalInteraction/useSwipeBack.cjs +71 -55
  70. package/lib/primitive/GlobalInteraction/useSwipeBack.d.ts +19 -2
  71. package/lib/primitive/GlobalInteraction/useSwipeBack.js +70 -51
  72. package/lib/primitive/index.cjs +55 -27
  73. package/lib/primitive/index.js +26 -9
  74. package/lib/primitive/private/useElementOffset.cjs +29 -51
  75. package/lib/primitive/private/useElementOffset.js +27 -28
  76. package/lib/primitive/private/useTopActivity.cjs +22 -31
  77. package/lib/primitive/private/useTopActivity.js +21 -27
  78. package/lib/utils/createStyleContext.cjs +91 -74
  79. package/lib/utils/createStyleContext.js +89 -70
  80. package/lib/utils/styled.cjs +13 -20
  81. package/lib/utils/styled.js +12 -16
  82. package/package.json +4 -4
  83. package/src/components/AppBar/AppBar.tsx +9 -3
  84. package/src/primitive/AppBar/AppBar.tsx +4 -1
  85. package/src/primitive/AppBar/anatomy.ts +16 -0
  86. package/src/primitive/AppBar/useAppBar.tsx +2 -1
  87. package/src/primitive/AppScreen/anatomy.ts +15 -0
  88. package/src/primitive/AppScreen/useAppScreen.tsx +5 -4
  89. package/src/primitive/GlobalInteraction/animation.ts +447 -0
  90. package/src/primitive/GlobalInteraction/constants.ts +24 -0
  91. package/src/primitive/GlobalInteraction/dom.ts +211 -0
  92. package/src/primitive/GlobalInteraction/useGlobalInteraction.ts +302 -129
  93. package/src/primitive/GlobalInteraction/useSwipeBack.ts +64 -26
@@ -1,132 +1,222 @@
1
- 'use client';
2
- import { useCallbackRef } from '@radix-ui/react-use-callback-ref';
3
- import { useState, useRef, useCallback, useMemo } from 'react';
4
- import { useTopActivity } from '../private/useTopActivity.js';
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
- const [swipeBackState, setSwipeBackState] = useState("idle");
8
- const swipeBackContextRef = useRef({
9
- x0: 0,
10
- t0: 0,
11
- displacement: 0,
12
- displacementRatio: 0,
13
- velocity: 0
14
- });
15
- const stackRef = useRef(null);
16
- const setSwipeBackContext = useCallback((ctx) => {
17
- swipeBackContextRef.current = ctx;
18
- stackRef.current?.style.setProperty(
19
- "--swipe-back-displacement",
20
- `${ctx.displacement.toString()}px`
21
- );
22
- stackRef.current?.style.setProperty(
23
- "--swipe-back-displacement-ratio",
24
- ctx.displacementRatio.toString()
25
- );
26
- }, []);
27
- const getSwipeBackEvents = useCallback(
28
- (props) => {
29
- const {
30
- swipeBackDisplacementRatioThreshold: displacementRatioThreshold = 0.4,
31
- swipeBackVelocityThreshold: velocityThreshold = 1
32
- } = props;
33
- const onSwipeStart = useCallbackRef(props.onSwipeBackStart);
34
- const onSwipeMove = useCallbackRef(props.onSwipeBackMove);
35
- const onSwipeEnd = useCallbackRef(props.onSwipeBackEnd);
36
- const startSwipeBack = useCallback(
37
- ({ x0, t0 }) => {
38
- setSwipeBackContext({
39
- x0,
40
- t0,
41
- displacement: 0,
42
- displacementRatio: 0,
43
- velocity: 0
44
- });
45
- setSwipeBackState((prev) => prev === "swiping" ? prev : "swiping");
46
- onSwipeStart?.();
47
- },
48
- [onSwipeStart]
49
- );
50
- const moveSwipeBack = useCallback(
51
- ({ x, t }) => {
52
- const displacement = Math.max(0, x - swipeBackContextRef.current.x0);
53
- const displacementRatio = displacement / window.innerWidth;
54
- const velocity = displacement / (t - swipeBackContextRef.current.t0);
55
- setSwipeBackContext({
56
- ...swipeBackContextRef.current,
57
- displacement,
58
- displacementRatio,
59
- velocity
60
- });
61
- setSwipeBackState((prev) => prev === "swiping" ? prev : "swiping");
62
- onSwipeMove?.({ displacement, displacementRatio });
63
- },
64
- [onSwipeMove]
65
- );
66
- const endSwipeBack = useCallback(
67
- (_) => {
68
- const swiped = swipeBackContextRef.current.displacementRatio > displacementRatioThreshold || swipeBackContextRef.current.velocity > velocityThreshold;
69
- if (swiped) {
70
- stackRef.current?.style.setProperty("--swipe-back-target", "100%");
71
- setSwipeBackState("completing");
72
- } else {
73
- stackRef.current?.style.setProperty("--swipe-back-target", "0");
74
- setSwipeBackState("canceling");
75
- }
76
- onSwipeEnd?.({ swiped });
77
- },
78
- [onSwipeEnd, displacementRatioThreshold, velocityThreshold]
79
- );
80
- const reset = useCallback(() => {
81
- setSwipeBackContext({
82
- x0: 0,
83
- t0: 0,
84
- displacement: 0,
85
- displacementRatio: 0,
86
- velocity: 0
87
- });
88
- stackRef.current?.style.setProperty("--swipe-back-target", "0");
89
- setSwipeBackState("idle");
90
- }, []);
91
- return useMemo(
92
- () => ({
93
- startSwipeBack,
94
- moveSwipeBack,
95
- endSwipeBack,
96
- reset
97
- }),
98
- [startSwipeBack, moveSwipeBack, endSwipeBack, reset]
99
- );
100
- },
101
- [setSwipeBackContext]
102
- );
103
- const topActivity = useTopActivity();
104
- const stackProps = useMemo(
105
- () => ({
106
- "data-swipe-back-state": swipeBackState,
107
- "data-global-transition-state": topActivity.transitionState,
108
- "data-top-activity-type": topActivity.activityType,
109
- "data-top-transition-style": topActivity.transitionStyle
110
- }),
111
- [
112
- swipeBackState,
113
- topActivity.transitionState,
114
- topActivity.activityType,
115
- topActivity.transitionStyle
116
- ]
117
- );
118
- return useMemo(
119
- () => ({
120
- stackRef,
121
- swipeBackContextRef,
122
- swipeBackState,
123
- setSwipeBackState,
124
- setSwipeBackContext,
125
- getSwipeBackEvents,
126
- stackProps
127
- }),
128
- [swipeBackState, setSwipeBackContext, getSwipeBackEvents, stackProps]
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
- 'use client';
2
- 'use strict';
3
-
4
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5
-
6
- const React = require('react');
7
-
8
- const GlobalInteractionContext = React.createContext(null);
9
- const GlobalInteractionProvider = GlobalInteractionContext.Provider;
10
- function useGlobalInteractionContext({
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
- 'use client';
2
- import { createContext, useContext } from 'react';
3
-
4
- const GlobalInteractionContext = createContext(null);
5
- const GlobalInteractionProvider = GlobalInteractionContext.Provider;
6
- function useGlobalInteractionContext({
7
- strict = true
8
- } = {}) {
9
- const context = useContext(GlobalInteractionContext);
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
- 'use client';
2
- 'use strict';
3
-
4
- Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
5
-
6
- const React = require('react');
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
- const globalInteraction = useGlobalInteractionContext.useGlobalInteractionContext();
11
- const events = globalInteraction.getSwipeBackEvents(props);
12
- React.useEffect(() => {
13
- return () => {
14
- events.reset();
15
- };
16
- }, [events]);
17
- return React.useMemo(
18
- () => ({
19
- activityProps: {
20
- "data-swipe-back": ""
21
- },
22
- layerProps: {
23
- onAnimationEnd: (e) => {
24
- if (e.target === e.currentTarget) {
25
- events.reset();
26
- }
27
- },
28
- onTransitionEnd: (e) => {
29
- if (e.target === e.currentTarget) {
30
- events.reset();
31
- }
32
- }
33
- },
34
- edgeProps: {
35
- tabIndex: -1,
36
- onTouchStart: (e) => {
37
- const x0 = e.touches[0].clientX;
38
- const t0 = Date.now();
39
- events.startSwipeBack({ x0, t0 });
40
- },
41
- onTouchMove: (e) => {
42
- const x = e.touches[0].clientX;
43
- const t = Date.now();
44
- events.moveSwipeBack({ x, t });
45
- },
46
- onTouchEnd: () => {
47
- events.endSwipeBack({});
48
- },
49
- onTouchCancel: () => {
50
- events.endSwipeBack({});
51
- }
52
- }
53
- }),
54
- [events]
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
- import { SwipeBackProps } from './useGlobalInteraction';
2
- export interface UseSwipeBackProps extends SwipeBackProps {
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>;