React Native / Asynchronous and data ownership / Lesson 14
Keyboard occlusion and draft input
Keep a native input usable when the keyboard appears while preserving drafts and testing platform-specific layout.
On this page
Lesson goals
- Explain keyboard avoidance and available space.
- Keep an input controlled by its owner.
- Verify focus and layout on native targets.
Identify the occlusion
A virtual keyboard reduces or covers the area available to a form. The correct adjustment depends on the host, screen layout, platform and navigation chrome. Start with a reproducible screen and identify which field or action is hidden. KeyboardAvoidingView can adjust layout, but a single behavior or offset is not a universal solution for every device and configuration.
Keep the draft owned
This screen keeps the text in React state and places a scrollable form within a keyboard-avoiding container. Its Clear button is an explicit local action, not a server save. The example requires a React Native host and must be tested with a real or simulated keyboard. Production forms also need validation, error feedback and an appropriate submission contract.
import { useState } from "react";
import { Button, KeyboardAvoidingView, ScrollView, Text, TextInput } from "react-native";
export default function DraftScreen() {
const [draft, setDraft] = useState("");
return <KeyboardAvoidingView behavior="padding" style={{ flex: 1 }}><ScrollView keyboardShouldPersistTaps="handled"><Text>Draft note</Text><TextInput accessibilityLabel="Draft note" value={draft} onChangeText={setDraft} /><Button title="Clear" onPress={() => setDraft("")} /></ScrollView></KeyboardAvoidingView>;
}
Typing updates the draft; Clear empties it.
The input updates its owned draft while the container adjusts for the keyboard.
Account for surrounding chrome
A navigation header or other top inset may affect the appropriate keyboard vertical offset. The small wrapper accepts an offset from the host instead of inventing a device-independent constant. The caller must measure or otherwise determine the correct value for its navigation setup. The sample padding behavior is illustrative; compare supported behaviors on each intended platform.
import { KeyboardAvoidingView } from "react-native";
export function KeyboardFrame({ children, offset }) {
return <KeyboardAvoidingView behavior="padding" keyboardVerticalOffset={offset} style={{ flex: 1 }}>{children}</KeyboardAvoidingView>;
}
The wrapper uses the offset supplied by its caller.
The host supplies the offset and children while the wrapper owns the avoidance view.
Preserve focus and accessibility
Provide an accessible label, keep focused content visible and make the next action reachable when the keyboard is open. Test large text, a small viewport and a hardware keyboard as appropriate. Do not dismiss the keyboard or clear a draft merely to make a layout appear correct. Platform-specific window settings and navigation headers may also affect the result.
Verify on each target
Open the form on the intended Android and iOS targets, focus the last field, type a synthetic draft and reach its action. Change orientation or available size where supported, then dismiss and reopen the keyboard. Confirm that the draft survives and focus remains understandable. Record target, build mode and keyboard configuration; a unit test alone cannot prove physical keyboard occlusion is fixed.
Common errors and fixes
The submit action is hidden by the keyboard.
- Why it happens
- The screen does not account for reduced available space.
- How to fix it
- Adjust the layout and scrolling for the target host.
- Prevent it next time
- Test the last field with the keyboard open.
A draft disappears after keyboard dismissal.
- Why it happens
- Input state was tied to a transient view or reset during layout.
- How to fix it
- Keep the draft with its intended owner.
- Prevent it next time
- Test dismissal and reopening without submission.
The form shifts too far on one platform.
- Why it happens
- An offset or behavior was copied without accounting for host chrome.
- How to fix it
- Choose and verify the host-specific configuration.
- Prevent it next time
- Test each supported platform and screen size.
2 practice exercises
1. Why does the wrapper receive an offset from the host?
Show worked solution
The position of navigation chrome varies with the application. The host can supply the appropriate offset and verify it on each target instead of relying on a universal number.
2. Does a passing state test prove the keyboard no longer covers the form?
Show worked solution
No. It can verify draft ownership, but occlusion depends on native layout and keyboard behavior. Exercise the actual screen on the intended targets.
Key takeaways
- Keyboard avoidance depends on the native host.
- Preserve draft state and accessible focus.
- Verify occlusion on each intended target.