Skip to content

Commit

Permalink
Use keyup to make Escape close the menu and simplify all the things.
Browse files Browse the repository at this point in the history
  • Loading branch information
afercia committed Jul 28, 2017
1 parent 2a1b53a commit ed9cd44
Showing 1 changed file with 23 additions and 49 deletions.
72 changes: 23 additions & 49 deletions components/dropdown-menu/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { findIndex } from 'lodash';
import IconButton from 'components/icon-button';
import Dashicon from 'components/dashicon';
import { findDOMNode } from 'element';
import { ESCAPE, LEFT, UP, RIGHT, DOWN } from 'utils/keycodes';
import { TAB, ESCAPE, LEFT, UP, RIGHT, DOWN } from 'utils/keycodes';

/**
* Internal dependencies
Expand All @@ -30,8 +30,6 @@ class DropdownMenu extends wp.element.Component {
this.focusNext = this.focusNext.bind( this );
this.handleKeyDown = this.handleKeyDown.bind( this );
this.handleKeyUp = this.handleKeyUp.bind( this );
this.handleFocus = this.handleFocus.bind( this );
this.handleBlur = this.handleBlur.bind( this );
this.menuNode = null;
this.state = {
open: false,
Expand All @@ -50,26 +48,10 @@ class DropdownMenu extends wp.element.Component {
this.closeMenu();
}

handleFocus() {
this.hasFocus = true;
}

handleBlur() {
this.hasFocus = false;

this.maybeCloseMenuOnBlur = setTimeout( () => {
this.closeMenu();
}, 100 );
}

closeMenu() {
clearTimeout( this.maybeCloseMenuOnBlur );

if ( ! this.hasFocus ) {
this.setState( {
open: false,
} );
}
this.setState( {
open: false,
} );
}

toggleMenu() {
Expand All @@ -90,11 +72,7 @@ class DropdownMenu extends wp.element.Component {

focusIndex( index ) {
if ( this.menuNode ) {
if ( index < 0 ) {
this.menuNode.previousElementSibling.focus();
} else {
this.menuNode.children[ index ].focus();
}
this.menuNode.children[ index ].focus();
}
}

Expand All @@ -114,29 +92,31 @@ class DropdownMenu extends wp.element.Component {

handleKeyUp( event ) {
// TODO: find a better way to isolate events on nested components see GH issue #1973.
if ( this.state.open ) {
/*
* VisualEditorBlock uses keyup to deselect the block. When the menu is
* open we need to stop propagation of keyup after Escape has been pressed
* to close the menu, otherwise the whole block toolbar will disappear.
* When the menu is closed, Escape will make the toolbar disappear as intended.
*/
/*
* VisualEditorBlock uses a keyup event to deselect the block. When the
* menu is open we need to stop propagation after Escape has been pressed
* so we use a keyup event instead of keydown, otherwise the whole block
* toolbar will disappear.
*/
if ( event.keyCode === ESCAPE && this.state.open ) {
event.preventDefault();
event.stopPropagation();
const node = findDOMNode( this );
const toggle = node.querySelector( '.components-dropdown-menu__toggle' );
toggle.focus();
this.closeMenu();
if ( this.props.onSelect ) {
this.props.onSelect( null );
}
}
}

handleKeyDown( keydown ) {
if ( this.state.open ) {
switch ( keydown.keyCode ) {
case ESCAPE:
keydown.preventDefault();
case TAB:
keydown.stopPropagation();
const node = findDOMNode( this );
const toggle = node.querySelector( '.components-dropdown-menu__toggle' );
toggle.focus();
if ( this.props.onSelect ) {
this.props.onSelect( null );
}
this.closeMenu();
break;

case LEFT:
Expand All @@ -160,6 +140,7 @@ class DropdownMenu extends wp.element.Component {
switch ( keydown.keyCode ) {
case DOWN:
keydown.preventDefault();
keydown.stopPropagation();
this.toggleMenu();
break;

Expand All @@ -169,10 +150,6 @@ class DropdownMenu extends wp.element.Component {
}
}

componentWillUnmount() {
clearTimeout( this.maybeCloseMenuOnBlur );
}

componentDidUpdate( prevProps, prevState ) {
// Focus the first item when the menu opens.
if ( ! prevState.open && this.state.open ) {
Expand Down Expand Up @@ -220,9 +197,6 @@ class DropdownMenu extends wp.element.Component {
role="menu"
aria-label={ menuLabel }
ref={ this.bindMenuReferenceNode }
onFocus={ this.handleFocus }
onBlur={ this.handleBlur }
tabIndex="-1"
>
{ controls.map( ( control, index ) => (
<IconButton
Expand Down

0 comments on commit ed9cd44

Please sign in to comment.