Skip to content

Commit

Permalink
Focus first focusable on Link UI
Browse files Browse the repository at this point in the history
  • Loading branch information
jeryj committed May 30, 2023
1 parent 2bb724a commit 82a3862
Showing 1 changed file with 2 additions and 9 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

0 comments on commit 82a3862

Please sign in to comment.