Skip to content

Commit

Permalink
Simplify the Inserter accessibility (#7058)
Browse files Browse the repository at this point in the history
* Remove ARIA roles and arrows navigation.

* Use a list for the block types list.
  • Loading branch information
afercia authored and youknowriad committed Jun 4, 2018
1 parent 80864d4 commit 60d1f55
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 81 deletions.
116 changes: 36 additions & 80 deletions editor/components/inserter/item-list.js
Original file line number Diff line number Diff line change
@@ -1,109 +1,65 @@
/**
* External dependencies
*/
import { isEqual } from 'lodash';
import classnames from 'classnames';

/**
* WordPress dependencies
*/
import { Component } from '@wordpress/element';
import { NavigableMenu } from '@wordpress/components';
import { getBlockMenuDefaultClassName } from '@wordpress/blocks';

/**
* Internal dependencies
*/
import BlockIcon from '../block-icon';

function deriveActiveItems( items ) {
return items.filter( ( item ) => ! item.isDisabled );
}

class ItemList extends Component {
constructor() {
super( ...arguments );
this.onNavigate = this.onNavigate.bind( this );
this.activeItems = deriveActiveItems( this.props.items );
this.state = {
current: this.activeItems.length > 0 ? this.activeItems[ 0 ] : null,
};
}

componentDidUpdate( prevProps ) {
if ( ! isEqual( this.props.items, prevProps.items ) ) {
this.activeItems = deriveActiveItems( this.props.items );

// Try and preserve any still valid selected state.
const currentIsStillActive = this.state.current && this.activeItems.some( ( item ) =>
item.id === this.state.current.id
);

if ( ! currentIsStillActive ) {
this.setState( {
current: this.activeItems.length > 0 ? this.activeItems[ 0 ] : null,
} );
}
}
}

onNavigate( index ) {
const { activeItems } = this;
const dest = activeItems[ index ];
if ( dest ) {
this.setState( {
current: dest,
} );
}
}

render() {
const { items, onSelect, onHover } = this.props;
const { current } = this.state;

return (
<NavigableMenu
className="editor-inserter__item-list"
orientation="both"
cycle={ false }
onNavigate={ this.onNavigate }
>
/*
* Disable reason: The `list` ARIA role is redundant but
* Safari+VoiceOver won't announce the list otherwise.
*/
/* eslint-disable jsx-a11y/no-redundant-roles */
<ul role="list" className="editor-inserter__list">
{ items.map( ( item ) => {
const isCurrent = current && current.id === item.id;
return (
<button
role="menuitem"
key={ item.id }
className={
classnames(
'editor-inserter__item',
getBlockMenuDefaultClassName( item.id ),
{
'editor-inserter__item-has-children': item.hasChildBlocks,
}
)
}
onClick={ () => onSelect( item ) }
tabIndex={ isCurrent || item.isDisabled ? null : '-1' }
disabled={ item.isDisabled }
onMouseEnter={ () => onHover( item ) }
onMouseLeave={ () => onHover( null ) }
onFocus={ () => onHover( item ) }
onBlur={ () => onHover( null ) }
aria-label={ item.title } // Fix for IE11 and JAWS 2018.
>
<span className="editor-inserter__item-icon">
<BlockIcon icon={ item.icon } />
{ item.hasChildBlocks && <span className="editor-inserter__item-icon-stack" /> }
</span>
<li className="editor-inserter__list-item" key={ item.id }>
<button
className={
classnames(
'editor-inserter__item',
getBlockMenuDefaultClassName( item.id ),
{
'editor-inserter__item-has-children': item.hasChildBlocks,
}
)
}
onClick={ () => onSelect( item ) }
disabled={ item.isDisabled }
onMouseEnter={ () => onHover( item ) }
onMouseLeave={ () => onHover( null ) }
onFocus={ () => onHover( item ) }
onBlur={ () => onHover( null ) }
aria-label={ item.title } // Fix for IE11 and JAWS 2018.
>
<span className="editor-inserter__item-icon">
<BlockIcon icon={ item.icon } />
{ item.hasChildBlocks && <span className="editor-inserter__item-icon-stack" /> }
</span>

<span className="editor-inserter__item-title">
{ item.title }
</span>
</button>
<span className="editor-inserter__item-title">
{ item.title }
</span>
</button>
</li>
);
} ) }
</NavigableMenu>
</ul>
/* eslint-enable jsx-a11y/no-redundant-roles */
);
}
}
Expand Down
8 changes: 7 additions & 1 deletion editor/components/inserter/style.scss
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,14 @@ $block-inserter-search-height: 38px;
}
}

.editor-inserter__item-list {
.editor-inserter__list {
list-style: none;
margin: 0 -8px;
padding: 0;
}

.editor-inserter__list-item {
display: inline;
}

.editor-inserter__item {
Expand Down

0 comments on commit 60d1f55

Please sign in to comment.