Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inserter: Autofocus the Search input #1187

Merged
merged 7 commits into from
Jun 27, 2017
35 changes: 19 additions & 16 deletions editor/inserter/menu.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class InserterMenu extends Component {
this.nodes = {};
this.state = {
filterValue: '',
currentFocus: null,
currentFocus: 'search',
};
this.filter = this.filter.bind( this );
this.isShownBlock = this.isShownBlock.bind( this );
Expand Down Expand Up @@ -92,8 +92,8 @@ class InserterMenu extends Component {
}

findByIncrement( blockTypes, increment = 1 ) {
// Add on a fake search block to the list to cycle through.
const list = blockTypes.concat( { name: 'search' } );
// Prepend a fake search block to the list to cycle through.
const list = [ { name: 'search' }, ...blockTypes ];

const currentIndex = findIndex( list, ( blockType ) => this.state.currentFocus === blockType.name );
const nextIndex = currentIndex + increment;
Expand Down Expand Up @@ -228,8 +228,23 @@ class InserterMenu extends Component {
const { position, instanceId } = this.props;
const visibleBlocksByCategory = this.getVisibleBlocksByCategory( getBlockTypes() );

/* eslint-disable jsx-a11y/no-autofocus */
return (
<Popover position={ position } className="editor-inserter__menu">
<label htmlFor={ `editor-inserter__search-${ instanceId }` } className="screen-reader-text">
{ __( 'Search blocks' ) }
</label>
<input
autoFocus
id={ `editor-inserter__search-${ instanceId }` }
type="search"
placeholder={ __( 'Search…' ) }
className="editor-inserter__search"
onChange={ this.filter }
onClick={ this.setSearchFocus }
ref={ this.bindReferenceNode( 'search' ) }
tabIndex="-1"
/>
<div role="menu" className="editor-inserter__content">
{ getCategories()
.map( ( category ) => !! visibleBlocksByCategory[ category.slug ] && (
Expand Down Expand Up @@ -267,21 +282,9 @@ class InserterMenu extends Component {
) )
}
</div>
<label htmlFor={ `editor-inserter__search-${ instanceId }` } className="screen-reader-text">
{ __( 'Search blocks' ) }
</label>
<input
id={ `editor-inserter__search-${ instanceId }` }
type="search"
placeholder={ __( 'Search…' ) }
className="editor-inserter__search"
onChange={ this.filter }
onClick={ this.setSearchFocus }
ref={ this.bindReferenceNode( 'search' ) }
tabIndex="-1"
/>
</Popover>
);
/* eslint-enable jsx-a11y/no-autofocus */
}
}

Expand Down