Skip to content

Commit

Permalink
Edit Post: Fix pattern modal reopening when making the title empty ag…
Browse files Browse the repository at this point in the history
…ain (#55873)

* Fix pattern modal reopening when making the title empty again

* Use boolean value
  • Loading branch information
t-hamano authored Nov 7, 2023
1 parent 38726ab commit 137ad68
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 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 [ isClosed, setIsClosed ] = useState( false );
const shouldEnableModal = useSelect( ( select ) => {
const { isCleanNewPost } = select( editorStore );
const { isEditingTemplate, isFeatureActive } = select( editPostStore );
Expand All @@ -107,9 +100,9 @@ export default function StartPageOptions() {
);
}, [] );

if ( ! shouldEnableModal ) {
if ( ! shouldEnableModal || isClosed ) {
return null;
}

return <StartPageOptionsModal />;
return <StartPageOptionsModal onClose={ () => setIsClosed( true ) } />;
}

0 comments on commit 137ad68

Please sign in to comment.