Command palette

@yasinsensoy/native

React Native · Components

KeyboardAwareScrollView

When the keyboard opens (or another field is focused while it is open), scrolls the focused text field above the keyboard; scrolling advances frame by frame together with the keyboard.

21 exports

Import

import {
  KeyboardAwareScrollView,
  KeyboardProvider,
  KeyboardAvoidingView,
  KeyboardStickyView,
  KeyboardSpacer,
  useKeyboard,
  useKeyboardAnimation,
  useKeyboardHandler,
  KeyboardController,
  hasNativeKeyboard,
  notifyInputFocus,
} from "@yasinsensoy/native";

When the keyboard opens (or another field is focused while it is open), scrolls the focused text field above the keyboard; scrolling advances frame by frame together with the keyboard. Space as tall as the keyboard is added at the end of the content: the bottom fields become visible too. Use it instead of ScrollView for forms.

KeyboardAwareScrollView({ bottomOffset, extraKeyboardSpace, enabled, onScroll, onLayout, onTouchEnd, scrollEventThrottle, children, ref, ...rest }: KeyboardAwareScrollViewProps): JSX.Element

Examples

<KeyboardAwareScrollView contentContainerStyle={{ padding: 16, gap: 12 }}>
  <TextField label="Name" />
  <TextField label="Address" multiline />
</KeyboardAwareScrollView>

Starts keyboard tracking. UIProvider places it itself; only apps not using UIProvider add it by hand. When nested, only the outermost one works.

KeyboardProvider({ children, style }: KeyboardProviderProps): JSX.Element

Keeps content above the keyboard when it opens. Unlike React Native's component of the same name: on Android too it advances frame by frame together with the keyboard; it measures the view's position on screen itself (no need to compute keyboardVerticalOffset).

KeyboardAvoidingView({ behavior, offset, enabled, style, onLayout, ref, ...rest }: KeyboardAvoidingViewProps): JSX.Element

Examples

<KeyboardAvoidingView style={{ flex: 1 }}>
  <Messages />
  <Composer />
</KeyboardAvoidingView>

Sticks to the keyboard: moves up with it when it opens (layout does not change, native driver). For a chat composer, a bottom action bar, a "Continue" button.

KeyboardStickyView({ offset, enabled, style, onLayout, ref, ...rest }: KeyboardStickyViewProps): JSX.Element

Examples

<KeyboardStickyView style={{ position: "absolute", left: 0, right: 0, bottom: 0 }}>
  <Button>Continue</Button>
</KeyboardStickyView>

An empty view as tall as the area the keyboard covers. Put it at the very bottom of the screen: the content above fits the remaining space when the keyboard opens.

KeyboardSpacer({ offset, style, ...rest }: KeyboardSpacerProps): JSX.Element

useKeyboard

function

Keyboard state: whether it is open, how much space it takes (once the animation ends), and whether an animation is running. Re-renders only at the start and end of the animation; for per-frame values see useKeyboardAnimation.

useKeyboard(): KeyboardState

Examples

const { visible } = useKeyboard();
return visible ? null : <TabBar />;

Animation values that advance frame by frame with the keyboard. On Android too it follows the keyboard's own animation like on iOS (a native view).

useKeyboardAnimation(): KeyboardAnimation

Examples

const { height } = useKeyboardAnimation();
<Animated.View style={{ transform: [{ translateY: Animated.multiply(height, -1) }] }} />

Listens to keyboard events: onStart (with the final height), onMove every frame, onEnd. Handlers are updated on every render; no need to resubscribe.

useKeyboardHandler(handler: KeyboardHandler): void

Examples

useKeyboardHandler({
  onStart: (e) => e.target > 0 && listRef.current?.scrollToEnd(),
  onMove: (e) => console.log(e.height),
});

Controls the keyboard programmatically.

NameTypeDescription
dismiss() => Promise<void>Dismisses the keyboard (blurs the focused field). Resolves once the keyboard is fully closed; immediately if it is already closed.
isVisible() => booleanWhether the keyboard is open (or opening).
state() => KeyboardStateSnapshot state.
addListener(handler: KeyboardHandler) => () => voidListens to keyboard events; returns a function that ends the subscription. In components use useKeyboardHandler.

Whether the native keyboard view exists in this build. If not (web, Expo Go, an app whose native code was not rebuilt), React Native's Keyboard events are used: the behavior is the same, only on Android the animation starts after the keyboard has opened.

hasNativeKeyboard(): boolean

Reports that a text field was focused: when another field is focused while the keyboard is already open, KeyboardAwareScrollView scrolls the new field into view. The library's fields call it themselves; you can call it in onFocus of your own TextInputs (if you do not, it is still caught after the touch).

notifyInputFocus(): void

Props of KeyboardAwareScrollView.

Extends: ScrollViewProps

NameTypeDescription
bottomOffset?numberSpace left between the focused field and the top of the keyboard. Default 24.
extraKeyboardSpace?numberExtra space added at the end of the content while the keyboard is open.
enabled?booleanfalse: behaves like a regular ScrollView.
ref?React.Ref<ScrollViewRef>ref of the scroll view.
children?ReactNodeContent.

Value of useKeyboardAnimation.

NameTypeDescription
heightAnimated.ValueHeight by which the keyboard covers the bottom of the app root. Animated with the native driver: only for transform and opacity (e.g. translateY: Animated.multiply(height, -1)).
progressAnimated.Value0 (closed) – 1 (open); native driver, for transform/opacity.
layout{ /** Height (for layout props). */ height: Animated.Value; /** 0–1 (for layout props). */ progress: Animated.Value; }The same values updated in JS every frame: for layout props like height, padding, margin (the native driver cannot animate them).

Props of KeyboardProvider.

NameTypeDescription
children?ReactNodeContent.
style?StyleProp<ViewStyle>Style of the root view (default flex: 1).

A moment of keyboard movement.

NameTypeDescription
heightnumberHeight by which the keyboard covers the bottom of the app root (dp / pt).
progressnumber0 (closed) – 1 (fully open).
targetnumberHeight reached when the animation ends.
durationnumberAnimation duration (ms); only meaningful in the onStart event.

Listeners of keyboard events; all optional.

NameTypeDescription
onStart?(event: KeyboardMoveEvent) => voidThe keyboard started opening, closing or resizing (target is the final height).
onMove?(event: KeyboardMoveEvent) => voidOn every frame of the animation.
onEnd?(event: KeyboardMoveEvent) => voidAnimation finished.

KeyboardState

interface

Snapshot state of the keyboard (changes only at the start and end of the animation).

NameTypeDescription
visiblebooleanWhether the keyboard is open (or opening).
heightnumberHeight by which the keyboard covers the bottom of the app root (once the animation ends).
animatingbooleanWhether an animation is running.
durationnumberDuration of the last animation (ms).

What the view does when the keyboard opens.

type KeyboardAvoidingBehavior = /** Bottom padding grows by the keyboard height; content fits the remaining space (layout changes). */ "padding" /** The view moves up by the keyboard height (layout does not change, native driver). */ | "translate"

Props of KeyboardAvoidingView.

Extends: ViewProps

NameTypeDescription
behavior?KeyboardAvoidingBehaviorDefault "padding".
offset?numberExtra space added while the keyboard is open (may be negative, e.g. the bottom tab bar height).
enabled?booleanfalse: does not react to the keyboard.
ref?React.Ref<ViewRef>ref of the view.

Props of KeyboardStickyView.

Extends: ViewProps

NameTypeDescription
offset?{ closed?: number; opened?: number; }Extra lift (positive is up): closed while the keyboard is closed, opened while open. For a bottom bar with safe area padding, { opened: -insets.bottom } removes that padding while the keyboard is open (the bar sits on the keyboard).
enabled?booleanfalse: stays in place.
ref?React.Ref<ViewRef>ref of the view.

Props of KeyboardSpacer.

Extends: ViewProps

NameTypeDescription
offset?numberExtra height added while the keyboard is open (may be negative).