Skip to content

Fix Focus Loss on Tab/Shift+Tab After Changing Heading Level#77959

Open
dpmehta wants to merge 1 commit into
WordPress:trunkfrom
dpmehta:fix/heading-level-tab-navigation
Open

Fix Focus Loss on Tab/Shift+Tab After Changing Heading Level#77959
dpmehta wants to merge 1 commit into
WordPress:trunkfrom
dpmehta:fix/heading-level-tab-navigation

Conversation

@dpmehta
Copy link
Copy Markdown
Contributor

@dpmehta dpmehta commented May 5, 2026

What?

Closes #55745

This PR fixes a bug where keyboard focus is lost when navigating back to a Heading or Site Title block from the block toolbar using Tab or Escape, specifically after its heading level was changed.

Why?

When the heading level is changed (H2 -> H3), React completely unmounts the old element and mounts a new one with the updated tag. However, the internal focus state (lastFocus.current) still held a reference to the old, detached DOM element. Consequently, attempting to return focus to the block via .focus() failed silently, breaking keyboard accessibility.

How?

Added a .isConnected check for lastFocus.current. If the previously focused element is no longer connected to the DOM (i.e. it was replaced by a new heading tag), the logic falls back to querying the active block via its [data-block] attribute. This correctly targets the newly mounted element and reliably restores focus.

Testing Instructions

  1. Open the block editor.
  2. Add a Heading block.
  3. Open the block toolbar.
  4. Select a different heading level (e.g., change from H2 to H3).
  5. Ensure focus can correctly be restored to the block (see keyboard instructions below).

Testing Instructions for Keyboard

  1. Add a Heading block and type some text.
  2. Press Shift + Tab to move focus from the block into the floating block toolbar.
  3. Use the keyboard to navigate to the "Change level" button and change the heading level.
  4. Press Tab to return focus to the block.
  5. Expected: Focus successfully returns to the Heading block.

Screenshots or screencast

Before Fix :

Screen.Recording.2023-10-31.at.3.49.36.PM.mov

After Fix :

after-fix.mov

Use of AI Tools

AI assitance used to draft PR description. all the changes are verified manually.

@github-actions github-actions Bot added the [Package] Block editor /packages/block-editor label May 5, 2026
@dpmehta dpmehta marked this pull request as ready for review May 5, 2026 14:23
@dpmehta dpmehta requested a review from ellatrix as a code owner May 5, 2026 14:23
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 5, 2026

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: dpmehta <mehtadev@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@t-hamano t-hamano added [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Type] Bug An existing feature does not function as intended [Feature] Writing Flow Block selection, navigation, splitting, merging, deletion... labels May 6, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Feature] Writing Flow Block selection, navigation, splitting, merging, deletion... [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] Block editor /packages/block-editor [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Changing Heading level loses Tab / Shift + Tab connection

2 participants