Skip to content

Commit

Permalink
Accessibility improvements for List View Part 2 (#38358)
Browse files Browse the repository at this point in the history
* Add aria-expanded to block selection button to force the announcement.

* Allow expand on column in index 0. Prevent focus from moving to next column if collapsed.

* Shorten var.

* Update code comment.

* Make sure focus can go back to row index 0 if coming from above Options button.

* Fix lint error.

* Try to fix bugs.

* Check that activeRow contains aria-expanded vs. checking the column index. Update comments.

* Try to make code cleaner. Update the Changelog.

* Revert some files.

* Fix Changelog.

* Update packages/components/src/tree-grid/index.js

Improvement for more than 2 <td> elements.

Co-authored-by: Daniel Richards <daniel.richards@automattic.com>

* Fix formatting error.

Co-authored-by: Daniel Richards <daniel.richards@automattic.com>
  • Loading branch information
alexstine and talldan authored Feb 8, 2022
1 parent 29d123b commit 271703c
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const ListViewBlockContents = forwardRef(
position,
siblingBlockCount,
level,
isExpanded,
...props
},
ref
Expand Down Expand Up @@ -71,6 +72,7 @@ const ListViewBlockContents = forwardRef(
draggable={ draggable }
onDragStart={ onDragStart }
onDragEnd={ onDragEnd }
isExpanded={ isExpanded }
{ ...props }
/>
) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ function ListViewBlockSelectButton(
onDragStart,
onDragEnd,
draggable,
isExpanded,
},
ref
) {
Expand Down Expand Up @@ -81,6 +82,7 @@ function ListViewBlockSelectButton(
onDragEnd={ onDragEnd }
draggable={ draggable }
href={ `#block-${ clientId }` }
aria-expanded={ isExpanded }
>
<ListViewExpander onClick={ onToggleExpanded } />
<BlockIcon icon={ blockInformation?.icon } showColors />
Expand Down
1 change: 1 addition & 0 deletions packages/block-editor/src/components/list-view/block.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,7 @@ function ListViewBlock( {
ref={ ref }
tabIndex={ tabIndex }
onFocus={ onFocus }
isExpanded={ isExpanded }
/>
</div>
) }
Expand Down
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
### Enhancements

- Update the visual design of the `Spinner` component. ([#37551](https://github.com/WordPress/gutenberg/pull/37551))
- TreeGrid accessibility enhancements. (#38358)

## 19.3.0 (2022-01-27)

Expand Down
14 changes: 11 additions & 3 deletions packages/components/src/tree-grid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ function TreeGrid(
const activeRow = activeElement.closest( '[role="row"]' );
const focusablesInRow = getRowFocusables( activeRow );
const currentColumnIndex = focusablesInRow.indexOf( activeElement );
const canExpandCollapse = 0 === currentColumnIndex;
const cannotFocusNextColumn =
canExpandCollapse &&
activeRow.getAttribute( 'aria-expanded' ) === 'false' &&
keyCode === RIGHT;

if ( includes( [ LEFT, RIGHT ], keyCode ) ) {
// Calculate to the next element.
Expand All @@ -91,8 +96,8 @@ function TreeGrid(
);
}

// Focus is either at the left or right edge of the grid.
if ( nextIndex === currentColumnIndex ) {
// Focus is at the left most column.
if ( canExpandCollapse ) {
if ( keyCode === LEFT ) {
// Left:
// If a row is focused, and it is expanded, collapses the current row.
Expand Down Expand Up @@ -149,7 +154,10 @@ function TreeGrid(
return;
}

// Focus the next element.
// Focus the next element. If at most left column and row is collapsed, moving right is not allowed as this will expand. However, if row is collapsed, moving left is allowed.
if ( cannotFocusNextColumn ) {
return;
}
focusablesInRow[ nextIndex ].focus();

// Prevent key use for anything else. This ensures Voiceover
Expand Down

0 comments on commit 271703c

Please sign in to comment.