Skip to content

Commit

Permalink
Navigation link block: replace global shortcut event handlers with lo…
Browse files Browse the repository at this point in the history
…cal ones
  • Loading branch information
ellatrix committed Sep 2, 2021
1 parent 75932b1 commit 0446a7e
Showing 1 changed file with 11 additions and 21 deletions.
32 changes: 11 additions & 21 deletions packages/block-library/src/navigation-link/edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,14 @@ import { escape } from 'lodash';
import { createBlock } from '@wordpress/blocks';
import { useSelect, useDispatch } from '@wordpress/data';
import {
KeyboardShortcuts,
PanelBody,
Popover,
TextControl,
TextareaControl,
ToolbarButton,
ToolbarGroup,
} from '@wordpress/components';
import { rawShortcut, displayShortcut } from '@wordpress/keycodes';
import { displayShortcut, isKeyboardEvent, ENTER } from '@wordpress/keycodes';
import { __, sprintf } from '@wordpress/i18n';
import {
BlockControls,
Expand Down Expand Up @@ -467,6 +466,15 @@ export default function NavigationLinkEdit( {
customBackgroundColor,
} = getColors( context, ! isTopLevelLink );

function onKeyDown( event ) {
if (
isKeyboardEvent.primary( event, 'k' ) ||
( ! url && event.keyCode === ENTER )
) {
setIsLinkOpen( true );
}
}

const blockProps = useBlockProps( {
ref: listItemRef,
className: classnames( 'wp-block-navigation-item', {
Expand All @@ -486,6 +494,7 @@ export default function NavigationLinkEdit( {
color: ! textColor && customTextColor,
backgroundColor: ! backgroundColor && customBackgroundColor,
},
onKeyDown,
} );

if ( ! url ) {
Expand Down Expand Up @@ -566,13 +575,6 @@ export default function NavigationLinkEdit( {
<Fragment>
<BlockControls>
<ToolbarGroup>
<KeyboardShortcuts
bindGlobal
shortcuts={ {
[ rawShortcut.primary( 'k' ) ]: () =>
setIsLinkOpen( true ),
} }
/>
<ToolbarButton
name="link"
icon={ linkIcon }
Expand Down Expand Up @@ -626,12 +628,6 @@ export default function NavigationLinkEdit( {
{ /* eslint-enable */ }
{ ! url ? (
<div className="wp-block-navigation-link__placeholder-text">
<KeyboardShortcuts
shortcuts={ {
enter: () =>
isSelected && setIsLinkOpen( true ),
} }
/>
{ missingText }
</div>
) : (
Expand Down Expand Up @@ -672,12 +668,6 @@ export default function NavigationLinkEdit( {
onClose={ () => setIsLinkOpen( false ) }
anchorRef={ listItemRef.current }
>
<KeyboardShortcuts
bindGlobal
shortcuts={ {
escape: () => setIsLinkOpen( false ),
} }
/>
<LinkControl
className="wp-block-navigation-link__inline-link-input"
value={ link }
Expand Down

0 comments on commit 0446a7e

Please sign in to comment.