|
| 1 | +--- |
| 2 | +keywords: [react-native-keyboard-controller, KeyboardAvoidingView, keyboard avoiding view, avoid keyboard, android] |
| 3 | +--- |
| 4 | + |
| 5 | +# KeyboardAvoidingView |
| 6 | + |
| 7 | +This component will automatically adjust its height, position, or bottom padding based on the keyboard height to remain visible while the virtual keyboard is displayed. |
| 8 | + |
| 9 | +## Why another `KeyboardAvoidingView` is needed? |
| 10 | + |
| 11 | +This new `KeyboardAvoidingView` maintains the familiar React Native [API](https://reactnative.dev/docs/keyboardavoidingview) but ensures consistent behavior and animations on both `iOS` and `Android` platforms. Unlike the existing solution, which primarily caters to `iOS`, this component eliminates platform discrepancies, providing a unified user experience. By reproducing the same animations and behaviors on both platforms, it simplifies cross-platform development, meets user expectations for consistency, and enhances code maintainability. Ultimately, it addresses the need for a reliable and uniform keyboard interaction solution across different devices. |
| 12 | + |
| 13 | +Below is a visual difference between the implementations (the animation is _**4x**_ times slower for better visual perception). |
| 14 | + |
| 15 | +import KeyboardAvoidingViewComparison from '../../../src/components/KeyboardAvoidingViewComparison' |
| 16 | + |
| 17 | +<KeyboardAvoidingViewComparison /> |
| 18 | + |
| 19 | +:::info Found a bug? Help the project and report it! |
| 20 | + |
| 21 | +If you found any bugs or inconsistent behavior comparing to `react-native` implementation - don't hesitate to open an [issue](https://github.com/kirillzyusko/react-native-keyboard-controller/issues/new?assignees=kirillzyusko&labels=bug&template=bug_report.md&title=). It will help the project 🙏 |
| 22 | + |
| 23 | +Also if there is any well-known problems in original `react-native` implementation which can not be fixed for a long time and they are present in this implementation as well - also feel free to submit an [issue](https://github.com/kirillzyusko/react-native-keyboard-controller/issues/new?assignees=kirillzyusko&labels=bug&template=bug_report.md&title=). Let's make this world better together 😎 |
| 24 | + |
| 25 | +::: |
| 26 | + |
| 27 | +## Example |
| 28 | + |
| 29 | +```tsx |
| 30 | +import React from 'react'; |
| 31 | +import { |
| 32 | + Text, |
| 33 | + TextInput, |
| 34 | + TouchableOpacity, |
| 35 | + View, |
| 36 | + StyleSheet, |
| 37 | +} from 'react-native'; |
| 38 | +import { KeyboardAvoidingView } from 'react-native-keyboard-controller'; |
| 39 | + |
| 40 | +export default function KeyboardAvoidingViewExample() { |
| 41 | + return ( |
| 42 | + <KeyboardAvoidingView |
| 43 | + behavior={'padding'} |
| 44 | + contentContainerStyle={styles.container} |
| 45 | + keyboardVerticalOffset={100} |
| 46 | + style={styles.content} |
| 47 | + > |
| 48 | + <View style={styles.inner}> |
| 49 | + <Text style={styles.heading}>Header</Text> |
| 50 | + <View> |
| 51 | + <TextInput placeholder="Username" style={styles.textInput} /> |
| 52 | + <TextInput placeholder="Password" style={styles.textInput} /> |
| 53 | + </View> |
| 54 | + <TouchableOpacity style={styles.button}> |
| 55 | + <Text style={styles.text}>Submit</Text> |
| 56 | + </TouchableOpacity> |
| 57 | + </View> |
| 58 | + </KeyboardAvoidingView> |
| 59 | + ); |
| 60 | +} |
| 61 | + |
| 62 | +const styles = StyleSheet.create({ |
| 63 | + container: { |
| 64 | + flex: 1, |
| 65 | + }, |
| 66 | + content: { |
| 67 | + flex: 1, |
| 68 | + maxHeight: 600, |
| 69 | + }, |
| 70 | + heading: { |
| 71 | + fontSize: 36, |
| 72 | + marginBottom: 48, |
| 73 | + fontWeight: '600', |
| 74 | + }, |
| 75 | + inner: { |
| 76 | + padding: 24, |
| 77 | + flex: 1, |
| 78 | + justifyContent: 'space-between', |
| 79 | + }, |
| 80 | + textInput: { |
| 81 | + height: 45, |
| 82 | + borderColor: '#000000', |
| 83 | + borderWidth: 1, |
| 84 | + borderRadius: 10, |
| 85 | + marginBottom: 36, |
| 86 | + paddingLeft: 10, |
| 87 | + }, |
| 88 | + button: { |
| 89 | + marginTop: 12, |
| 90 | + height: 45, |
| 91 | + borderRadius: 10, |
| 92 | + backgroundColor: 'rgb(40, 64, 147)', |
| 93 | + justifyContent: 'center', |
| 94 | + alignItems: 'center', |
| 95 | + }, |
| 96 | + text: { |
| 97 | + fontWeight: '500', |
| 98 | + fontSize: 16, |
| 99 | + color: 'white', |
| 100 | + }, |
| 101 | +}); |
| 102 | +``` |
| 103 | + |
| 104 | +## Props |
| 105 | + |
| 106 | +### `behavior` |
| 107 | + |
| 108 | +Specify how to react to the presence of the keyboard. Could be one value of: |
| 109 | + |
| 110 | +- `position` |
| 111 | +- `padding` |
| 112 | +- `height` |
| 113 | + |
| 114 | +### `contentContainerStyle` |
| 115 | + |
| 116 | +The style of the content container (View) when behavior is `position`. |
| 117 | + |
| 118 | +### `enabled` |
| 119 | + |
| 120 | +A boolean prop indicating whether `KeyboardAvoidingView` is enabled or disabled. Default is `true`. |
| 121 | + |
| 122 | +### `keyboardVerticalOffset` |
| 123 | + |
| 124 | +This is the distance between the top of the user screen and the react native view, may be non-zero in some use cases. Default is `0`. |
0 commit comments