Skip to content

Commit

Permalink
navigation-menu-item: close LinkControl in ESCAPE
Browse files Browse the repository at this point in the history
  • Loading branch information
retrofox committed Oct 24, 2019
1 parent 4c4a2b5 commit f3cc035
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions packages/block-library/src/navigation-menu-item/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
DOWN,
BACKSPACE,
ENTER,
ESCAPE,
} from '@wordpress/keycodes';
import { __ } from '@wordpress/i18n';
import {
Expand Down Expand Up @@ -51,14 +52,20 @@ function NavigationMenuItemEdit( {

const inputValue = urlInput !== null ? urlInput : url;

const onKeyDown = ( event ) => {
if ( [ LEFT, DOWN, RIGHT, UP, BACKSPACE, ENTER ].indexOf( event.keyCode ) > -1 ) {
const handleLinkControlOnKeyDown = ( event ) => {
const { keyCode } = event;

if ( [ LEFT, DOWN, RIGHT, UP, BACKSPACE, ENTER ].indexOf( keyCode ) > -1 ) {
// Stop the key event from propagating up to ObserveTyping.startTypingInTextField.
event.stopPropagation();
}

if ( ESCAPE === keyCode ) {
closeLinkControl ();
}
};

const closeURLPopover = () => {
const closeLinkControl = () => {
setIsEditingLink( false );
setUrlInput( null );
setIsLinkOpen( false );
Expand All @@ -71,7 +78,7 @@ function NavigationMenuItemEdit( {
if ( autocompleteElement && autocompleteElement.contains( event.target ) ) {
return;
}
closeURLPopover();
closeLinkControl ();
};

const { label, url } = attributes;
Expand Down Expand Up @@ -108,7 +115,7 @@ function NavigationMenuItemEdit( {
{ isLinkOpen &&
<LinkControl
className="wp-block-navigation-menu-item__inline-link-input"
onKeyDown={ onKeyDown }
onKeyDown={ handleLinkControlOnKeyDown }
onKeyPress={ ( event ) => { event.stopPropagation() } }
/>
}
Expand Down

0 comments on commit f3cc035

Please sign in to comment.