-
-
Notifications
You must be signed in to change notification settings - Fork 452
Expand file tree
/
Copy pathanimatedValueContext.tsx
More file actions
228 lines (195 loc) · 6.24 KB
/
animatedValueContext.tsx
File metadata and controls
228 lines (195 loc) · 6.24 KB
1
2
3
4
5
6
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import React, { useMemo, useEffect, useCallback, useContext } from "react";
import {
useAnimatedReaction,
useDerivedValue,
useSharedValue,
} from "react-native-reanimated";
import { State as GestureState } from "react-native-gesture-handler";
import { useProps } from "./propsContext";
const AnimatedValueContext = React.createContext<
ReturnType<typeof useSetupAnimatedValues> | undefined
>(undefined);
export default function AnimatedValueProvider({
children,
}: {
children: React.ReactNode;
}) {
const value = useSetupAnimatedValues();
return (
<AnimatedValueContext.Provider value={value}>
{children}
</AnimatedValueContext.Provider>
);
}
export function useAnimatedValues() {
const value = useContext(AnimatedValueContext);
if (!value) {
throw new Error(
"useAnimatedValues must be called from within AnimatedValueProvider!"
);
}
return value;
}
function useSetupAnimatedValues<T>() {
const props = useProps<T>();
const DEFAULT_VAL = useSharedValue(0);
const containerSize = useSharedValue(0);
const scrollViewSize = useSharedValue(0);
const panGestureState = useSharedValue<GestureState>(
GestureState.UNDETERMINED
);
const touchTranslate = useSharedValue(0);
const isTouchActiveNative = useSharedValue(false);
const hasMoved = useSharedValue(0);
const disabled = useSharedValue(false);
const horizontalAnim = useSharedValue(!!props.horizontal);
const activeIndexAnim = useSharedValue(-1); // Index of hovering cell
const spacerIndexAnim = useSharedValue(-1); // Index of hovered-over cell
const activeCellSize = useSharedValue(0); // Height or width of acctive cell
const activeCellOffset = useSharedValue(0); // Distance between active cell and edge of container
const scrollOffset = useSharedValue(0);
const scrollInit = useSharedValue(0);
const viewableIndexMin = useSharedValue(0);
const viewableIndexMax = useSharedValue(0);
// If list is nested there may be an outer scrollview
const outerScrollOffset = props.outerScrollOffset || DEFAULT_VAL;
const outerScrollInit = useSharedValue(0);
useAnimatedReaction(
() => {
return activeIndexAnim.value;
},
(cur, prev) => {
if (cur !== prev && cur >= 0) {
scrollInit.value = scrollOffset.value;
outerScrollInit.value = outerScrollOffset.value;
}
},
[outerScrollOffset]
);
const placeholderOffset = useSharedValue(0);
const isDraggingCell = useDerivedValue(() => {
return isTouchActiveNative.value && activeIndexAnim.value >= 0;
}, []);
useAnimatedReaction(
() => {
return outerScrollOffset.value;
},
(cur, prev) => {
if (isDraggingCell.value) {
return;
}
outerScrollInit.value = cur;
},
[outerScrollInit, isDraggingCell]
);
const autoScrollDistance = useDerivedValue(() => {
if (!isDraggingCell.value) return 0;
const innerScrollDiff = scrollOffset.value - scrollInit.value;
// If list is nested there may be an outer scroll diff
const outerScrollDiff = outerScrollOffset.value - outerScrollInit.value;
const scrollDiff = innerScrollDiff + outerScrollDiff;
return scrollDiff;
}, []);
const touchPositionDiff = useDerivedValue(() => {
const extraTranslate = isTouchActiveNative.value
? autoScrollDistance.value
: 0;
return touchTranslate.value + extraTranslate;
}, []);
const touchPositionDiffConstrained = useDerivedValue(() => {
const containerMinusActiveCell =
containerSize.value - activeCellSize.value + scrollOffset.value;
const offsetRelativeToScrollTop =
touchPositionDiff.value + activeCellOffset.value;
const constrained = Math.min(
containerMinusActiveCell,
Math.max(scrollOffset.value, offsetRelativeToScrollTop)
);
const maxTranslateNegative = -activeCellOffset.value;
const maxTranslatePositive =
scrollViewSize.value - (activeCellOffset.value + activeCellSize.value);
// Only constrain the touch position while the finger is on the screen. This allows the active cell
// to snap above/below the fold once let go, if the drag ends at the top/bottom of the screen.
const constrainedBase = isTouchActiveNative.value
? constrained - activeCellOffset.value
: touchPositionDiff.value;
// Make sure item is constrained to the boundaries of the scrollview
return Math.min(
Math.max(constrainedBase, maxTranslateNegative),
maxTranslatePositive
);
}, []);
const hoverAnim = useDerivedValue(() => {
if (activeIndexAnim.value < 0) return 0;
return props.dragItemOverflow
? touchPositionDiff.value
: touchPositionDiffConstrained.value;
}, []);
const hoverOffset = useDerivedValue(() => {
return hoverAnim.value + activeCellOffset.value;
}, [hoverAnim, activeCellOffset]);
useDerivedValue(() => {
// Reset spacer index when we stop hovering
const isHovering = activeIndexAnim.value >= 0;
if (!isHovering && spacerIndexAnim.value >= 0) {
spacerIndexAnim.value = -1;
}
}, []);
// Note: this could use a refactor as it combines touch state + cell animation
const resetTouchedCell = useCallback(() => {
activeCellOffset.value = 0;
hasMoved.value = 0;
}, []);
const value = useMemo(
() => ({
activeCellOffset,
activeCellSize,
activeIndexAnim,
containerSize,
disabled,
horizontalAnim,
hoverAnim,
hoverOffset,
isDraggingCell,
isTouchActiveNative,
panGestureState,
placeholderOffset,
resetTouchedCell,
scrollOffset,
scrollViewSize,
spacerIndexAnim,
touchPositionDiff,
touchTranslate,
autoScrollDistance,
viewableIndexMin,
viewableIndexMax,
}),
[
activeCellOffset,
activeCellSize,
activeIndexAnim,
containerSize,
disabled,
horizontalAnim,
hoverAnim,
hoverOffset,
isDraggingCell,
isTouchActiveNative,
panGestureState,
placeholderOffset,
resetTouchedCell,
scrollOffset,
scrollViewSize,
spacerIndexAnim,
touchPositionDiff,
touchTranslate,
autoScrollDistance,
viewableIndexMin,
viewableIndexMax,
]
);
useEffect(() => {
props.onAnimValInit?.(value);
}, [value]);
return value;
}