Skip to content

Commit

Permalink
useNavigateRegions: Use closest to determine the next region to navig…
Browse files Browse the repository at this point in the history
…ate to (#44883)

* Store current region in state so region navigation remains in-order.

* Remove constant tabindex from regions. Add back when needed. Try to find better focus locations.

* Use closest to determine the current region since it operates in a DOM upwards motion. From the current element, I should get the closest wrapping region.

* Better code comment.

* Revert focus/tabindex changes.

* Try fixing E2E tests.

* Add changelog entry.

* Fix edge case with selector matching regions without our negative tabindex.

* Try again to only select regions with tabindex of -1. Fix E2E.

* Fix E2E.

* Fix E2E.

* Fix E2E.
  • Loading branch information
alexstine authored Dec 25, 2022
1 parent 96a8df7 commit 6051295
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 10 deletions.
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 @@
- `Navigator`: Allow calling `goTo` and `goBack` twice in one render cycle ([#46391](https://github.com/WordPress/gutenberg/pull/46391)).
- `Modal`: Fix unexpected modal closing in IME Composition ([#46453](https://github.com/WordPress/gutenberg/pull/46453)).
- `Toolbar`: Fix duplicate focus style on anchor link button ([#46759](https://github.com/WordPress/gutenberg/pull/46759)).
- `useNavigateRegions`: Ensure region navigation picks the next region based on where the current user focus is located instead of starting at the beginning ([#44883](https://github.com/WordPress/gutenberg/pull/44883)).

### Enhancements

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,17 @@ export function useNavigateRegions( shortcuts = defaultShortcuts ) {

function focusRegion( offset ) {
const regions = Array.from(
ref.current.querySelectorAll( '[role="region"]' )
ref.current.querySelectorAll( '[role="region"][tabindex="-1"]' )
);
if ( ! regions.length ) {
return;
}
let nextRegion = regions[ 0 ];
// Based off the current element, use closest to determine the wrapping region since this operates up the DOM. Also, match tabindex to avoid edge cases with regions we do not want.
const selectedIndex = regions.indexOf(
ref.current.ownerDocument.activeElement
ref.current.ownerDocument.activeElement.closest(
'[role="region"][tabindex="-1"]'
)
);
if ( selectedIndex !== -1 ) {
let nextIndex = selectedIndex + offset;
Expand Down
4 changes: 3 additions & 1 deletion packages/e2e-tests/specs/editor/various/a11y.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ describe( 'a11y', () => {

it( 'tabs header bar', async () => {
await pressKeyWithModifier( 'ctrl', '`' );

await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrl', '`' );
await page.keyboard.press( 'Tab' );

const isFocusedToggle = await page.$eval(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,16 +111,14 @@ describe( 'Navigating the block hierarchy', () => {

// Move focus to the sidebar area.
await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrl', '`' );
await tabToColumnsControl();

// Tweak the columns count by increasing it by one.
await page.keyboard.press( 'ArrowRight' );

// Navigate to the third column in the columns block.
await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrlShift', '`' );
await pressKeyWithModifier( 'ctrlShift', '`' );
await pressKeyTimes( 'Tab', 4 );
await pressKeyTimes( 'ArrowDown', 4 );
await page.waitForSelector(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ const navigateToContentEditorTop = async () => {
// Use 'Ctrl+`' to return to the top of the editor.
await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrl', '`' );
};

const tabThroughParagraphBlock = async ( paragraphText ) => {
Expand All @@ -36,6 +39,10 @@ const tabThroughParagraphBlock = async ( paragraphText ) => {

await page.keyboard.press( 'Tab' );
await expect( await getActiveLabel() ).toBe( 'Open document settings' );

// Need to shift+tab here to end back in the block. If not, we'll be in the next region and it will only require 4 region jumps instead of 5.
await pressKeyWithModifier( 'shift', 'Tab' );
await expect( await getActiveLabel() ).toBe( 'Paragraph block' );
};

const tabThroughBlockToolbar = async () => {
Expand Down
2 changes: 0 additions & 2 deletions packages/e2e-tests/specs/editor/various/sidebar.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,6 @@ describe( 'Sidebar', () => {

// Region navigate to Sidebar.
await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrl', '`' );
await pressKeyWithModifier( 'ctrl', '`' );

// Tab lands at first (presumed selected) option "Post".
await page.keyboard.press( 'Tab' );
Expand Down
5 changes: 4 additions & 1 deletion test/e2e/specs/editor/various/a11y-region-navigation.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ test.describe( 'Region navigation (@firefox, @webkit)', () => {
attributes: { content: 'Dummy text' },
} );

// Navigate to first region and check that we made it.
// Navigate to first region and check that we made it. Must navigate forward 4 times as initial focus is placed in post title field.
await page.keyboard.press( 'Control+`' );
await page.keyboard.press( 'Control+`' );
await page.keyboard.press( 'Control+`' );
await page.keyboard.press( 'Control+`' );
const editorTopBar = page.locator(
'role=region[name="Editor top bar"i]'
Expand Down

1 comment on commit 6051295

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/3777635970
📝 Reported issues:

Please sign in to comment.