Skip to content

Commit

Permalink
Navigation Link: Limit Nesting Depth to 5 (#30199)
Browse files Browse the repository at this point in the history
Removes the ability to nest submenus beyond 5 to avoid scrolling off the screen.

Closes #21691
  • Loading branch information
georgeh authored Apr 7, 2021
1 parent 6604dd3 commit 71ec908
Showing 1 changed file with 16 additions and 6 deletions.
22 changes: 16 additions & 6 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,12 @@ import { store as coreStore } from '@wordpress/core-data';
* Internal dependencies
*/
import { ItemSubmenuIcon } from './icons';
import { name } from './block.json';

const ALLOWED_BLOCKS = [ 'core/navigation-link', 'core/spacer' ];

const MAX_NESTING = 5;

/**
* A React hook to determine if it's dragging within the target element.
*
Expand Down Expand Up @@ -164,6 +167,7 @@ export default function NavigationLinkEdit( {
const ref = useRef();

const {
isAtMaxNesting,
isParentOfSelectedBlock,
isImmediateParentOfSelectedBlock,
hasDescendants,
Expand All @@ -177,6 +181,7 @@ export default function NavigationLinkEdit( {
getClientIdsOfDescendants,
hasSelectedInnerBlock,
getSelectedBlockClientId,
getBlockParentsByBlockName,
} = select( blockEditorStore );

const selectedBlockId = getSelectedBlockClientId();
Expand All @@ -185,6 +190,9 @@ export default function NavigationLinkEdit( {
.length;

return {
isAtMaxNesting:
getBlockParentsByBlockName( clientId, name ).length >=
MAX_NESTING,
isParentOfSelectedBlock: hasSelectedInnerBlock(
clientId,
true
Expand Down Expand Up @@ -382,12 +390,14 @@ export default function NavigationLinkEdit( {
shortcut={ displayShortcut.primary( 'k' ) }
onClick={ () => setIsLinkOpen( true ) }
/>
<ToolbarButton
name="submenu"
icon={ addSubmenu }
title={ __( 'Add submenu' ) }
onClick={ insertLinkBlock }
/>
{ ! isAtMaxNesting && (
<ToolbarButton
name="submenu"
icon={ addSubmenu }
title={ __( 'Add submenu' ) }
onClick={ insertLinkBlock }
/>
) }
</ToolbarGroup>
</BlockControls>
<InspectorControls>
Expand Down

0 comments on commit 71ec908

Please sign in to comment.