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

Edit Post: Fix pattern modal reopening when making the title empty again #55873

Merged
merged 2 commits into from
Nov 7, 2023
Merged
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
25 changes: 10 additions & 15 deletions packages/edit-post/src/components/start-page-options/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
*/
import { Modal } from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { useState, useEffect, useMemo } from '@wordpress/element';
import { useState, useMemo } from '@wordpress/element';
import {
store as blockEditorStore,
__experimentalBlockPatternsList as BlockPatternsList,
Expand Down Expand Up @@ -62,19 +62,11 @@ function PatternSelection( { blockPatterns, onChoosePattern } ) {
);
}

function StartPageOptionsModal() {
const [ modalState, setModalState ] = useState( 'initial' );
function StartPageOptionsModal( { onClose } ) {
const startPatterns = useStartPatterns();
const hasStartPattern = startPatterns.length > 0;
const shouldOpenModal = hasStartPattern && modalState === 'initial';

useEffect( () => {
if ( shouldOpenModal ) {
setModalState( 'open' );
}
}, [ shouldOpenModal ] );

if ( modalState !== 'open' ) {
if ( ! hasStartPattern ) {
return null;
}

Expand All @@ -83,19 +75,20 @@ function StartPageOptionsModal() {
className="edit-post-start-page-options__modal"
title={ __( 'Choose a pattern' ) }
isFullScreen
onRequestClose={ () => setModalState( 'closed' ) }
onRequestClose={ onClose }
>
<div className="edit-post-start-page-options__modal-content">
<PatternSelection
blockPatterns={ startPatterns }
onChoosePattern={ () => setModalState( 'closed' ) }
onChoosePattern={ onClose }
/>
</div>
</Modal>
);
}

export default function StartPageOptions() {
const [ modalState, setModalState ] = useState( 'initial' );
Copy link
Member

Choose a reason for hiding this comment

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

It's nice that you've been able to reduce the three-state (initial, open, closed) logic into just two states, "open", "closed". The code could be further simplified to use a single boolean value:

const [ isClosed, setClosed ] = useState( false );

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense, updated with 8127399 👍

const shouldEnableModal = useSelect( ( select ) => {
const { isCleanNewPost } = select( editorStore );
const { isEditingTemplate, isFeatureActive } = select( editPostStore );
Expand All @@ -107,9 +100,11 @@ export default function StartPageOptions() {
);
}, [] );

if ( ! shouldEnableModal ) {
if ( ! shouldEnableModal || modalState === 'closed' ) {
return null;
}

return <StartPageOptionsModal />;
return (
<StartPageOptionsModal onClose={ () => setModalState( 'closed' ) } />
);
}
Loading