Skip to content

Commit

Permalink
InserterMenu: Don't leak arrow key presses
Browse files Browse the repository at this point in the history
As a user, pressing the wrong group of arrows (e.g. down instead of
right or tab) shouldn't take the focus out of the inserter. This is most
easily experienced when using VisualEditorSiblingInserter, the inserter
sitting in between any two blocks.
  • Loading branch information
mcsf committed Nov 14, 2017
1 parent dddaae0 commit ac75a8a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
2 changes: 1 addition & 1 deletion components/navigable-container/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { Component } from '@wordpress/element';
import { focus, keycodes } from '@wordpress/utils';

/**
* Module Constants
* Module constants
*/
const { UP, DOWN, LEFT, RIGHT, TAB } = keycodes;

Expand Down
34 changes: 32 additions & 2 deletions editor/components/inserter/menu.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,17 @@
/**
* External dependencies
*/
import { flow, groupBy, sortBy, findIndex, filter, find, some } from 'lodash';
import {
filter,
find,
findIndex,
flow,
groupBy,
includes,
pick,
some,
sortBy,
} from 'lodash';
import { connect } from 'react-redux';

/**
Expand All @@ -16,6 +26,7 @@ import {
withSpokenMessages,
} from '@wordpress/components';
import { getCategories, getBlockTypes } from '@wordpress/blocks';
import { keycodes } from '@wordpress/utils';

/**
* Internal dependencies
Expand All @@ -35,6 +46,11 @@ export const searchBlocks = ( blocks, searchTerm ) => {
);
};

/**
* Module constants
*/
const ARROWS = pick( keycodes, [ 'UP', 'DOWN', 'LEFT', 'RIGHT' ] );

export class InserterMenu extends Component {
constructor() {
super( ...arguments );
Expand Down Expand Up @@ -198,12 +214,26 @@ export class InserterMenu extends Component {
}
}

interceptArrows( event ) {
if ( includes( ARROWS, event.keyCode ) ) {
// Prevent cases of focus being unexpectedly stolen up in the tree,
// notably when using VisualEditorSiblingInserter, where focus is
// moved to sibling blocks.
//
// We don't need to stop the native event, which has its uses, e.g.
// allowing window scrolling.
event.stopPropagation();
}
}

render() {
const { instanceId } = this.props;
const isSearching = this.state.filterValue;

return (
<TabbableContainer className="editor-inserter__menu" deep>
<TabbableContainer className="editor-inserter__menu" deep
onKeyDown={ this.interceptArrows }
>
<label htmlFor={ `editor-inserter__search-${ instanceId }` } className="screen-reader-text">
{ __( 'Search for a block' ) }
</label>
Expand Down

5 comments on commit ac75a8a

@ephox-mogran
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd prefer to do this in a different way. TabbableContainer should handle it via a prop I think, as it's going to be common.

@ephox-mogran
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The capability was there at one point, but we removed it in some of the refactoring (I think). I think TabbableContainer should be able to stop arrow keys propagating. Or do you think it shouldn't be its concern?

@mcsf
Copy link
Contributor Author

@mcsf mcsf commented on ac75a8a Nov 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was just about to ask you about this change. I initially thought of making this a proper option, but I wonder if it prematurely adds complexity/burden of choice. What do you think?

it's going to be common

I agree it's very plausible, but so far still speculative. Should we hold off, or do you prefer implementing now? My main motivation was to not ship the PR with that unexpected behavior of focus shift.

@ephox-mogran
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see your point. I'm happy enough with it now, but won't be surprised if we need to change it later :)

@mcsf
Copy link
Contributor Author

@mcsf mcsf commented on ac75a8a Nov 14, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't be surprised if we need to change it later

Totally, it's thus paramount that our code remain easy to refactor. :)

Please sign in to comment.