diff --git a/packages/block-library/src/navigation-submenu/edit.js b/packages/block-library/src/navigation-submenu/edit.js index 206fe9441e7af..d1faa6b6b99e9 100644 --- a/packages/block-library/src/navigation-submenu/edit.js +++ b/packages/block-library/src/navigation-submenu/edit.js @@ -42,6 +42,7 @@ import { placeCaretAtHorizontalEdge } from '@wordpress/dom'; import { link as linkIcon } from '@wordpress/icons'; import { store as coreStore } from '@wordpress/core-data'; import { speak } from '@wordpress/a11y'; +import { createBlock } from '@wordpress/blocks'; /** * Internal dependencies @@ -291,6 +292,7 @@ export default function NavigationSubmenuEdit( { }; const { showSubmenuIcon } = context; const { saveEntityRecord } = useDispatch( coreStore ); + const { insertBlock } = useDispatch( blockEditorStore ); const [ isLinkOpen, setIsLinkOpen ] = useState( false ); const listItemRef = useRef( null ); const isDraggingWithin = useIsDraggingWithin( listItemRef ); @@ -303,6 +305,7 @@ export default function NavigationSubmenuEdit( { isParentOfSelectedBlock, isImmediateParentOfSelectedBlock, hasDescendants, + numberOfDescendants, selectedBlockHasDescendants, userCanCreatePages, userCanCreatePosts, @@ -335,6 +338,7 @@ export default function NavigationSubmenuEdit( { false ), hasDescendants: !! descendants, + numberOfDescendants: descendants, selectedBlockHasDescendants: !! getClientIdsOfDescendants( [ selectedBlockId, ] )?.length, @@ -429,7 +433,7 @@ export default function NavigationSubmenuEdit( { const blockProps = useBlockProps( { ref: listItemRef, - className: classnames( { + className: classnames( 'wp-block-navigation-item', { 'is-editing': isSelected || isParentOfSelectedBlock, 'is-dragging-within': isDraggingWithin, 'has-link': !! url, @@ -455,6 +459,17 @@ export default function NavigationSubmenuEdit( { pull( ALLOWED_BLOCKS, 'core/navigation-submenu' ); } + // Add a link when creating a new Submenu. + useEffect( () => { + if ( ! hasDescendants ) { + insertBlock( + createBlock( 'core/navigation-link' ), + numberOfDescendants, + clientId + ); + } + }, [] ); + const innerBlocksProps = useInnerBlocksProps( { className: classnames( 'wp-block-navigation__submenu-container', { diff --git a/packages/block-library/src/navigation-submenu/index.php b/packages/block-library/src/navigation-submenu/index.php index 7375dc4a257fb..96a5c31bca76b 100644 --- a/packages/block-library/src/navigation-submenu/index.php +++ b/packages/block-library/src/navigation-submenu/index.php @@ -164,7 +164,7 @@ function render_block_core_navigation_submenu( $attributes, $content, $block ) { $wrapper_attributes = get_block_wrapper_attributes( array( - 'class' => $css_classes . ( $has_submenu ? ' has-child' : '' ) . + 'class' => $css_classes . ' wp-block-navigation-item' . ( $has_submenu ? ' has-child' : '' ) . ( $is_active ? ' current-menu-item' : '' ), 'style' => $style_attribute, )