Skip to content

Commit e9daf2a

Browse files
getdavescruffian
andauthored
Add Navigation Add Page Button (#71192)
* Add Add page button to Navigation block toolbar - Creates a new hook that adds an Add page button to Navigation blocks - Button appears in the block toolbar when Navigation block is selected - Clicking the button inserts a new navigation-link block at the end - Hook follows the same pattern as navigation-link-view-button.js - Button works in all editing modes (not just contentOnly) * Fix Navigation Link Block to use contentOnly mode - Added useBlockEditingMode hook import - Used useBlockEditingMode() to get current block editing mode - Fixed undefined blockEditingMode variable error - Navigation Link Block now properly respects contentOnly mode * Move Navigation Add Page Button from hook to Navigation block - Moved NavigationAddPageButton component directly into Navigation block edit/index.js - Added required imports: ToolbarButton, ToolbarGroup, page icon, createBlock, __unstableBlockToolbarLastItem - Removed old navigation-add-page-button.js hook file - Removed import from hooks/index.js - Component now renders directly in Navigation block when isEntityAvailable is true - Maintains same functionality: only shows in contentOnly mode * Use DEFAULT_BLOCK constant from constants.js for createBlock - Import DEFAULT_BLOCK from ../constants - Replace hardcoded 'core/navigation-link' with DEFAULT_BLOCK.name - Makes the code more maintainable and consistent with constants usage * Use DEFAULT_BLOCK attributes when creating navigation link block - Pass kind and type attributes from DEFAULT_BLOCK constant to createBlock - Ensures new navigation link blocks are created with consistent default attributes - Makes the block creation more robust and aligned with constants * Pull in correct attributes for new block Co-authored-by: getdave <get_dave@git.wordpress.org> Co-authored-by: scruffian <scruffian@git.wordpress.org>
1 parent c3f4b72 commit e9daf2a

File tree

1 file changed

+50
-1
lines changed
  • packages/block-library/src/navigation/edit

1 file changed

+50
-1
lines changed

packages/block-library/src/navigation/edit/index.js

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
__experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown,
2727
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
2828
useBlockEditingMode,
29+
BlockControls,
2930
} from '@wordpress/block-editor';
3031
import { EntityProvider, store as coreStore } from '@wordpress/core-data';
3132

@@ -40,10 +41,13 @@ import {
4041
Button,
4142
Spinner,
4243
Notice,
44+
ToolbarButton,
45+
ToolbarGroup,
4346
} from '@wordpress/components';
4447
import { __ } from '@wordpress/i18n';
4548
import { speak } from '@wordpress/a11y';
46-
import { close, Icon } from '@wordpress/icons';
49+
import { close, Icon, page } from '@wordpress/icons';
50+
import { createBlock } from '@wordpress/blocks';
4751
import { useInstanceId } from '@wordpress/compose';
4852

4953
/**
@@ -75,6 +79,48 @@ import AccessibleDescription from './accessible-description';
7579
import AccessibleMenuDescription from './accessible-menu-description';
7680
import { unlock } from '../../lock-unlock';
7781
import { useToolsPanelDropdownMenuProps } from '../../utils/hooks';
82+
import { DEFAULT_BLOCK } from '../constants';
83+
84+
/**
85+
* Component that renders the Add page button for the Navigation block.
86+
*
87+
* @param {Object} props Component props.
88+
* @param {string} props.clientId Block client ID.
89+
* @return {JSX.Element|null} The Add page button component or null if not applicable.
90+
*/
91+
function NavigationAddPageButton( { clientId } ) {
92+
const { insertBlock } = useDispatch( blockEditorStore );
93+
const { getBlockCount } = useSelect( blockEditorStore );
94+
95+
const onAddPage = useCallback( () => {
96+
// Get the current number of blocks to insert at the end
97+
const blockCount = getBlockCount( clientId );
98+
99+
// Create a new navigation link block (default block)
100+
const newBlock = createBlock( DEFAULT_BLOCK.name, {
101+
kind: DEFAULT_BLOCK.attributes.kind,
102+
type: DEFAULT_BLOCK.attributes.type,
103+
} );
104+
105+
// Insert the block at the end of the navigation
106+
insertBlock( newBlock, blockCount, clientId );
107+
}, [ clientId, insertBlock, getBlockCount ] );
108+
109+
return (
110+
<BlockControls>
111+
<ToolbarGroup>
112+
<ToolbarButton
113+
name="add-page"
114+
icon={ page }
115+
title={ __( 'Add page' ) }
116+
onClick={ onAddPage }
117+
>
118+
{ __( 'Add page' ) }
119+
</ToolbarButton>
120+
</ToolbarGroup>
121+
</BlockControls>
122+
);
123+
}
78124

79125
function ColorTools( {
80126
textColor,
@@ -937,6 +983,9 @@ function Navigation( {
937983
blockEditingMode={ blockEditingMode }
938984
/>
939985
{ blockEditingMode === 'default' && stylingInspectorControls }
986+
{ blockEditingMode === 'contentOnly' && isEntityAvailable && (
987+
<NavigationAddPageButton clientId={ clientId } />
988+
) }
940989
{ blockEditingMode === 'default' && isEntityAvailable && (
941990
<InspectorControls group="advanced">
942991
{ hasResolvedCanUserUpdateNavigationMenu &&

0 commit comments

Comments
 (0)