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] - Starter Page Templates - Picker and Preview design updates #20959

Merged
merged 4 commits into from
Mar 27, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 1 addition & 2 deletions packages/block-editor/src/components/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export { default as VideoPlayer, VIDEO_ASPECT_RATIO } from './video-player';
// Content Related Components
export {
__experimentalPageTemplatePicker,
__experimentalUsePageTemplatePickerVisible,
__experimentalWithPageTemplatePickerVisible,
__experimentalWithPageTemplatePicker,
} from './page-template-picker';
export { default as BlockList } from './block-list';
export { default as BlockMover } from './block-mover';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
* Internal dependencies
*/
import __experimentalPageTemplatePicker from './picker';
import __experimentalWithPageTemplatePickerVisible from './with-page-template-picker-visible';
import __experimentalUsePageTemplatePickerVisible from './use-page-template-picker-visible';
import __experimentalWithPageTemplatePicker from './with-page-template-picker';

export { __experimentalPageTemplatePicker };
export { __experimentalWithPageTemplatePickerVisible };
export { __experimentalUsePageTemplatePickerVisible };
export { __experimentalWithPageTemplatePicker };
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { useState, useEffect } from '@wordpress/element';
import { useState, useEffect, useRef } from '@wordpress/element';
import { useDispatch, useSelect } from '@wordpress/data';

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

const PICKER_ANIMATION_DELAY = 500;

const __experimentalPageTemplatePicker = ( {
templates = getDefaultTemplates(),
visible,
Expand All @@ -35,14 +33,10 @@ const __experimentalPageTemplatePicker = ( {

const [ templatePreview, setTemplatePreview ] = useState();
const [ pickerVisible, setPickerVisible ] = useState( visible );
const contentOpacity = new Animated.Value( 1 );
const contentOpacity = useRef( new Animated.Value( 0 ) );

useEffect( () => {
if ( ! visible ) {
onHidePicker();
} else {
setPickerVisible( true );
}
onPickerAnimation();
}, [ visible ] );

const onApply = () => {
Expand All @@ -56,13 +50,19 @@ const __experimentalPageTemplatePicker = ( {
setTemplatePreview( undefined );
};

const onHidePicker = () => {
Animated.timing( contentOpacity, {
toValue: 0,
const onPickerAnimation = () => {
if ( visible && ! pickerVisible ) {
setPickerVisible( true );
}

Animated.timing( contentOpacity.current, {
toValue: visible ? 1 : 0,
duration: 300,
delay: PICKER_ANIMATION_DELAY,
useNativeDriver: true,
} ).start( () => {
setPickerVisible( false );
if ( ! visible ) {
setPickerVisible( false );
}
} );
};

Expand All @@ -71,7 +71,7 @@ const __experimentalPageTemplatePicker = ( {
}

return (
<Animated.View style={ [ { opacity: contentOpacity } ] }>
<Animated.View style={ [ { opacity: contentOpacity.current } ] }>
<Container>
{ templates.map( ( template ) => (
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,17 @@
import { isUnmodifiedDefaultBlock } from '@wordpress/blocks';
import { useSelect } from '@wordpress/data';

const __experimentalUsePageTemplatePickerVisible = () => {
export const __experimentalUsePageTemplatePickerAvailable = () => {
return useSelect( ( select ) => {
const { getCurrentPostType } = select( 'core/editor' );
return getCurrentPostType() === 'page';
}, [] );
};

export const __experimentalUsePageTemplatePickerVisible = () => {
const isTemplatePickerAvailable = __experimentalUsePageTemplatePickerAvailable();

return useSelect( ( select ) => {
const { getBlockOrder, getBlock } = select( 'core/block-editor' );

const blocks = getBlockOrder();
Expand All @@ -16,10 +23,7 @@ const __experimentalUsePageTemplatePickerVisible = () => {
const isOnlyUnmodifiedDefault =
blocks.length === 1 && isUnmodifiedDefaultBlock( firstBlock );
const isEmptyContent = isEmptyBlockList || isOnlyUnmodifiedDefault;
const isPage = getCurrentPostType() === 'page';

return isEmptyContent && isPage;
return isEmptyContent && isTemplatePickerAvailable;
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure if I'm overthinking this, but I imagine the majority of times this runs it will return false. I'd suggest refactoring a bit to return early and avoid any extra calculations:

if ( ! isTemplatePickerAvailable ) {
    return false;
}
//...
if ( isEmptyBlockList ) {
   return false;
}
// ...

It'd be interesting to check if this impacts performance at all, or I'm just worried about nothing 😅

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm always down for performance improvements 🙌, I'll definitely check it out, thanks for the review koke!

}, [] );
};

export default __experimentalUsePageTemplatePickerVisible;

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* WordPress dependencies
*/
import { createHigherOrderComponent } from '@wordpress/compose';

/**
* Internal dependencies
*/
import {
__experimentalUsePageTemplatePickerVisible,
__experimentalUsePageTemplatePickerAvailable,
} from './use-page-template-picker';

const __experimentalWithPageTemplatePicker = createHigherOrderComponent(
( WrappedComponent ) => ( props ) => {
const isTemplatePickerVisible = __experimentalUsePageTemplatePickerVisible();
const isTemplatePickerAvailable = __experimentalUsePageTemplatePickerAvailable();

return (
<WrappedComponent
{ ...props }
isTemplatePickerVisible={ isTemplatePickerVisible }
isTemplatePickerAvailable={ isTemplatePickerAvailable }
/>
);
},
'__experimentalWithPageTemplatePicker'
);

export default __experimentalWithPageTemplatePicker;
20 changes: 9 additions & 11 deletions packages/edit-post/src/components/layout/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { withSelect } from '@wordpress/data';
import {
BottomSheetSettings,
__experimentalPageTemplatePicker,
__experimentalWithPageTemplatePickerVisible,
__experimentalWithPageTemplatePicker,
} from '@wordpress/block-editor';
import { compose, withPreferredColorScheme } from '@wordpress/compose';
import { HTMLTextInput, KeyboardAvoidingView } from '@wordpress/components';
Expand Down Expand Up @@ -95,9 +95,9 @@ class Layout extends Component {
render() {
const {
getStylesFromColorScheme,
isPage,
isTemplatePickerAvailable,
isTemplatePickerVisible,
mode,
showPageTemplatePicker,
} = this.props;

const isHtmlView = mode === 'text';
Expand Down Expand Up @@ -144,9 +144,9 @@ class Layout extends Component {
parentHeight={ this.state.rootViewHeight }
style={ toolbarKeyboardAvoidingViewStyle }
>
{ isPage && (
{ isTemplatePickerAvailable && (
<__experimentalPageTemplatePicker
visible={ showPageTemplatePicker }
visible={ isTemplatePickerVisible }
/>
) }
<Header />
Expand All @@ -160,18 +160,16 @@ class Layout extends Component {

export default compose( [
withSelect( ( select ) => {
const {
__unstableIsEditorReady: isEditorReady,
getCurrentPostType,
} = select( 'core/editor' );
const { __unstableIsEditorReady: isEditorReady } = select(
'core/editor'
);
const { getEditorMode } = select( 'core/edit-post' );

return {
isPage: getCurrentPostType(),
isReady: isEditorReady(),
mode: getEditorMode(),
};
} ),
withPreferredColorScheme,
__experimentalWithPageTemplatePickerVisible,
__experimentalWithPageTemplatePicker,
] )( Layout );