Skip to content

Commit 791801b

Browse files
committed
perf: remove useAnimatedReaction in favor of inline decision tree
1 parent e9eed45 commit 791801b

1 file changed

Lines changed: 13 additions & 74 deletions

File tree

package/src/contexts/overlayContext/MessageOverlayHostLayer.tsx

Lines changed: 13 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import { Gesture, GestureDetector } from 'react-native-gesture-handler';
1111
import Animated, {
1212
clamp,
1313
runOnJS,
14-
useAnimatedReaction,
1514
useAnimatedStyle,
1615
useDerivedValue,
1716
useSharedValue,
@@ -65,9 +64,6 @@ export const MessageOverlayHostLayer = ({ BackgroundComponent }: MessageOverlayH
6564
const topH = useSharedValue<Rect>(undefined);
6665
const bottomH = useSharedValue<Rect>(undefined);
6766
const closeCorrectionY = useSharedValue(0);
68-
const topVisualY = useSharedValue(0);
69-
const messageVisualY = useSharedValue(0);
70-
const bottomVisualY = useSharedValue(0);
7167

7268
const topInset = insets.top;
7369
// Due to edge-to-edge in combination with various libraries, Android sometimes reports
@@ -99,9 +95,6 @@ export const MessageOverlayHostLayer = ({ BackgroundComponent }: MessageOverlayH
9995
topH.value = undefined;
10096
bottomH.value = undefined;
10197
closeCorrectionY.value = 0;
102-
topVisualY.value = 0;
103-
messageVisualY.value = 0;
104-
bottomVisualY.value = 0;
10598
},
10699
setBottomH: (rect) => {
107100
bottomH.value = rect;
@@ -113,7 +106,7 @@ export const MessageOverlayHostLayer = ({ BackgroundComponent }: MessageOverlayH
113106
topH.value = rect;
114107
},
115108
}),
116-
[bottomH, bottomVisualY, closeCorrectionY, messageH, messageVisualY, topH, topVisualY],
109+
[bottomH, closeCorrectionY, messageH, topH],
117110
);
118111

119112
useEffect(() => {
@@ -163,85 +156,27 @@ export const MessageOverlayHostLayer = ({ BackgroundComponent }: MessageOverlayH
163156
return solvedBottomTop - bottomH.value.y;
164157
});
165158

166-
useAnimatedReaction(
167-
() => {
168-
if (!topH.value) return undefined;
169-
return isActive ? (closing ? closeCorrectionY.value : messageShiftY.value) : 0;
170-
},
171-
(next, previous) => {
172-
if (next === undefined) {
173-
topVisualY.value = 0;
174-
return;
175-
}
176-
177-
if (previous === undefined) {
178-
topVisualY.value = next;
179-
return;
180-
}
181-
182-
topVisualY.value = withSpring(next, { duration: DURATION });
183-
},
184-
[isActive, closing],
185-
);
186-
187-
useAnimatedReaction(
188-
() => {
189-
if (!messageH.value) return undefined;
190-
return isActive ? (closing ? closeCorrectionY.value : messageShiftY.value) : 0;
191-
},
192-
(next, previous) => {
193-
if (next === undefined) {
194-
messageVisualY.value = 0;
195-
return;
196-
}
197-
198-
if (previous === undefined) {
199-
messageVisualY.value = next;
200-
return;
201-
}
202-
203-
messageVisualY.value = withSpring(next, { duration: DURATION });
204-
},
205-
[isActive, closing],
206-
);
207-
208-
useAnimatedReaction(
209-
() => {
210-
if (!bottomH.value) return undefined;
211-
return isActive ? (closing ? closeCorrectionY.value : bottomShiftY.value) : 0;
212-
},
213-
(next, previous) => {
214-
if (next === undefined) {
215-
bottomVisualY.value = 0;
216-
return;
217-
}
218-
219-
if (previous === undefined) {
220-
bottomVisualY.value = next;
221-
return;
222-
}
223-
224-
bottomVisualY.value = withSpring(next, { duration: DURATION });
225-
},
226-
[isActive, closing],
227-
);
228-
229159
const topItemStyle = useAnimatedStyle(() => {
230160
if (!topH.value) return { opacity: 0, height: 0, width: 0 };
161+
const translateY = isActive ? (closing ? closeCorrectionY.value : messageShiftY.value) : 0;
231162
const horizontalPosition = I18nManager.isRTL ? { right: topH.value.x } : { left: topH.value.x };
232163
return {
233164
height: topH.value.h,
234165
opacity: 1,
235166
position: 'absolute',
236167
top: topH.value.y,
237-
transform: [{ scale: backdrop.value }, { translateY: topVisualY.value }],
168+
transform: [
169+
{ scale: backdrop.value },
170+
{ translateY: withSpring(translateY, { duration: DURATION }) },
171+
],
238172
width: topH.value.w,
239173
...horizontalPosition,
240174
};
241175
});
242176

243177
const bottomItemStyle = useAnimatedStyle(() => {
244178
if (!bottomH.value) return { opacity: 0, height: 0, width: 0 };
179+
const translateY = isActive ? (closing ? closeCorrectionY.value : bottomShiftY.value) : 0;
245180
const horizontalPosition = I18nManager.isRTL
246181
? { right: bottomH.value.x }
247182
: { left: bottomH.value.x };
@@ -250,20 +185,24 @@ export const MessageOverlayHostLayer = ({ BackgroundComponent }: MessageOverlayH
250185
opacity: 1,
251186
position: 'absolute',
252187
top: bottomH.value.y,
253-
transform: [{ scale: backdrop.value }, { translateY: bottomVisualY.value }],
188+
transform: [
189+
{ scale: backdrop.value },
190+
{ translateY: withSpring(translateY, { duration: DURATION }) },
191+
],
254192
width: bottomH.value.w,
255193
...horizontalPosition,
256194
};
257195
});
258196

259197
const hostStyle = useAnimatedStyle(() => {
260198
if (!messageH.value) return { height: 0 };
199+
const translateY = isActive ? (closing ? closeCorrectionY.value : messageShiftY.value) : 0;
261200
return {
262201
height: messageH.value.h,
263202
left: messageH.value.x,
264203
position: 'absolute',
265204
top: messageH.value.y,
266-
transform: [{ translateY: messageVisualY.value }],
205+
transform: [{ translateY: withSpring(translateY, { duration: DURATION }) }],
267206
width: messageH.value.w,
268207
};
269208
});

0 commit comments

Comments
 (0)