Skip to content

Instantly share code, notes, and snippets.

@felippepuhle
Last active January 15, 2024 13:26
Show Gist options
  • Save felippepuhle/4c74f918c5eba24093bd0502ceb7a845 to your computer and use it in GitHub Desktop.
Save felippepuhle/4c74f918c5eba24093bd0502ceb7a845 to your computer and use it in GitHub Desktop.
import { useState } from 'react';
import { Dimensions, KeyboardAvoidingViewProps, Platform, StyleSheet, View } from 'react-native';
import { useSafeAreaInsets } from 'react-native-safe-area-context';
import CommonKeyboardAvoidingView from '../KeyboardAvoidingView';
const { height: DEVICE_HEIGHT } = Dimensions.get('screen');
const IOSKeyboardAvoidingView = (props: KeyboardAvoidingViewProps) => {
const insets = useSafeAreaInsets();
const [screenHeight, setScreenHeight] = useState(0);
const modalOffsetFromTop = DEVICE_HEIGHT - screenHeight;
return (
<View
style={styles.container}
onLayout={(event) => {
setScreenHeight(event.nativeEvent.layout.height);
}}
>
{screenHeight ? (
<CommonKeyboardAvoidingView {...props} keyboardVerticalOffset={modalOffsetFromTop - insets.bottom} />
) : null}
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
},
});
const KeyboardAvoidingView =
Platform.OS === 'ios' ? IOSKeyboardAvoidingView : CommonKeyboardAvoidingView;
export default KeyboardAvoidingView;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment