Skip to content

Commit

Permalink
Use composite pattern to improve keyboard navigation on the inserter (#…
Browse files Browse the repository at this point in the history
…23610)

Co-authored-by: Haz <[email protected]>
  • Loading branch information
youknowriad and diegohaz committed Jul 15, 2020
1 parent 4ae9ab1 commit d8c7146
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 7 deletions.
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions packages/block-editor/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
"react-autosize-textarea": "^3.0.2",
"react-spring": "^8.0.19",
"react-transition-group": "^2.9.0",
"reakit": "1.1.0",
"redux-multi": "^0.1.12",
"refx": "^3.0.0",
"rememo": "^3.0.0",
Expand Down
27 changes: 25 additions & 2 deletions packages/block-editor/src/components/block-types-list/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
/**
* External dependencies
*/
import { Composite, useCompositeState } from 'reakit';

/**
* WordPress dependencies
*/
import { getBlockMenuDefaultClassName } from '@wordpress/blocks';
import { useEffect } from '@wordpress/element';

/**
* Internal dependencies
Expand All @@ -14,16 +20,32 @@ function BlockTypesList( {
onSelect,
onHover = () => {},
children,
label,
} ) {
const composite = useCompositeState();
const normalizedItems = includeVariationsInInserterItems( items );
const orderId = normalizedItems.reduce(
( acc, item ) => acc + '--' + item.id,
''
);

// This ensures the composite state refreshes when the list order changes.
useEffect( () => {
composite.unstable_sort();
}, [ composite.unstable_sort, orderId ] );

return (
/*
* 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="block-editor-block-types-list">
<Composite
{ ...composite }
role="listbox"
className="block-editor-block-types-list"
aria-label={ label }
>
{ normalizedItems.map( ( item ) => {
return (
<InserterListItem
Expand All @@ -40,11 +62,12 @@ function BlockTypesList( {
onBlur={ () => onHover( null ) }
isDisabled={ item.isDisabled }
title={ item.title }
composite={ composite }
/>
);
} ) }
{ children }
</ul>
</Composite>
/* eslint-enable jsx-a11y/no-redundant-roles */
);
}
Expand Down
13 changes: 9 additions & 4 deletions packages/block-editor/src/components/inserter-list-item/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* External dependencies
*/
import classnames from 'classnames';
import { CompositeItem } from 'reakit';

/**
* WordPress dependencies
Expand All @@ -19,6 +20,7 @@ function InserterListItem( {
isDisabled,
title,
className,
composite,
...props
} ) {
const itemIconStyle = icon
Expand All @@ -29,8 +31,11 @@ function InserterListItem( {
: {};

return (
<li className="block-editor-block-types-list__list-item">
<Button
<div className="block-editor-block-types-list__list-item">
<CompositeItem
role="option"
as={ Button }
{ ...composite }
className={ classnames(
'block-editor-block-types-list__item',
className
Expand All @@ -51,8 +56,8 @@ function InserterListItem( {
<span className="block-editor-block-types-list__item-title">
{ title }
</span>
</Button>
</li>
</CompositeItem>
</div>
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ export function BlockTypesTab( {
items={ filteredItems }
onSelect={ onSelectItem }
onHover={ onHover }
label={ __( 'Child Blocks' ) }
/>
</ChildBlocks>
) }
Expand All @@ -145,6 +146,7 @@ export function BlockTypesTab( {
items={ suggestedItems }
onSelect={ onSelectItem }
onHover={ onHover }
label={ _x( 'Most used', 'blocks' ) }
/>
</InserterPanel>
) }
Expand All @@ -165,6 +167,7 @@ export function BlockTypesTab( {
items={ categoryItems }
onSelect={ onSelectItem }
onHover={ onHover }
label={ category.title }
/>
</InserterPanel>
);
Expand All @@ -179,6 +182,7 @@ export function BlockTypesTab( {
items={ uncategorizedItems }
onSelect={ onSelectItem }
onHover={ onHover }
label={ __( 'Uncategorized' ) }
/>
</InserterPanel>
) }
Expand All @@ -200,6 +204,7 @@ export function BlockTypesTab( {
items={ collectionItems }
onSelect={ onSelectItem }
onHover={ onHover }
label={ collection.title }
/>
</InserterPanel>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ function QuickInserterList( {
items={ shownBlockTypes }
onSelect={ onSelectBlockType }
onHover={ onHover }
label={ __( 'Blocks' ) }
/>
</InserterPanel>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ function ReusableBlocksList( {
items={ filteredItems }
onSelect={ onSelectItem }
onHover={ onHover }
label={
filterValue
? __( 'Search Results' )
: __( 'Reusable blocks' )
}
/>
</InserterPanel>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,6 @@ describe( 'adding blocks', () => {

// Tab to the block list
await page.keyboard.press( 'Tab' );
await page.keyboard.press( 'Tab' );

// Expect the block list to be the active element.
activeElementClassList = await page.evaluate(
Expand Down

0 comments on commit d8c7146

Please sign in to comment.