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

Enable navigation nesting to be filtered and manually set #38621

Merged
merged 8 commits into from
Apr 4, 2022
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ A collection of blocks that allow visitors to get around your site. ([Source](ht
- **Name:** core/navigation
- **Category:** theme
- **Supports:** align (full, wide), anchor, inserter, spacing (blockGap, units), typography (fontSize, lineHeight), ~~html~~
- **Attributes:** __unstableLocation, backgroundColor, customBackgroundColor, customOverlayBackgroundColor, customOverlayTextColor, customTextColor, hasIcon, openSubmenusOnClick, overlayBackgroundColor, overlayMenu, overlayTextColor, ref, rgbBackgroundColor, rgbTextColor, showSubmenuIcon, textColor
- **Attributes:** __unstableLocation, backgroundColor, customBackgroundColor, customOverlayBackgroundColor, customOverlayTextColor, customTextColor, hasIcon, maxNestingLevel, openSubmenusOnClick, overlayBackgroundColor, overlayMenu, overlayTextColor, ref, rgbBackgroundColor, rgbTextColor, showSubmenuIcon, textColor

## Navigation Area

Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation-link/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"fontSize",
"customFontSize",
"showSubmenuIcon",
"maxNestingLevel",
Copy link
Member

Choose a reason for hiding this comment

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

I'd suggest calling this maxLevel

"style"
],
"supports": {
Expand Down
5 changes: 2 additions & 3 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ import { store as coreStore } from '@wordpress/core-data';
*/
import { name } from './block.json';

const MAX_NESTING = 5;

/**
* A React hook to determine if it's dragging within the target element.
*
Expand Down Expand Up @@ -400,6 +398,7 @@ export default function NavigationLinkEdit( {
} = attributes;

const [ isInvalid, isDraft ] = useIsInvalidLink( kind, type, id );
const { maxNestingLevel } = context;

const link = {
url,
Expand Down Expand Up @@ -452,7 +451,7 @@ export default function NavigationLinkEdit( {
getBlockParentsByBlockName( clientId, [
name,
'core/navigation-submenu',
] ).length >= MAX_NESTING,
] ).length >= maxNestingLevel,
isTopLevelLink:
getBlockName( getBlockRootClientId( clientId ) ) ===
'core/navigation',
Expand Down
1 change: 1 addition & 0 deletions packages/block-library/src/navigation-submenu/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
"fontSize",
"customFontSize",
"showSubmenuIcon",
"maxNestingLevel",
"openSubmenusOnClick",
"style"
],
Expand Down
8 changes: 3 additions & 5 deletions packages/block-library/src/navigation-submenu/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ const DEFAULT_BLOCK = {
name: 'core/navigation-link',
};

const MAX_NESTING = 5;

/**
* A React hook to determine if it's dragging within the target element.
*
Expand Down Expand Up @@ -293,7 +291,7 @@ export default function NavigationSubmenuEdit( {
url,
opensInNewTab,
};
const { showSubmenuIcon, openSubmenusOnClick } = context;
const { showSubmenuIcon, maxNestingLevel, openSubmenusOnClick } = context;
const { saveEntityRecord } = useDispatch( coreStore );

const {
Expand Down Expand Up @@ -350,8 +348,8 @@ export default function NavigationSubmenuEdit( {

return {
isAtMaxNesting:
getBlockParentsByBlockName( clientId, name ).length >=
MAX_NESTING,
getBlockParentsByBlockName( selectedBlockId, name )
draganescu marked this conversation as resolved.
Show resolved Hide resolved
.length >= maxNestingLevel,
isTopLevelItem:
getBlockParentsByBlockName( clientId, name ).length === 0,
isParentOfSelectedBlock: hasSelectedInnerBlock(
Expand Down
7 changes: 6 additions & 1 deletion packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@
},
"customOverlayTextColor": {
"type": "string"
},
"maxNestingLevel": {
"type": "number",
"default": 5
}
},
"usesContext": [ "navigationArea" ],
Expand All @@ -76,7 +80,8 @@
"showSubmenuIcon": "showSubmenuIcon",
"openSubmenusOnClick": "openSubmenusOnClick",
"style": "style",
"orientation": "orientation"
"orientation": "orientation",
"maxNestingLevel": "maxNestingLevel"
},
"supports": {
"align": [ "wide", "full" ],
Expand Down
40 changes: 40 additions & 0 deletions packages/e2e-tests/specs/editor/blocks/navigation.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -884,6 +884,46 @@ describe( 'Navigation', () => {
newMenuButton.click();
}

it( 'respects the nesting level', async () => {
await createNewPost();

await insertBlock( 'Navigation' );

const navBlock = await waitForBlock( 'Navigation' );

// Create empty Navigation block with no items
const startEmptyButton = await page.waitForXPath(
START_EMPTY_XPATH
);
await startEmptyButton.click();

await populateNavWithOneItem();

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

let code = await codeEditorInput.evaluate( ( el ) => el.value );
code = code.replace( '} /-->', ',"maxNestingLevel":0} /-->' );
await codeEditorInput.evaluate(
( el, newCode ) => ( el.value = newCode ),
code
);
await clickButton( 'Exit code editor' );

const blockAppender = navBlock.$( '.block-list-appender' );

expect( blockAppender ).not.toBeNull();

// Check the Submenu block is no longer present.
const navSubmenuSelector =
'[aria-label="Editor content"][role="region"] [aria-label="Block: Submenu"]';
const submenuBlock = await page.$( navSubmenuSelector );

expect( submenuBlock ).toBeFalsy();
} );

it( 'does not retain uncontrolled inner blocks when creating a new entity', async () => {
await createNewPost();
await clickOnMoreMenuItem( 'Code editor' );
Expand Down
3 changes: 2 additions & 1 deletion test/integration/fixtures/blocks/core__navigation.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"showSubmenuIcon": true,
"openSubmenusOnClick": false,
"overlayMenu": "mobile",
"hasIcon": true
"hasIcon": true,
"maxNestingLevel": 5
},
"innerBlocks": []
}
Expand Down