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

Navigation block: Try using a templated page list when there are no menus or inner blocks #44596

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
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
20 changes: 3 additions & 17 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ import {
} from '@wordpress/components';
import { __, sprintf } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import { createBlock } from '@wordpress/blocks';
import { close, Icon } from '@wordpress/icons';

/**
Expand Down Expand Up @@ -282,19 +281,6 @@ function Navigation( {
hasResolvedNavigationMenus &&
! hasUncontrolledInnerBlocks;

useEffect( () => {
if ( isPlaceholder && ! ref ) {
/**
* this fallback only displays (both in editor and on front)
* the list of pages block if no menu is available as a fallback.
* We don't want the fallback to request a save,
* nor to be undoable, hence we mark it non persistent.
*/
__unstableMarkNextChangeAsNotPersistent();
replaceInnerBlocks( clientId, [ createBlock( 'core/page-list' ) ] );
}
}, [ clientId, isPlaceholder, ref ] );

const isEntityAvailable =
! isNavigationMenuMissing && isNavigationMenuResolved;

Expand Down Expand Up @@ -621,14 +607,14 @@ function Navigation( {
</InspectorControls>
);

// If the block has inner blocks, but no menu id, then these blocks are either:
// If no navigation menu entity is available, then these blocks are either:
// - showing the default Page List inner block when there are no menus or inner blocks of any kind.
// - inserted via a pattern.
// - inserted directly via Code View (or otherwise).
// - from an older version of navigation block added before the block used a wp_navigation entity.
// Consider this state as 'unsaved' and offer an uncontrolled version of inner blocks,
// that automatically saves the menu as an entity when changes are made to the inner blocks.
const hasUnsavedBlocks = hasUncontrolledInnerBlocks && ! isEntityAvailable;
if ( hasUnsavedBlocks ) {
if ( ! isEntityAvailable ) {
return (
<TagName { ...blockProps }>
<InspectorControls>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ const ALLOWED_BLOCKS = [
'core/navigation-submenu',
];

const TEMPLATE = [ [ 'core/page-list' ] ];

export default function UnsavedInnerBlocks( {
blocks,
clientId,
Expand Down Expand Up @@ -82,6 +84,7 @@ export default function UnsavedInnerBlocks( {
className: 'wp-block-navigation__container',
},
{
template: TEMPLATE,
renderAppender: hasSelection ? undefined : false,
allowedBlocks: ALLOWED_BLOCKS,
__experimentalDefaultBlock: DEFAULT_BLOCK,
Expand Down