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

Respect uncontrolled inner blocks on Navigation block in editor and front of site #42182

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 8 additions & 3 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,11 +207,16 @@ function Navigation( {
hasResolvedCanUserCreateNavigationMenu,
} = useNavigationMenu( ref );

// Attempt to retrieve and prioritize any existing navigation menu unless
// a specific ref is allocated or the user is explicitly creating a new menu. The aim is
// for the block to "just work" from a user perspective using existing data.
// Attempt to retrieve and prioritize any existing navigation menu unless:
// - the are uncontrolled inner blocks already present in the block.
// - the user is creating a new menu.
// - there is more than 1 Navigation Post (wp_navigation).
// This attempts to pick the first menu if there is a single Navigation Post. If more
// than 1 exists then no attempt to automatically pick a menu is made.
// The aim is for the block to "just work" from a user perspective using existing data.
useEffect( () => {
if (
hasUncontrolledInnerBlocks ||
isCreatingNavigationMenu ||
ref ||
! navigationMenus?.length ||
Expand Down
45 changes: 45 additions & 0 deletions packages/e2e-tests/specs/editor/blocks/navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1393,6 +1393,51 @@ Expected mock function not to be called but it was called with: ["POST", "http:/
expect( linkText ).toBe( 'WordPress' );
} );

it( 'does not automatically use the first Navigation Menu if uncontrolled inner blocks are present', async () => {
const pageTitle = 'A Test Page';

await createNavigationMenu( {
title: 'Example Navigation',
content:
'<!-- wp:navigation-link {"label":"First Nav Menu Item","type":"custom","url":"http://www.wordpress.org/","kind":"custom","isTopLevelLink":true} /-->',
} );

await rest( {
method: 'POST',
path: `/wp/v2/pages/`,
data: {
status: 'publish',
title: pageTitle,
content: 'Hello world',
},
} );

await createNewPost();

await clickOnMoreMenuItem( 'Code editor' );

const codeEditorInput = await page.waitForSelector(
'.editor-post-text-editor'
);
await codeEditorInput.click();

const markup =
'<!-- wp:navigation --><!-- wp:page-list /--><!-- /wp:navigation -->';
await page.keyboard.type( markup );
await clickButton( 'Exit code editor' );

await waitForBlock( 'Navigation' );

const hasUncontrolledInnerBlocks = await page.evaluate( () => {
const blocks = wp.data
.select( 'core/block-editor' )
.getBlocks();
return !! blocks[ 0 ]?.innerBlocks?.length;
} );

expect( hasUncontrolledInnerBlocks ).toBe( true );
} );

it( 'does not automatically use first Navigation Menu if more than one exists', async () => {
await createNavigationMenu( {
title: 'Example Navigation',
Expand Down