Skip to content

Commit

Permalink
[RNMobile] Fix toolbar above the keyboard (#24573)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukewalczak authored Sep 8, 2020
1 parent 481ca1b commit f899b3e
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 4 deletions.
1 change: 0 additions & 1 deletion packages/block-library/src/button/edit.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,6 @@ class ButtonEdit extends Component {
__unstableMobileNoFocusOnMount={ ! isSelected }
selectionColor={ textColor }
onBlur={ () => {
this.onToggleButtonFocus( false );
this.onSetMaxWidth();
} }
onReplace={ onReplace }
Expand Down
115 changes: 112 additions & 3 deletions packages/components/src/mobile/keyboard-avoiding-view/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,128 @@
*/
import {
KeyboardAvoidingView as IOSKeyboardAvoidingView,
Animated,
Keyboard,
Dimensions,
View,
} from 'react-native';
import SafeArea from 'react-native-safe-area';

/**
* WordPress dependencies
*/
import { useEffect, useRef, useState } from '@wordpress/element';
import { useResizeObserver } from '@wordpress/compose';

/**
* Internal dependencies
*/
import styles from './styles.scss';

const AnimatedKeyboardAvoidingView = Animated.createAnimatedComponent(
IOSKeyboardAvoidingView
);

const MIN_HEIGHT = 44;

export const KeyboardAvoidingView = ( {
parentHeight,
style,
withAnimatedHeight = false,
...otherProps
} ) => {
const [ resizeObserver, sizes ] = useResizeObserver();
const [ isKeyboardOpen, setIsKeyboardOpen ] = useState( false );
const [ safeAreaBottomInset, setSafeAreaBottomInset ] = useState( 0 );
const { height = 0 } = sizes || {};

const animatedHeight = useRef( new Animated.Value( MIN_HEIGHT ) ).current;

export const KeyboardAvoidingView = ( { parentHeight, ...otherProps } ) => {
const { height: fullHeight } = Dimensions.get( 'window' );
const keyboardVerticalOffset = fullHeight - parentHeight;

useEffect( () => {
SafeArea.getSafeAreaInsetsForRootView().then(
( { safeAreaInsets } ) => {
setSafeAreaBottomInset( safeAreaInsets.bottom );
}
);
SafeArea.addEventListener(
'safeAreaInsetsForRootViewDidChange',
onSafeAreaInsetsUpdate
);
Keyboard.addListener( 'keyboardWillShow', onKeyboardWillShow );
Keyboard.addListener( 'keyboardWillHide', onKeyboardWillHide );

return () => {
SafeArea.removeEventListener(
'safeAreaInsetsForRootViewDidChange',
onSafeAreaInsetsUpdate
);
Keyboard.removeListener( 'keyboardWillShow', onKeyboardWillShow );
Keyboard.removeListener( 'keyboardWillHide', onKeyboardWillHide );
};
}, [] );

function onSafeAreaInsetsUpdate( { safeAreaInsets } ) {
setSafeAreaBottomInset( safeAreaInsets.bottom );
}

function onKeyboardWillShow( { endCoordinates } ) {
setIsKeyboardOpen( true );
animatedHeight.setValue( endCoordinates.height + MIN_HEIGHT );
}

function onKeyboardWillHide( { duration, startCoordinates } ) {
const animatedListenerId = animatedHeight.addListener(
( { value } ) => {
if ( value < startCoordinates.height / 3 ) {
setIsKeyboardOpen( false );
}
}
);

Animated.timing( animatedHeight, {
toValue: MIN_HEIGHT,
duration,
useNativeDriver: false,
} ).start( () => {
animatedHeight.removeListener( animatedListenerId );
} );
}

return (
<IOSKeyboardAvoidingView
<AnimatedKeyboardAvoidingView
{ ...otherProps }
behavior="padding"
keyboardVerticalOffset={ keyboardVerticalOffset }
/>
style={
withAnimatedHeight
? [
style,
{
height: animatedHeight,
marginBottom: isKeyboardOpen
? -safeAreaBottomInset
: 0,
},
]
: style
}
>
<View
style={ [
{
top: -height + MIN_HEIGHT,
},
styles.animatedChildStyle,
! withAnimatedHeight && styles.defaultChildStyle,
] }
>
{ resizeObserver }
{ otherProps.children }
</View>
</AnimatedKeyboardAvoidingView>
);
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
.animatedChildStyle {
position: absolute;
width: 100%;
}

.defaultChildStyle {
height: 100%;
top: 0;
}
1 change: 1 addition & 0 deletions packages/edit-post/src/components/layout/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ class Layout extends Component {
<KeyboardAvoidingView
parentHeight={ this.state.rootViewHeight }
style={ toolbarKeyboardAvoidingViewStyle }
withAnimatedHeight
>
{ isTemplatePickerAvailable && (
<__experimentalPageTemplatePicker
Expand Down

0 comments on commit f899b3e

Please sign in to comment.