Skip to content

Commit

Permalink
Prefer replacing inner blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
getdave committed Jan 5, 2023
1 parent 7e7e78c commit 8df4b1c
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions packages/block-library/src/navigation/leaf-more-menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const POPOVER_PROPS = {
export const LeafMoreMenu = ( props ) => {
const { clientId, block } = props;

const { insertBlock, replaceBlock, removeBlocks } =
const { insertBlock, replaceBlock, removeBlocks, replaceInnerBlocks } =
useDispatch( blockEditorStore );

const label = sprintf(
Expand All @@ -40,11 +40,11 @@ export const LeafMoreMenu = ( props ) => {
<MenuItem
icon={ addSubmenu }
onClick={ () => {
const updateSelectionOnInsert = false;
const newLink = createBlock(
'core/navigation-link'
);
if ( block.name === 'core/navigation-submenu' ) {
const updateSelectionOnInsert = false;
insertBlock(
newLink,
block.innerBlocks.length,
Expand All @@ -57,10 +57,21 @@ export const LeafMoreMenu = ( props ) => {
'core/navigation-submenu',
block.attributes,
block.innerBlocks
? [ ...block.innerBlocks, newLink ]
: [ newLink ]
);

// The following must happen as two independent actions.
// Why? Because the offcanvas editor relies on the getLastInsertedBlocksClientIds
// selector to determine which block is "active". As the UX needs the newLink to be
// the "active" block it must be the last block to be inserted.
// Therefore the Submenu is first created and **then** the newLink is inserted
// thus ensuring it is the last inserted block.
replaceBlock( clientId, newSubmenu );

replaceInnerBlocks(
newSubmenu.clientId,
[ newLink ],
updateSelectionOnInsert
);
}
onClose();
} }
Expand Down

1 comment on commit 8df4b1c

@github-actions
Copy link

Choose a reason for hiding this comment

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

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3847265491
📝 Reported issues:

Please sign in to comment.