react-native-keyboard-accessory

repository·master·Indexed 19 days ago

https://github.com/ardaogulcan/react-native-keyboard-accessory

A React Native library providing components to create sticky views and navigation bars that remain attached to the keyboard when visible. It includes KeyboardAccessoryView for custom sticky content, KeyboardAccessoryNavigation for toolbars with Done, Next, and Previous buttons, and KeyboardAwareTabBarComponent for integration with tab bar components.

Tokens
3.5K
Snippets
8
Records
13
Agent score
62%

What's inside react-native-keyboard-accessory

  1. Configure Android for Ejected Apps

    master

    If you are using an ejected React Native app on Android, you must perform two steps to ensure the accessory works correctly:

    1. In android/app/src/main/AndroidManifest.xml, set android:windowSoftInputMode to adjustResize.
    2. Pass the androidAdjustResize prop as true to your component.
    <KeyboardAccessoryView androidAdjustResize />
    // or
    <KeyboardAccessoryNavigation androidAdjustResize />
    <KeyboardAccessoryView androidAdjustResize />
  2. Use KeyboardAccessoryView

    master

    The KeyboardAccessoryView is a component used to create sticky views that stay attached to the keyboard.

    Important: The component must be positioned inside the Root Element of your application, typically the top-most view styled with { flex: 1 }.

    Basic Usage

    <KeyboardAccessoryView>
      <View>
        <TextInput />
      </View>
    </KeyboardAccessoryView>

    Using the Render Prop

    You can pass a function as children to access the isKeyboardVisible boolean. This is useful for conditional rendering based on whether the keyboard is currently active.

    <KeyboardAccessoryView>
      {({ isKeyboardVisible }) => {
        return (
          <>
            <Text>Always visible</Text>
            {!isKeyboardVisible ? (
              <Text>Hidden when keyboard is visible</Text>
            ) : null}
          </>
        );
      }}
    </KeyboardAccessoryView>
  3. Use KeyboardAccessoryNavigation

    master

    The KeyboardAccessoryNavigation component provides a navigation bar (with Done, Next, and Previous buttons) that stays sticky to the keyboard.

    Important: Like KeyboardAccessoryView, this should be positioned inside your Root Element (the top-most view with { flex: 1 }).

    <KeyboardAccessoryNavigation />
  4. KeyboardAccessoryNavigation API Reference

    master

    The KeyboardAccessoryNavigation component inherits all props from KeyboardAccessoryView. Additional props include:

    PropTypeDefaultDescription
    doneButtonTitlestring'Done'Title for the right-side button
    tintColorstring'#007AFF'Color for arrows and buttons
    doneButtonnodenullCustom non-Touchable node for Done Button
    nextButtonnodenullCustom non-Touchable node for Next Button
    previousButtonnodenullCustom non-Touchable node for Previous Button
    doneDisabledbooleanfalseDisables Done Button
    nextDisabledbooleanfalseDisables Next Button
    previousDisabledbooleanfalseDisables Previous Button
    doneHiddenbooleanfalseHides Done Button
    nextHiddenbooleanfalseHides Next Button
    previousHiddenbooleanfalseHides Previous Button
    accessoryStyleobjectnullStyle for the Navigation Accessory View
    doneButtonStyleobjectnullStyle for the Done Button View
    doneButtonTitleStyleobjectnullStyle for the Done Button Text
    doneButtonHitslopInsets{ left: 0, top: 0, right: 0, bottom: 0 }Touch area for the doneButton
    previousButtonStyleobject0Style for the Previous Button View
    nextButtonStyleobject0Style for the Next Button View
    nextButtonDirectionenum:string'down'Direction for Next Button: ['down', 'up', 'right', 'left']
    nextButtonHitslopInsets{ left: 0, top: 0, right: 0, bottom: 0 }Touch area for the nextButton
    previousButtonDirectionenum:string'up'Direction for Previous Button: ['up', 'down', 'right', 'left']
    previousButtonHitslopInsets{ left: 0, top: 0, right: 0, bottom: 0 }Touch area for the previousButton
    onDonefunctionnullTriggered on Done Button press
    onNextfunctionnullTriggered on Next Button press
    onPreviousfunctionnullTriggered on Previous Button press
  5. KeyboardAccessoryView API Reference

    master

    Props for KeyboardAccessoryView:

    PropTypeDefaultDescription
    styleobjectnullStyle applied to the Accessory View
    animateOnenum:string'ios'Enables animation on: ['ios', 'android', 'all', 'none']
    animationConfigfunction or objectnullCustom animation config. If a function, receives duration and easing from Keyboard event
    alwaysVisiblebooleanfalseIf true, view is always visible at the bottom (good for sticky TextInputs)
    bumperHeightnumber15Height to prevent visual glitches during animation
    visibleOpacitynumber1Opacity when visible (used for hiding to prevent render delays)
    heightPropertyenum:string'height'Manages height. Use 'minHeight' for multiline autogrowing TextInputs
    hiddenOpacitynumber0Opacity when hidden
    hideBorderbooleanfalseHides the top border of the Accessory
    inSafeAreaViewbooleanfalseAdapts SafeAreaView on iPhone X
    androidAdjustResizebooleanfalseRequired for ejected Android apps
    avoidKeyboardbooleanfalseMaintains KeyboardAvoidingView behavior
  6. Use KeyboardAccessoryNavigation for sticky navigation views

    master

    The KeyboardAccessoryNavigation component provides a specialized toolbar that sits above the keyboard. It is designed for forms or multi-step inputs where users need to navigate between fields (Next/Previous) or dismiss the keyboard (Done).

    It wraps KeyboardAccessoryView and provides a layout containing:

    1. Navigation Buttons: Left-aligned buttons for previous and next actions.
    2. Info Area: A central area for an infoMessage or a custom infoContainer.
    3. Done Button: A right-aligned button to dismiss the keyboard.

    All buttons can be customized with custom elements, styles, and hit slops.

    import KeyboardAccessoryNavigation from 'react-native-keyboard-accessory';
    
    // Example usage concept
    <KeyboardAccessoryNavigation
      onNext={() => console.log('Next field')}
      onPrevious={() => console.log('Prev field')}
      onDone={() => console.log('Done pressed')}
      nextButtonDirection="down"
      previousButtonDirection="up"
      infoMessage="Please enter your email"
      tintColor="#007AFF"
    />
  7. Use the KeyboardAccessoryView component

    master

    KeyboardAccessoryView is a component designed to render a 'sticky' view that sits on top of the software keyboard when it is visible. It handles keyboard event listeners, layout animations, and safe area adjustments automatically.

    Key Behaviors

    • Visibility: By default, the accessory is only visible when the keyboard is active. You can use alwaysVisible={true} to keep it visible regardless of keyboard state.
    • Positioning: The component uses absolute positioning to sit above the keyboard. You can adjust the gap between the keyboard and the accessory using bumperHeight.
    • Animations: It uses LayoutAnimation to smoothly transition the accessory's position and opacity. You can control which platform triggers animations via animateOn and provide custom animation configurations via animationConfig.
    • Render Props: The children prop can be a function, allowing you to access the isKeyboardVisible state within your accessory UI.
    import KeyboardAccessoryView from 'react-native-keyboard-accessory';
    
    <KeyboardAccessoryView
      alwaysVisible={false}
      bumperHeight={10}
      style={{ backgroundColor: 'white' }}
    >
      {( { isKeyboardVisible } ) => (
        <View>
          <Text>{isKeyboardVisible ? 'Keyboard is up!' : 'Keyboard is down'}</Text>
        </View>
      )}
    </KeyboardAccessoryView>
  8. Import components from react-native-keyboard-accessory

    master

    The library exports three main components to handle keyboard interactions and accessory views in React Native:

    • KeyboardAccessoryView: A component used to create a view that sits above the keyboard.
    • KeyboardAccessoryNavigation: A component designed for navigation elements that need to react to the keyboard.
    • KeyboardAwareTabBarComponent: A tab bar component that is aware of the keyboard state.

    You can import them using named exports from the package root.

    import {
      KeyboardAccessoryView,
      KeyboardAccessoryNavigation,
      KeyboardAwareTabBarComponent
    } from 'react-native-keyboard-accessory';
  9. Configure KeyboardAccessoryNavigation props

    master

    The KeyboardAccessoryNavigation component accepts the following props to control its behavior and appearance:

    • onNext: Function called when the next button is pressed.
    • onPrevious: Function called when the previous button is pressed.
    • onDone: Function called when the done button is pressed (also triggers Keyboard.dismiss()).
    • nextButton: Custom element to replace the default next arrow.
    • previousButton: Custom element to replace the default previous arrow.
    • nextDisabled: Boolean to disable the next button (default: false).
    • previousDisabled: Boolean to disable the previous button (default: false).
    • nextHidden: Boolean to hide the next button (default: false).
    • previousHidden: Boolean to hide the previous button (default: false).
    • nextButtonDirection: Direction for the next arrow: 'up', 'down', 'left', or 'right' (default: 'down').
    • previousButtonDirection: Direction for the previous arrow: 'up', 'down', 'left', or 'right' (default: 'up').

    Done Button Props

    • doneButton: Custom element to replace the default done button.
    • doneButtonTitle: String text for the done button (default: 'Done').
    • doneButtonStyle: Style for the done button container.
    • doneButtonTitleStyle: Style for the done button text.
    • doneButtonHitslop: Hit slop for the done button.

    Info & Styling Props

    • infoMessage: String text to display in the info area.
    • infoContainer: Custom element to replace the info message/text.
    • infoMessageStyle: Style for the info text.
    • tintColor: Color applied to text and icons (default: '#007AFF').
    • accessoryStyle: Style for the main accessory container.

    Hit Slop & Button Styles

    • nextButtonStyle: Style for the next button.
    • previousButtonStyle: Style for the previous button.
    • nextButtonHitslop: Hit slop for the next button.
    • previousButtonHitslop: Hit slop for the previous button.