Skip to content

Commit

Permalink
Navigation Screen: Display error notice inside modal (#34884)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mamaduka authored Sep 17, 2021
1 parent 44ed70a commit dc271ff
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 12 deletions.
22 changes: 13 additions & 9 deletions packages/edit-navigation/src/components/add-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import classnames from 'classnames';
*/
import { useState } from '@wordpress/element';
import { useDispatch } from '@wordpress/data';
import { TextControl, Button } from '@wordpress/components';
import { Button, TextControl, withNotices } from '@wordpress/components';
import { useFocusOnMount } from '@wordpress/compose';
import { __, sprintf } from '@wordpress/i18n';
import { store as noticesStore } from '@wordpress/notices';
Expand All @@ -18,22 +18,23 @@ import { store as coreStore } from '@wordpress/core-data';
const menuNameMatches = ( menuName ) => ( menu ) =>
menu.name.toLowerCase() === menuName.toLowerCase();

export default function AddMenu( {
function AddMenu( {
className,
menus,
onCreate,
titleText,
helpText,
focusInputOnMount = false,
noticeUI,
noticeOperations,
} ) {
const inputRef = useFocusOnMount( focusInputOnMount );
const [ menuName, setMenuName ] = useState( '' );
const { createErrorNotice, createInfoNotice, removeNotice } = useDispatch(
noticesStore
);
const [ isCreatingMenu, setIsCreatingMenu ] = useState( false );
const { createInfoNotice } = useDispatch( noticesStore );
const { saveMenu } = useDispatch( coreStore );

const inputRef = useFocusOnMount( focusInputOnMount );
const { createErrorNotice, removeAllNotices } = noticeOperations;

const createMenu = async ( event ) => {
event.preventDefault();
Expand All @@ -42,8 +43,8 @@ export default function AddMenu( {
return;
}

// Remove any existing notices so duplicates aren't created.
removeNotice( 'edit-navigation-error' );
// Remove any existing notices.
removeAllNotices();

if ( some( menus, menuNameMatches( menuName ) ) ) {
const message = sprintf(
Expand All @@ -53,7 +54,7 @@ export default function AddMenu( {
),
menuName
);
createErrorNotice( message, { id: 'edit-navigation-error' } );
createErrorNotice( message );
return;
}

Expand All @@ -79,6 +80,7 @@ export default function AddMenu( {
className={ classnames( 'edit-navigation-add-menu', className ) }
onSubmit={ createMenu }
>
{ noticeUI }
{ titleText && (
<h3 className="edit-navigation-add-menu__title">
{ titleText }
Expand All @@ -104,3 +106,5 @@ export default function AddMenu( {
</form>
);
}

export default withNotices( AddMenu );
19 changes: 19 additions & 0 deletions packages/edit-navigation/src/components/add-menu/style.scss
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
.edit-navigation-add-menu {
display: flex;
flex-direction: column;

// Notices.
.components-with-notices-ui {
margin-bottom: $grid-unit-20;

// Notice is too big with default styles.
.components-notice {
margin: 0;
padding: $grid-unit-10 $grid-unit-15;
}

.components-notice__content {
margin: 0;
}

.components-notice__dismiss {
align-self: center;
}
}
}

.edit-navigation-add-menu__title {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export default function NewButton( { menus } ) {
{ isModalOpen && (
<Modal
title={ __( 'Create a new menu' ) }
className="edit-navigation-menu-switcher__modal"
onRequestClose={ () => setIsModalOpen( false ) }
>
<AddMenu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ export default function MenuSwitcher( {
{ isModalVisible && (
<Modal
title={ __( 'Create a new menu' ) }
className="edit-navigation-menu-switcher__modal"
onRequestClose={ closeModal }
>
<AddMenu
className="edit-navigation-menu-switcher__add-menu"
menus={ menus }
onCreate={ ( menuId ) => {
closeModal();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.edit-navigation-header__add-menu {
min-width: 220px;
.edit-navigation-menu-switcher__modal {
max-width: 400px;
}

0 comments on commit dc271ff

Please sign in to comment.