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

TreeGrid: Add Home/End keys to jump to start/end of grid #38679

Merged
merged 2 commits into from
Feb 10, 2022
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
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
- Update the visual design of the `Spinner` component. ([#37551](https://github.com/WordPress/gutenberg/pull/37551))
- `TreeGrid` accessibility enhancements around the expand/collapse functionality. ([#38358](https://github.com/WordPress/gutenberg/pull/38358))
- `TreeGrid` accessibility: improve browser support for Left Arrow focus to parent row in child row. ([#38639](https://github.com/WordPress/gutenberg/pull/38639))
- `TreeGrid` accessibility: Add Home/End keys for better keyboard navigation. ([#38679](https://github.com/WordPress/gutenberg/pull/38679))

### Experimental

Expand Down
51 changes: 49 additions & 2 deletions packages/components/src/tree-grid/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { includes } from 'lodash';
*/
import { focus } from '@wordpress/dom';
import { forwardRef, useCallback } from '@wordpress/element';
import { UP, DOWN, LEFT, RIGHT } from '@wordpress/keycodes';
import { UP, DOWN, LEFT, RIGHT, HOME, END } from '@wordpress/keycodes';

/**
* Internal dependencies
Expand Down Expand Up @@ -60,7 +60,7 @@ function TreeGrid(

if (
hasModifierKeyPressed ||
! includes( [ UP, DOWN, LEFT, RIGHT ], keyCode )
! includes( [ UP, DOWN, LEFT, RIGHT, HOME, END ], keyCode )
) {
return;
}
Expand Down Expand Up @@ -216,6 +216,53 @@ function TreeGrid(
);
focusablesInNextRow[ nextIndex ].focus();

// Prevent key use for anything else. This ensures Voiceover
// doesn't try to handle key navigation.
event.preventDefault();
} else if ( includes( [ HOME, END ], keyCode ) ) {
// Calculate the rowIndex of the next row.
const rows = Array.from(
treeGridElement.querySelectorAll( '[role="row"]' )
);
const currentRowIndex = rows.indexOf( activeRow );
let nextRowIndex;

if ( keyCode === HOME ) {
nextRowIndex = 0;
} else {
nextRowIndex = rows.length - 1;
}

// Focus is either at the top or bottom edge of the grid. Do nothing.
if ( nextRowIndex === currentRowIndex ) {
// Prevent key use for anything else. For example, Voiceover
// will start navigating horizontally when reaching the vertical
// bounds of a table.
event.preventDefault();
return;
}

// Get the focusables in the next row.
const focusablesInNextRow = getRowFocusables(
rows[ nextRowIndex ]
);

// If for some reason there are no focusables in the next row, do nothing.
if ( ! focusablesInNextRow || ! focusablesInNextRow.length ) {
// Prevent key use for anything else. For example, Voiceover
// will still focus text when using arrow keys, while this
// component should limit navigation to focusables.
event.preventDefault();
return;
}

// Try to focus the element in the next row that's at a similar column to the activeElement.
const nextIndex = Math.min(
currentColumnIndex,
focusablesInNextRow.length - 1
);
focusablesInNextRow[ nextIndex ].focus();

// Prevent key use for anything else. This ensures Voiceover
// doesn't try to handle key navigation.
event.preventDefault();
Expand Down