Skip to content

Commit

Permalink
Focus first focusable on Link UI (#51105)
Browse files Browse the repository at this point in the history
* Focus first focusable on Link UI

* Fix “should preserve trailing/leading whitespace from linked text in text input” test

* Fix “should allow for modification of link text via Link UI” test

---------

Co-authored-by: Dave Smith <getdavemail@gmail.com>
  • Loading branch information
jeryj and getdave authored Jun 9, 2023
1 parent e41ad64 commit b7432c8
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 11 deletions.
11 changes: 2 additions & 9 deletions packages/block-editor/src/components/link-control/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -181,22 +181,15 @@ function LinkControl( {
isMounting.current = false;
return;
}
// Unless we are mounting, we always want to focus either:
// - the URL input
// - the first focusable element in the Link UI.
// But in editing mode if there is a text input present then
// the URL input is at index 1. If not then it is at index 0.
const whichFocusTargetIndex = textInputRef?.current ? 1 : 0;

// Scenario - when:
// - switching between editable and non editable LinkControl
// - clicking on a link
// ...then move focus to the *first* element to avoid focus loss
// and to ensure focus is *within* the Link UI.
const nextFocusTarget =
focus.focusable.find( wrapperNode.current )[
whichFocusTargetIndex
] || wrapperNode.current;
focus.focusable.find( wrapperNode.current )[ 0 ] ||
wrapperNode.current;

nextFocusTarget.focus();

Expand Down
8 changes: 6 additions & 2 deletions packages/e2e-tests/specs/editor/various/links.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -585,7 +585,9 @@ describe( 'Links', () => {

await editButton.click();

// Tabbing back should land us in the text input.
// Tabbing forward should land us in the "Text" input.
await page.keyboard.press( 'Tab' );

const textInputValue = await page.evaluate(
() => document.activeElement.value
);
Expand All @@ -612,7 +614,9 @@ describe( 'Links', () => {
);
await editButton.click();

// Tabbing should land us in the text input.
// tab forward to the text input.
await page.keyboard.press( 'Tab' );

const textInputValue = await page.evaluate(
() => document.activeElement.value
);
Expand Down

1 comment on commit b7432c8

@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 in b7432c8.
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/5221984652
📝 Reported issues:

Please sign in to comment.