Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RNMobile] - Layout Picker - Keyboard visibility improvements #21463

Merged
merged 3 commits into from
Apr 8, 2020
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { useDispatch, useSelect } from '@wordpress/data';
* External dependencies
*/
import { logUserEvent, userEvents } from 'react-native-gutenberg-bridge';
import { Animated } from 'react-native';
import { Animated, Dimensions, Keyboard } from 'react-native';
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Didn't use useWindowDimensions because there's a bug that will be fixed for 0.62


/**
* Internal dependencies
Expand All @@ -18,6 +18,9 @@ import Container from './container';
import getDefaultTemplates from './default-templates';
import Preview from './preview';

// Used to hide the picker if there's no enough space in the window
const PICKER_HEIGHT_OFFSET = 150;

const __experimentalPageTemplatePicker = ( {
templates = getDefaultTemplates(),
visible,
Expand All @@ -36,9 +39,41 @@ const __experimentalPageTemplatePicker = ( {
const contentOpacity = useRef( new Animated.Value( 0 ) );

useEffect( () => {
if ( shouldShowPicker() && visible && ! pickerVisible ) {
setPickerVisible( true );
}
onPickerAnimation();

Keyboard.addListener( 'keyboardDidShow', onKeyboardDidShow );
Keyboard.addListener( 'keyboardDidHide', onKeyboardDidHide );

return () => {
Keyboard.removeListener( 'keyboardDidShow', onKeyboardDidShow );
Keyboard.removeListener( 'keyboardDidHide', onKeyboardDidHide );
};
}, [ visible ] );

const onKeyboardDidShow = () => {
if ( visible ) {
setPickerVisible( shouldShowPicker() );
onPickerAnimation();
}
};

const onKeyboardDidHide = () => {
if ( visible ) {
setPickerVisible( true );
onPickerAnimation();
}
};

const shouldShowPicker = () => {
// On smaller devices on landscape we hide the picker
// so it doesn't overlap with the editor's content
const windowHeight = Dimensions.get( 'window' ).height;
return PICKER_HEIGHT_OFFSET < windowHeight / 3;
};

const onApply = () => {
editPost( {
title: title || templatePreview.name,
Expand All @@ -51,10 +86,6 @@ const __experimentalPageTemplatePicker = ( {
};

const onPickerAnimation = () => {
if ( visible && ! pickerVisible ) {
setPickerVisible( true );
}

Animated.timing( contentOpacity.current, {
toValue: visible ? 1 : 0,
duration: 300,
Expand Down