Skip to content

Commit 041d24f

Browse files
authored
docs: better keyboardVerticalOffset for KeyboardAvoidingView explanation (#890)
## 📜 Description Added a better explanation of what `keyboardVerticalOffset` does and when to use it. ## 💡 Motivation and Context Clearer documentation -> less bug reports 😎 Closes #887 ## 📢 Changelog <!-- High level overview of important changes --> <!-- For example: fixed status bar manipulation; added new types declarations; --> <!-- If your changes don't affect one of platform/language below - then remove this platform/language --> ### Docs - added better explanation of `keyboardVerticalOffset property`; ## 🤔 How Has This Been Tested? Tested via preview. ## 📸 Screenshots (if appropriate): <img width="1003" alt="image" src="https://github.com/user-attachments/assets/19a63ae9-305d-4828-be4a-a8af69444e11" /> ## 📝 Checklist - [x] CI successfully passed - [x] I added new mocks and corresponding unit-tests if library API was changed
1 parent d379ff5 commit 041d24f

File tree

6 files changed

+136
-2
lines changed

6 files changed

+136
-2
lines changed

docs/docs/api/components/keyboard-avoiding-view.mdx

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,57 @@ A boolean prop indicating whether `KeyboardAvoidingView` is enabled or disabled.
132132

133133
### `keyboardVerticalOffset`
134134

135-
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`.
135+
This is the distance between the top of the user screen and the react native view This is particularly useful when there are fixed headers, navigation bars, or other UI elements at the top of the screen. Default is `0`.
136+
137+
import KeyboardVerticalOffset from "@site/src/components/KeyboardVerticalOffset";
138+
139+
<details>
140+
<summary>When to use `keyboardVerticalOffset`?</summary>
141+
142+
You should use `keyboardVerticalOffset` in the following scenarios:
143+
144+
- **Navigation Bars / Headers** - If your screen is inside a `Stack.Navigator` from `react-navigation`, the header height should be compensated using `keyboardVerticalOffset`:
145+
146+
```tsx
147+
import { useHeaderHeight } from "@react-navigation/elements";
148+
import { KeyboardAvoidingView } from "react-native-keyboard-controller";
149+
150+
const MyScreen = () => {
151+
const headerHeight = useHeaderHeight();
152+
153+
return (
154+
<KeyboardAvoidingView
155+
behavior="padding"
156+
keyboardVerticalOffset={headerHeight}
157+
>
158+
<TextInput placeholder="Type here..." />
159+
</KeyboardAvoidingView>
160+
);
161+
};
162+
```
163+
164+
- **Custom Toolbars or Fixed Elements at the Top** - If your app has a fixed toolbar, status bar, or other UI elements at the top, you should offset accordingly.
165+
166+
- **Modal Screens with Different Layouts** - When using `KeyboardAvoidingView` inside a `Modal`, you may need to manually define the vertical offset to account for the modal’s positioning.
167+
168+
Below shown a visual representation of `keyboardVerticalOffset`:
169+
170+
<KeyboardVerticalOffset />
171+
172+
:::warning Translucent `StatusBar`
173+
If you are using translucent `StatusBar` on Android, then `StatusBar` padding gets handled by `react-navigation` automatically and it will include safe-area padding in `useHeaderHeight` (i. e. `StatusBar` height will be included in the returned value and it will match iOS behavior).
174+
175+
If you are not using translucent `StatusBar`, then you need to include `StatusBar` height yourself:
176+
177+
```tsx
178+
const headerHeight = useHeaderHeight() + (StatusBar.currentHeight ?? 0);
179+
```
180+
181+
<blockquote>
182+
`StatusBar.currentHeight` is Android-only prop so you can use `?? 0` and don't
183+
write platform-specific code using `Platform.OS` or `Platform.select`.
184+
</blockquote>
185+
186+
:::
187+
188+
</details>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useColorMode } from "@docusaurus/theme-common";
2+
import React from "react";
3+
4+
import KeyboardVerticalOffsetDark from "./kvo-dark.svg";
5+
import KeyboardVerticalOffsetLight from "./kvo-light.svg";
6+
7+
export default function KeyboardVerticalOffsetIcon() {
8+
const { colorMode } = useColorMode();
9+
10+
return (
11+
<div className="center">
12+
{colorMode === "dark" ? (
13+
<KeyboardVerticalOffsetDark className="svg" />
14+
) : (
15+
<KeyboardVerticalOffsetLight className="svg" />
16+
)}
17+
</div>
18+
);
19+
}
Lines changed: 2 additions & 0 deletions
Loading

0 commit comments

Comments
 (0)