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

Simplify the Inserter accessibility #7058

Merged
merged 2 commits into from
Jun 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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