Skip to content
Merged
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
51 changes: 50 additions & 1 deletion packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import {
__experimentalColorGradientSettingsDropdown as ColorGradientSettingsDropdown,
__experimentalUseMultipleOriginColorsAndGradients as useMultipleOriginColorsAndGradients,
useBlockEditingMode,
BlockControls,
} from '@wordpress/block-editor';
import { EntityProvider, store as coreStore } from '@wordpress/core-data';

Expand All @@ -40,10 +41,13 @@ import {
Button,
Spinner,
Notice,
ToolbarButton,
ToolbarGroup,
} from '@wordpress/components';
import { __ } from '@wordpress/i18n';
import { speak } from '@wordpress/a11y';
import { close, Icon } from '@wordpress/icons';
import { close, Icon, page } from '@wordpress/icons';
import { createBlock } from '@wordpress/blocks';
import { useInstanceId } from '@wordpress/compose';

/**
Expand Down Expand Up @@ -75,6 +79,48 @@ import AccessibleDescription from './accessible-description';
import AccessibleMenuDescription from './accessible-menu-description';
import { unlock } from '../../lock-unlock';
import { useToolsPanelDropdownMenuProps } from '../../utils/hooks';
import { DEFAULT_BLOCK } from '../constants';

/**
* Component that renders the Add page button for the Navigation block.
*
* @param {Object} props Component props.
* @param {string} props.clientId Block client ID.
* @return {JSX.Element|null} The Add page button component or null if not applicable.
*/
function NavigationAddPageButton( { clientId } ) {
const { insertBlock } = useDispatch( blockEditorStore );
const { getBlockCount } = useSelect( blockEditorStore );

const onAddPage = useCallback( () => {
// Get the current number of blocks to insert at the end
const blockCount = getBlockCount( clientId );

// Create a new navigation link block (default block)
const newBlock = createBlock( DEFAULT_BLOCK.name, {
kind: DEFAULT_BLOCK.attributes.kind,
type: DEFAULT_BLOCK.attributes.type,
} );

// Insert the block at the end of the navigation
insertBlock( newBlock, blockCount, clientId );
}, [ clientId, insertBlock, getBlockCount ] );

return (
<BlockControls>
<ToolbarGroup>
<ToolbarButton
name="add-page"
icon={ page }
title={ __( 'Add page' ) }
onClick={ onAddPage }
>
{ __( 'Add page' ) }
</ToolbarButton>
</ToolbarGroup>
</BlockControls>
);
}

function ColorTools( {
textColor,
Expand Down Expand Up @@ -937,6 +983,9 @@ function Navigation( {
blockEditingMode={ blockEditingMode }
/>
{ blockEditingMode === 'default' && stylingInspectorControls }
{ blockEditingMode === 'contentOnly' && isEntityAvailable && (
<NavigationAddPageButton clientId={ clientId } />
) }
{ blockEditingMode === 'default' && isEntityAvailable && (
<InspectorControls group="advanced">
{ hasResolvedCanUserUpdateNavigationMenu &&
Expand Down
Loading