Skip to content

Commit 191b9d7

Browse files
committed
add some procs
1 parent 22f18ac commit 191b9d7

File tree

2 files changed

+107
-31
lines changed

2 files changed

+107
-31
lines changed

src/index.tsx

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,15 @@ import {
77
GestureHandlerStateChangeNativeEvent
88
} from "react-native-gesture-handler";
99
import Animated from "react-native-reanimated";
10-
import { springFill } from "./procs";
10+
import {
11+
springFill,
12+
getOpenPct,
13+
getLeftActive,
14+
getMaxTranslate,
15+
getMinTranslate,
16+
getIsActive,
17+
getOnPanEvent
18+
} from "./procs";
1119
const {
1220
event,
1321
cond,
@@ -134,25 +142,21 @@ class SwipeableItem<T> extends React.PureComponent<Props<T>> {
134142
? this.props.snapPointsRight![this.props.snapPointsRight!.length - 1]
135143
: 0
136144
);
137-
percentOpenLeft = cond(
145+
146+
percentOpenLeft = getOpenPct(
138147
this.swipingLeft,
139-
divide(abs(this.animState.position), this.leftWidth)
148+
this.animState.position,
149+
this.leftWidth
140150
);
141-
percentOpenRight = cond(
151+
percentOpenRight = getOpenPct(
142152
this.swipingRight,
143-
divide(abs(this.animState.position), this.rightWidth)
144-
);
145-
isSwiping = or(this.swipingLeft, this.swipingRight);
146-
147-
leftActive = or(
148-
this.swipingLeft,
149-
and(not(this.isSwiping), lessThan(this.panX, 0))
153+
this.animState.position,
154+
this.rightWidth
150155
);
151156

152-
isActive = or(
153-
eq(this.gestureState, GestureState.ACTIVE),
154-
eq(this.gestureState, GestureState.BEGAN)
155-
);
157+
isSwiping = or(this.swipingLeft, this.swipingRight);
158+
leftActive = getLeftActive(this.swipingLeft, this.isSwiping, this.panX);
159+
isActive = getIsActive(this.gestureState);
156160

157161
onSwipeLeftChange = ([isSwiping]: readonly number[]) => {
158162
if (isSwiping) this.setState({ swipeDirection: OpenDirection.LEFT });
@@ -269,16 +273,15 @@ class SwipeableItem<T> extends React.PureComponent<Props<T>> {
269273
this.props.onChange({ open: OpenDirection.NONE, snapPoint: 0 });
270274
};
271275

272-
maxTranslate = cond(
276+
maxTranslate = getMaxTranslate(
273277
this.hasRight,
274-
add(this.rightWidth, this.props.overSwipe),
275-
0
278+
this.rightWidth,
279+
this.props.overSwipe
276280
);
277-
278-
minTranslate = cond(
281+
minTranslate = getMinTranslate(
279282
this.hasLeft,
280-
multiply(-1, add(this.leftWidth, this.props.overSwipe)),
281-
0
283+
this.leftWidth,
284+
this.props.overSwipe
282285
);
283286

284287
onHandlerStateChange = event([
@@ -296,13 +299,12 @@ class SwipeableItem<T> extends React.PureComponent<Props<T>> {
296299
set(this.panX, translationX),
297300
set(this.velocity, velocityX),
298301
set(this.tempTranslate, add(translationX, this.prevTranslate)),
299-
cond(
300-
and(
301-
eq(this.gestureState, GestureState.ACTIVE),
302-
lessOrEq(this.tempTranslate, this.maxTranslate),
303-
greaterOrEq(this.tempTranslate, this.minTranslate)
304-
),
305-
set(this.animState.position, this.tempTranslate)
302+
getOnPanEvent(
303+
this.gestureState,
304+
this.tempTranslate,
305+
this.maxTranslate,
306+
this.minTranslate,
307+
this.animState.position
306308
)
307309
])
308310
}

src/procs.tsx

Lines changed: 76 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,23 @@
11
import Animated from "react-native-reanimated";
2-
3-
let { proc, spring, Value } = Animated;
2+
import { State as GestureState } from "react-native-gesture-handler";
3+
const {
4+
cond,
5+
divide,
6+
abs,
7+
or,
8+
and,
9+
not,
10+
add,
11+
eq,
12+
set,
13+
lessThan,
14+
lessOrEq,
15+
greaterOrEq,
16+
multiply,
17+
proc,
18+
spring,
19+
Value
20+
} = Animated;
421

522
if (!proc) {
623
console.warn("Use reanimated > 1.3 for optimal perf");
@@ -46,6 +63,63 @@ const betterSpring = proc(
4663
)
4764
);
4865

66+
export const getOpenPct = proc(
67+
(
68+
isSwiping: Animated.Node<number>,
69+
pos: Animated.Node<number>,
70+
width: Animated.Node<number>
71+
) => cond(isSwiping, divide(abs(pos), width), 0)
72+
);
73+
74+
export const getLeftActive = proc(
75+
(
76+
swipingLeft: Animated.Node<number>,
77+
isSwiping: Animated.Node<number>,
78+
panX: Animated.Node<number>
79+
) => or(swipingLeft, and(not(isSwiping), lessThan(panX, 0)))
80+
);
81+
82+
export const getMaxTranslate = proc(
83+
(
84+
hasRight: Animated.Node<number>,
85+
rightWidth: Animated.Node<number>,
86+
overSwipe: number
87+
) => cond(hasRight, add(rightWidth, overSwipe), 0)
88+
);
89+
90+
export const getMinTranslate = proc(
91+
(
92+
hasLeft: Animated.Node<number>,
93+
leftWidth: Animated.Node<number>,
94+
overSwipe: number
95+
) => cond(hasLeft, multiply(-1, add(leftWidth, overSwipe)), 0)
96+
);
97+
98+
export const getIsActive = proc((gestureState: Animated.Node<GestureState>) =>
99+
or(
100+
eq(gestureState, GestureState.ACTIVE),
101+
eq(gestureState, GestureState.BEGAN)
102+
)
103+
);
104+
105+
export const getOnPanEvent = proc(
106+
(
107+
gestureState: Animated.Node<GestureState>,
108+
tempTranslate: Animated.Node<number>,
109+
maxTranslate: Animated.Node<number>,
110+
minTranslate: Animated.Node<number>,
111+
pos: Animated.Value<number>
112+
) =>
113+
cond(
114+
and(
115+
eq(gestureState, GestureState.ACTIVE),
116+
lessOrEq(tempTranslate, maxTranslate),
117+
greaterOrEq(tempTranslate, minTranslate)
118+
),
119+
set(pos, tempTranslate)
120+
)
121+
);
122+
49123
export function springFill(
50124
clock: Animated.Clock,
51125
state: Animated.SpringState,

0 commit comments

Comments
 (0)