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

Allow setting defaultBlock in navigation block #52610

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
7 changes: 7 additions & 0 deletions packages/block-library/src/navigation/block.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@
"templateLock": {
"type": [ "string", "boolean" ],
"enum": [ "all", "insert", "contentOnly", false ]
},
"defaultBlock": {
"type": "object",
"default": {
"name": "core/navigation-link",
"attributes": {}
}
}
},
"providesContext": {
Expand Down
2 changes: 2 additions & 0 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ function Navigation( {
overlayMenu,
showSubmenuIcon,
templateLock,
defaultBlock,
layout: {
justifyContent,
orientation = 'horizontal',
Expand Down Expand Up @@ -858,6 +859,7 @@ function Navigation( {
}
templateLock={ templateLock }
orientation={ orientation }
defaultBlock={ defaultBlock }
/>
) }
</ResponsiveWrapper>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export default function NavigationInnerBlocks( {
hasCustomPlaceholder,
orientation,
templateLock,
defaultBlock,
} ) {
const {
isImmediateParentOfSelectedBlock,
Expand Down Expand Up @@ -88,6 +89,11 @@ export default function NavigationInnerBlocks( {
const showPlaceholder =
! hasCustomPlaceholder && ! hasMenuItems && ! isSelected;

// If the `defaultBlock` attribute is set and itself has an attributes object with values then use it
// otherwise fallback to the DEFAULT_BLOCK constant.
// This allows site owners to set the default `core/navigation-link` variation to use.
const navDefaultBlock = defaultBlock?.attributes && Object.keys(defaultBlock.attributes).length > 0 ? defaultBlock : DEFAULT_BLOCK;

const innerBlocksProps = useInnerBlocksProps(
{
className: 'wp-block-navigation__container',
Expand All @@ -98,7 +104,7 @@ export default function NavigationInnerBlocks( {
onChange,
allowedBlocks: ALLOWED_BLOCKS,
prioritizedInserterBlocks: PRIORITIZED_INSERTER_BLOCKS,
defaultBlock: DEFAULT_BLOCK,
defaultBlock: navDefaultBlock,
directInsert: shouldDirectInsert,
orientation,
templateLock,
Expand Down