Skip to content

Commit

Permalink
Add menuTitle attribute to navigation block and update label display
Browse files Browse the repository at this point in the history
  • Loading branch information
yogeshbhutkar committed Jan 1, 2025
1 parent b4304f8 commit 54b9364
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/reference-guides/core-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ A collection of blocks that allow visitors to get around your site. ([Source](ht
- **Category:** theme
- **Allowed Blocks:** core/navigation-link, core/search, core/social-links, core/page-list, core/spacer, core/home-link, core/site-title, core/site-logo, core/navigation-submenu, core/loginout, core/buttons
- **Supports:** align (full, wide), ariaLabel, inserter, interactivity, layout (allowSizingOnChildren, default, ~~allowInheriting~~, ~~allowSwitching~~, ~~allowVerticalAlignment~~), spacing (blockGap, units), typography (fontSize, lineHeight), ~~html~~, ~~renaming~~
- **Attributes:** __unstableLocation, backgroundColor, customBackgroundColor, customOverlayBackgroundColor, customOverlayTextColor, customTextColor, hasIcon, icon, maxNestingLevel, openSubmenusOnClick, overlayBackgroundColor, overlayMenu, overlayTextColor, ref, rgbBackgroundColor, rgbTextColor, showSubmenuIcon, templateLock, textColor
- **Attributes:** __unstableLocation, backgroundColor, customBackgroundColor, customOverlayBackgroundColor, customOverlayTextColor, customTextColor, hasIcon, icon, maxNestingLevel, menuTitle, openSubmenusOnClick, overlayBackgroundColor, overlayMenu, overlayTextColor, ref, rgbBackgroundColor, rgbTextColor, showSubmenuIcon, templateLock, textColor

## Custom Link

Expand Down
3 changes: 3 additions & 0 deletions packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
"ref": {
"type": "number"
},
"menuTitle": {
"type": "string"
},
"textColor": {
"type": "string"
},
Expand Down
16 changes: 16 additions & 0 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ function Navigation( {
__unstableLayoutClassNames: layoutClassNames,
} ) {
const {
menuTitle,
openSubmenusOnClick,
overlayMenu,
showSubmenuIcon,
Expand Down Expand Up @@ -300,6 +301,7 @@ function Navigation( {
canUserCreateNavigationMenus,
isResolvingCanUserCreateNavigationMenus,
hasResolvedCanUserCreateNavigationMenus,
navigationMenu,
} = useNavigationMenu( ref );

const navMenuResolvedButMissing =
Expand Down Expand Up @@ -342,6 +344,20 @@ function Navigation( {
? getNavigationFallbackId()
: null;

const initializeMenuTitle = useCallback( () => {
const { title: navTitle } = navigationMenu || {};

if ( ! menuTitle || ! navTitle || menuTitle === navTitle ) {
return;
}

setAttributes( { menuTitle: navTitle } );
}, [ navigationMenu, menuTitle, setAttributes ] );

useEffect( () => {
initializeMenuTitle();
}, [ initializeMenuTitle ] );

useEffect( () => {
// If:
// - there is an existing menu, OR
Expand Down
11 changes: 10 additions & 1 deletion packages/block-library/src/navigation/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* WordPress dependencies
*/
import { __ } from '@wordpress/i18n';
import { __, sprintf } from '@wordpress/i18n';
import { navigation as icon } from '@wordpress/icons';

/**
Expand Down Expand Up @@ -52,6 +52,15 @@ export const settings = {
},
edit,
save,
__experimentalLabel: ( { menuTitle } ) => {
return menuTitle
? sprintf(
/* translators: %s: menu title */
__( 'Navigation (%s)' ),
menuTitle
)
: __( 'Navigation' );
},
deprecated,
};

Expand Down

0 comments on commit 54b9364

Please sign in to comment.