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

Fix: Show creation popover on empty page links in the navigation sidebar. #48746

Merged
merged 2 commits into from
Mar 6, 2023
Merged
Changes from all 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 @@ -7,7 +7,9 @@ import {
BlockList,
BlockTools,
} from '@wordpress/block-editor';
import { useSelect } from '@wordpress/data';
import { useDispatch, useSelect } from '@wordpress/data';
import { createBlock } from '@wordpress/blocks';
import { useCallback } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -30,9 +32,29 @@ export default function NavigationMenuContent( { rootClientId, onSelect } ) {
},
[ rootClientId ]
);
const { replaceBlock, __unstableMarkNextChangeAsNotPersistent } =
useDispatch( blockEditorStore );

const { OffCanvasEditor, LeafMoreMenu } = unlock( blockEditorPrivateApis );

const offCanvasOnselect = useCallback(
( block ) => {
if (
block.name === 'core/navigation-link' &&
! block.attributes.url
) {
__unstableMarkNextChangeAsNotPersistent();
replaceBlock(
block.clientId,
createBlock( 'core/navigation-link', block.attributes )
);
} else {
onSelect( block );
}
},
[ onSelect, __unstableMarkNextChangeAsNotPersistent, replaceBlock ]
);

// The hidden block is needed because it makes block edit side effects trigger.
// For example a navigation page list load its items has an effect on edit to load its items.
return (
Expand All @@ -41,12 +63,12 @@ export default function NavigationMenuContent( { rootClientId, onSelect } ) {
{ ! isLoading && (
<OffCanvasEditor
blocks={ clientIdsTree }
onSelect={ onSelect }
onSelect={ offCanvasOnselect }
LeafMoreMenu={ LeafMoreMenu }
showAppender={ false }
/>
) }
<div style={ { display: 'none' } }>
<div style={ { visibility: 'hidden' } }>
<BlockTools>
<BlockList />
</BlockTools>
Expand Down