@@ -6,6 +6,7 @@ import Animated, {
6
6
runOnUI ,
7
7
useAnimatedKeyboard ,
8
8
useAnimatedStyle ,
9
+ useDerivedValue ,
9
10
useSharedValue
10
11
} from 'react-native-reanimated' ;
11
12
import { useSafeAreaInsets } from 'react-native-safe-area-context' ;
@@ -43,30 +44,51 @@ export const KeyboardAvoidingView: React.FC<React.PropsWithChildren<Props>> = ({
43
44
[ setCurrentFrame ]
44
45
) ;
45
46
46
- const animatedStyles = useAnimatedStyle ( ( ) => {
47
- if ( ! currentFrame . value ) return { } ;
47
+ const offset = useDerivedValue ( ( ) => {
48
+ const frame = currentFrame . value ;
49
+ if ( ! frame ) return 0 ;
48
50
49
- const offset = Math . max (
50
- currentFrame . value . y +
51
- currentFrame . value . height -
51
+ return Math . max (
52
+ frame . y +
53
+ frame . height -
52
54
( screenHeight + topPadding - keyboard . height . value ) ,
53
55
0
54
56
) ;
57
+ } ) ;
55
58
59
+ const offsetStyle = useAnimatedStyle ( ( ) => {
56
60
if ( behavior === 'padding' ) {
57
- return { paddingBottom : offset } ;
61
+ return { paddingBottom : offset . value } ;
58
62
} 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 } ]
62
65
} ;
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 ;
63
82
} else {
64
83
return { } ;
65
84
}
66
85
} ) ;
67
86
68
87
return (
69
- < Animated . View onLayout = { onLayout } style = { [ style , animatedStyles ] } >
88
+ < Animated . View
89
+ onLayout = { onLayout }
90
+ style = { [ style , offsetStyle , resetPaddingStyle ] }
91
+ >
70
92
{ children }
71
93
</ Animated . View >
72
94
) ;
0 commit comments