Skip to main content

KeyboardAvoidingWrapper

Zero-config wrapper that keeps form content visible when the keyboard opens. Uses automaticallyAdjustKeyboardInsets on iOS and KeyboardAvoidingView on Android.

Usage

import { KeyboardAvoidingWrapper, TextField } from '@onlynative/components'
import { ThemeProvider } from '@onlynative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { useState } from 'react'

export default function App() {
const [name, setName] = useState('')
const [email, setEmail] = useState('')
return (
<SafeAreaProvider>
<ThemeProvider>
<KeyboardAvoidingWrapper>
<TextField label="Name" value={name} onChangeText={setName} />
<TextField label="Email" value={email} onChangeText={setEmail} />
</KeyboardAvoidingWrapper>
</ThemeProvider>
</SafeAreaProvider>
)
}

How it works

PlatformStrategy
iOSScrollView with automaticallyAdjustKeyboardInsets
AndroidKeyboardAvoidingView with configurable behavior

The inner ScrollView uses keyboardShouldPersistTaps="handled" so taps on buttons still work while the keyboard is open.

Keyboard events

Listen to keyboard show/hide events:

import { KeyboardAvoidingWrapper, TextField, Typography } from '@onlynative/components'
import { ThemeProvider } from '@onlynative/core'
import { SafeAreaProvider } from 'react-native-safe-area-context'
import { useState } from 'react'

export default function App() {
const [status, setStatus] = useState('idle')
return (
<SafeAreaProvider>
<ThemeProvider>
<KeyboardAvoidingWrapper
onKeyboardShow={(e) => setStatus(`shown @ ${e.endCoordinates.height}`)}
onKeyboardHide={() => setStatus('hidden')}
>
<Typography variant="bodyMedium">Status: {status}</Typography>
<TextField label="Tap to focus" />
</KeyboardAvoidingWrapper>
</ThemeProvider>
</SafeAreaProvider>
)
}

On iOS these fire on keyboardWillShow / keyboardWillHide; on Android they fire on keyboardDidShow / keyboardDidHide.

Props

PropTypeDefaultRequiredDescription
behaviorenumpaddingNoKeyboard avoidance strategy.
keyboardVerticalOffsetnumber0NoExtra offset added to the keyboard height calculation. Useful for accounting for headers or tab bars.
enabledbooleanNoEnable or disable the keyboard avoiding behavior.
scrollViewPropsScrollViewProps-NoProps forwarded to the inner `ScrollView`.
onKeyboardShow(event: KeyboardEvent) => void-NoCalled when the keyboard is about to show (iOS) or has shown (Android).
onKeyboardHide(event: KeyboardEvent) => void-NoCalled when the keyboard is about to hide (iOS) or has hidden (Android).
styleStyleProp<ViewStyle>-NoStyle applied to the outer `KeyboardAvoidingView`.
contentContainerStyleStyleProp<ViewStyle>-NoStyle applied to the inner `ScrollView` contentContainerStyle.