Skip to content

Commit cb4c1c0

Browse files
committed
Fix for KAV performance issues on New Arch
1 parent 2baaf3d commit cb4c1c0

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

src/usecase/shared/KeyboardAvoidingView.tsx

+32-10
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import Animated, {
66
runOnUI,
77
useAnimatedKeyboard,
88
useAnimatedStyle,
9+
useDerivedValue,
910
useSharedValue
1011
} from 'react-native-reanimated';
1112
import { useSafeAreaInsets } from 'react-native-safe-area-context';
@@ -43,30 +44,51 @@ export const KeyboardAvoidingView: React.FC<React.PropsWithChildren<Props>> = ({
4344
[setCurrentFrame]
4445
);
4546

46-
const animatedStyles = useAnimatedStyle(() => {
47-
if (!currentFrame.value) return {};
47+
const offset = useDerivedValue(() => {
48+
const frame = currentFrame.value;
49+
if (!frame) return 0;
4850

49-
const offset = Math.max(
50-
currentFrame.value.y +
51-
currentFrame.value.height -
51+
return Math.max(
52+
frame.y +
53+
frame.height -
5254
(screenHeight + topPadding - keyboard.height.value),
5355
0
5456
);
57+
});
5558

59+
const offsetStyle = useAnimatedStyle(() => {
5660
if (behavior === 'padding') {
57-
return { paddingBottom: offset };
61+
return { paddingBottom: offset.value };
5862
} else if (behavior === 'transform') {
59-
return {
60-
transform: [{ translateY: -offset }],
61-
paddingTop: keyboard.state.value === KeyboardState.OPEN ? offset : 0
63+
const style: ViewStyle = {
64+
transform: [{ translateY: -offset.value }]
6265
};
66+
if (keyboard.state.value === KeyboardState.OPEN) {
67+
style.paddingTop = offset.value;
68+
}
69+
return style;
70+
} else {
71+
return {};
72+
}
73+
});
74+
75+
const resetPaddingStyle = useAnimatedStyle(() => {
76+
if (behavior === 'transform') {
77+
const style: ViewStyle = {};
78+
if (keyboard.state.value !== KeyboardState.OPEN) {
79+
style.paddingTop = 0;
80+
}
81+
return style;
6382
} else {
6483
return {};
6584
}
6685
});
6786

6887
return (
69-
<Animated.View onLayout={onLayout} style={[style, animatedStyles]}>
88+
<Animated.View
89+
onLayout={onLayout}
90+
style={[style, offsetStyle, resetPaddingStyle]}
91+
>
7092
{children}
7193
</Animated.View>
7294
);

0 commit comments

Comments
 (0)