React Native Keyboard Composer

repository·main·Indexed 43 days ago

https://github.com/launchtodayhq/react-native-keyboard-composer

A native keyboard-aware composer component for React Native (v1.0.1) providing smooth keyboard animations and intelligent input components similar to iMessage. Includes the KeyboardComposer for text input and KeyboardAwareWrapper for managing scroll view behavior during keyboard transitions.

Tokens
2.9K
Snippets
12
Records
16
Agent score
51%

What's inside React Native Keyboard Composer

  1. Core components demonstrated in the example

    main

    The example application showcases three primary components/concepts from the library:

    1. KeyboardComposer: A native text input component designed for smooth keyboard tracking and animations.
    2. KeyboardAwareWrapper: A wrapper for scroll views that manages intelligent keyboard behavior:
      • If the view is at the bottom when the keyboard opens, it stays at the bottom.
      • If the view is scrolled up when the keyboard opens, the keyboard opens over the content without forcing a scroll.
    3. LegendList: Used in the example as a high-performance list for rendering chat messages.
  2. Develop the library using local source

    main

    If you are contributing to the @launchhq/react-native-keyboard-composer library and want the example app to use your local unreleased code instead of the npm package, use the local development commands.

    Note: If you need TypeScript path mapping to point to the local ../src directory, temporarily copy tsconfig.local.json to tsconfig.json.

    After making changes to the native Android or iOS code within the package, you must re-run the local prebuild and run commands.

    pnpm use:local
    pnpm prebuild:local
    pnpm android:local
    # or
    pnpm ios:local
  3. Enable local development mode for Keyboard Composer

    main

    When developing the react-native-keyboard-composer package itself or making native changes that you want to test immediately in the example app, you can use the USE_LOCAL_KEYBOARD_COMPOSER environment variable.

    Setting USE_LOCAL_KEYBOARD_COMPOSER=1 configures Expo autolinking to look for the module in the parent directory (../). This allows native Android and iOS changes made within the package source to be picked up by the example app after performing a clean prebuild.

    USE_LOCAL_KEYBOARD_COMPOSER=1 npx expo prebuild
  4. Configure Metro for local library development

    main

    The example project supports two modes of operation via the USE_LOCAL_KEYBOARD_COMPOSER environment variable. This allows developers to switch between using the published package from node_modules or developing against the local package source in a workspace.

    • Default mode: If USE_LOCAL_KEYBOARD_COMPOSER is not set to "1", Metro behaves like a standard consumer, resolving the package from node_modules.
    • Local mode: If USE_LOCAL_KEYBOARD_COMPOSER is set to "1", Metro uses a local configuration (metro.config.local.js) to resolve the package from the parent workspace, enabling real-time library development.
    # To run the example using the local package source:
    USE_LOCAL_KEYBOARD_COMPOSER=1 npx expo start
  5. Configure Expo Module Autolinking

    main

    The expo-module.config.json file defines the native module entry points for the react-native-keyboard-composer package, enabling autolinking for both iOS (Apple) and Android platforms within an Expo environment.

    • Apple (iOS): Uses the KeyboardComposerModule class.
    • Android: Uses the expo.modules.launchhq.reactnativekeyboardcomposer.KeyboardComposerModule class.
    {
      "platforms": ["apple", "android"],
      "apple": {
        "modules": ["KeyboardComposerModule"]
      },
      "android": {
        "modules": [
          "expo.modules.launchhq.reactnativekeyboardcomposer.KeyboardComposerModule"
        ]
      }
    }
  6. Configure KeyboardComposerProps

    main

    The KeyboardComposerProps type defines the configuration for the high-level wrapper component. It provides a simplified API compared to the raw native view props, returning direct values (like string or number) instead of nested nativeEvent objects.

    Key Props

    • Text Control: text (controlled value), placeholder (empty state text), and onChangeText (callback with the new string).
    • Sizing: minHeight and maxHeight control the vertical expansion of the composer.
    • Button Behavior: showSendButton toggles the trailing button; sendButtonEnabled controls its interactivity; isStreaming determines if the button shows a 'stop' action instead of 'send'.
    • Interaction: editable (input state), autoFocus (initial focus), and blurTrigger (a numeric value that, when changed, triggers a blur event).
    • Callbacks: onSend(text: string), onStop(), onHeightChange(height: number), onKeyboardHeightChange(height: number), onComposerFocus(), and onComposerBlur().
    <KeyboardComposer
      placeholder="Type a message..."
      text={message}
      onChangeText={(newText) => setMessage(newText)}
      onSend={(text) => sendMessage(text)}
      minHeight={40}
      maxHeight={150}
      showSendButton={true}
      isStreaming={false}
    />
  7. Use KeyboardAwareWrapper to handle keyboard adjustments

    main

    The KeyboardAwareWrapper is a native component used to wrap content (typically a ScrollView) to ensure it responds correctly to keyboard visibility.

    Behavior (matching iOS):

    • When scrolled to bottom + keyboard opens: The component automatically scrolls to keep content at the bottom.
    • When NOT at bottom + keyboard opens: The keyboard opens over the content without triggering an automatic scroll.

    Use extraBottomInset to account for the height of a composer/input bar and any additional gaps, as the keyboard height itself is handled natively.

    <KeyboardAwareWrapper extraBottomInset={composerHeight + gap}>
      <ScrollView>...</ScrollView>
    </KeyboardAwareWrapper>
  8. Reference: KeyboardComposerViewProps (Native View Props)

    main

    If you are interacting with the underlying native view directly, use KeyboardComposerViewProps. Unlike the wrapper component, callbacks in this interface return event objects containing nativeEvent payloads.

    Event Payloads

    • TextEventPayload: { text: string }
    • HeightEventPayload: { height: number }

    Callback Signatures

    • onChangeText: (event: { nativeEvent: TextEventPayload }) => void
    • onSend: (event: { nativeEvent: TextEventPayload }) => void
    • onHeightChange: (event: { nativeEvent: HeightEventPayload }) => void
    • onKeyboardHeightChange: (event: { nativeEvent: HeightEventPayload }) => void
  9. Access KeyboardComposer configuration constants

    main

    The constants object provides access to the native configuration values for the keyboard composer. These values define the default sizing and spacing behavior used by the native implementation.

    Available constants:

    • defaultMinHeight: The default minimum height for the composer.
    • defaultMaxHeight: The default maximum height for the composer.
    • contentGap: The default spacing/gap between content elements.
    import { constants } from '@launchhq/react-native-keyboard-composer';
    
    console.log(constants.defaultMinHeight);
    console.log(constants.defaultMaxHeight);
    console.log(constants.contentGap);
  10. Configure KeyboardAwareWrapper props

    main

    The KeyboardAwareWrapper accepts the following props to control its behavior and layout:

    PropTypeDescription
    childrenReactNodeThe content to be wrapped (e.g., a ScrollView).
    styleStyleProp<ViewStyle>Standard React Native view styles.
    pinToTopEnabledbooleanEnables ChatGPT-style pin-to-top behavior (a runway below the pinned user message). If omitted, native defaults apply.
    extraBottomInsetnumberExtra bottom inset (composer height + gap). Keyboard height is handled automatically by native code.
    extraTopInsetnumberExtra top inset for overlay/transparent headers. Android requires this; iOS can use contentInset. Provide height in dp/points.
    scrollToTopTriggernumberA value that, when changed, triggers a scroll to the top. Use Date.now() or a counter to trigger this.
  11. Reference KeyboardComposer types

    main

    The library exports several TypeScript types for strict typing of props, refs, and event payloads:

    • KeyboardComposerProps: Props for the main component.
    • KeyboardComposerRef: The ref type for accessing the KeyboardComposer instance.
    • KeyboardComposerViewProps: Props for the underlying view.
    • KeyboardComposerConstants: The shape of the constants object.
    • TextEventPayload: The data structure emitted during text events.
    • HeightEventPayload: The data structure emitted during height change events.