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

Navigation: Add delete nav menu button #35981

Merged
merged 3 commits into from
Nov 1, 2021
Merged
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
14 changes: 12 additions & 2 deletions packages/block-library/src/navigation/edit/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import NavigationInnerBlocks from './inner-blocks';
import NavigationMenuSelector from './navigation-menu-selector';
import NavigationMenuNameControl from './navigation-menu-name-control';
import UnsavedInnerBlocks from './unsaved-inner-blocks';
import NavigationMenuDeleteControl from './navigation-menu-delete-control';

function getComputedStyle( node ) {
return node.ownerDocument.defaultView.getComputedStyle( node );
Expand Down Expand Up @@ -112,7 +113,7 @@ function Navigation( {
[ clientId ]
);
const hasExistingNavItems = !! innerBlocks.length;
const { selectBlock } = useDispatch( blockEditorStore );
const { replaceInnerBlocks, selectBlock } = useDispatch( blockEditorStore );

const [ isPlaceholderShown, setIsPlaceholderShown ] = useState(
! hasExistingNavItems
Expand Down Expand Up @@ -298,8 +299,17 @@ function Navigation( {
{ listViewModal }
<InspectorControls>
{ isEntityAvailable && (
<PanelBody title={ __( 'Navigation menu name' ) }>
<PanelBody title={ __( 'Navigation menu' ) }>
<NavigationMenuNameControl />
<NavigationMenuDeleteControl
onDelete={ () => {
replaceInnerBlocks( clientId, [] );
setAttributes( {
navigationMenuId: undefined,
} );
setIsPlaceholderShown( true );
} }
/>
</PanelBody>
) }
{ hasSubmenuIndicatorSetting && (
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/**
* WordPress dependencies
*/
import { Button, Flex, FlexItem, Modal } from '@wordpress/components';
import {
store as coreStore,
useEntityId,
useEntityProp,
} from '@wordpress/core-data';
import { useDispatch } from '@wordpress/data';
import { useState } from '@wordpress/element';
import { __, sprintf } from '@wordpress/i18n';

export default function NavigationMenuDeleteControl( { onDelete } ) {
const [ isConfirmModalVisible, setIsConfirmModalVisible ] = useState(
false
);
const id = useEntityId( 'postType', 'wp_navigation' );
const [ title ] = useEntityProp( 'postType', 'wp_navigation', 'title' );
const { deleteEntityRecord } = useDispatch( coreStore );

return (
<>
<Button
className="wp-block-navigation-delete-menu-button"
variant="secondary"
isDestructive
onClick={ () => {
setIsConfirmModalVisible( true );
} }
>
{ __( 'Delete menu' ) }
</Button>
{ isConfirmModalVisible && (
<Modal
title={ sprintf(
/* translators: %s: the name of a menu to delete */
__( 'Delete %s' ),
title
) }
closeLabel={ __( 'Cancel' ) }
onRequestClose={ () => setIsConfirmModalVisible( false ) }
>
<p>
{ __(
'Are you sure you want to delete this navigation menu?'
) }
</p>
<Flex justify="flex-end">
<FlexItem>
<Button
variant="secondary"
onClick={ () => {
setIsConfirmModalVisible( false );
} }
>
{ __( 'Cancel' ) }
</Button>
</FlexItem>
<FlexItem>
<Button
variant="primary"
onClick={ () => {
deleteEntityRecord(
'postType',
'wp_navigation',
id,
{ force: true }
);
onDelete();
} }
>
{ __( 'Confirm' ) }
</Button>
</FlexItem>
</Flex>
</Modal>
) }
</>
);
}
5 changes: 5 additions & 0 deletions packages/block-library/src/navigation/editor.scss
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,8 @@ body.editor-styles-wrapper
margin-top: 0;
}
}

.wp-block-navigation-delete-menu-button {
width: 100%;
justify-content: center;
}