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

Alternative Nav Offcanvas show link UI for submenu creation #46857

Merged
merged 1 commit into from
Jan 5, 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
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