From d368ddac41efe20abf2fc1a4c48e496b8a5acc16 Mon Sep 17 00:00:00 2001 From: Adam Miskiewicz Date: Fri, 20 Apr 2018 07:03:25 -0700 Subject: [PATCH] Make StackNavigator keyboard aware (#3951) * Make StackNavigator keyboard aware One thing that has always annoyed me in React Navigation is the handling of the keyboard. When a keyboard is visible on screen and a navigation action occurs (either by tapping a button or using a gesture), the keyboard tends to stay on screen until the transition completes. This feels janky and broken. On native iOS, for instance, the keyboard hides immediately when the navigation starts, and if the transition is cancelled (say, when the user releases the gesture), the keyboard reappears. This PR introduces a "KeyboardAwareNavigator" higher order component that is enabled on the StackNavigator, unless a `disableKeyboardHandling` prop is passed into the StackNavigator's configuration. * Set status bar in keyboard handling example * Call gesture props in keyboard aware navigator if available * Fix formatting --- .../examples/NavigationPlayground/js/App.js | 7 +++ .../js/KeyboardHandlingExample.js | 62 +++++++++++++++++++ .../createKeyboardAwareNavigator.js | 49 +++++++++++++++ .../src/navigators/createNavigator.js | 1 + .../src/navigators/createStackNavigator.js | 7 ++- .../react-navigation/src/react-navigation.js | 3 +- .../src/views/StackView/StackView.js | 3 + .../src/views/StackView/StackViewLayout.js | 6 ++ 8 files changed, 136 insertions(+), 2 deletions(-) create mode 100644 packages/react-navigation/examples/NavigationPlayground/js/KeyboardHandlingExample.js create mode 100644 packages/react-navigation/src/navigators/createKeyboardAwareNavigator.js diff --git a/packages/react-navigation/examples/NavigationPlayground/js/App.js b/packages/react-navigation/examples/NavigationPlayground/js/App.js index a5abafca..c091ca6c 100644 --- a/packages/react-navigation/examples/NavigationPlayground/js/App.js +++ b/packages/react-navigation/examples/NavigationPlayground/js/App.js @@ -34,6 +34,7 @@ import StackWithTranslucentHeader from './StackWithTranslucentHeader'; import SimpleTabs from './SimpleTabs'; import SwitchWithStacks from './SwitchWithStacks'; import TabsWithNavigationFocus from './TabsWithNavigationFocus'; +import KeyboardHandlingExample from './KeyboardHandlingExample'; const ExampleInfo = { SimpleStack: { @@ -114,6 +115,11 @@ const ExampleInfo = { name: 'withNavigationFocus', description: 'Receive the focus prop to know when a screen is focused', }, + KeyboardHandlingExample: { + name: 'Keyboard Handling Example', + description: + 'Demo automatic handling of keyboard showing/hiding inside StackNavigator', + }, }; const ExampleRoutes = { @@ -143,6 +149,7 @@ const ExampleRoutes = { path: 'settings', }, TabsWithNavigationFocus, + KeyboardHandlingExample, }; type State = { diff --git a/packages/react-navigation/examples/NavigationPlayground/js/KeyboardHandlingExample.js b/packages/react-navigation/examples/NavigationPlayground/js/KeyboardHandlingExample.js new file mode 100644 index 00000000..7a0674a5 --- /dev/null +++ b/packages/react-navigation/examples/NavigationPlayground/js/KeyboardHandlingExample.js @@ -0,0 +1,62 @@ +import React from 'react'; +import { StatusBar, View, TextInput, InteractionManager } from 'react-native'; +import { createStackNavigator, withNavigationFocus } from 'react-navigation'; +import { Button } from './commonComponents/ButtonWithMargin'; + +class ScreenOne extends React.Component { + static navigationOptions = { + title: 'Home', + }; + + render() { + const { navigation } = this.props; + return ( + +