Skip to content

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

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

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

Conversation

@dpmehta

@dpmehta dpmehta commented May 5, 2026 •

Copy link
Copy Markdown
Contributor

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

github-actions Bot commented May 5, 2026

Copy link
Copy Markdown

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
@youknowriad

Copy link
Copy Markdown
Contributor

Thanks for digging into this — the isConnected diagnosis was right at the time. Since then #81319 rewrote the toolbar's Escape handler and #82354 reworked the focus-capture elements, and retesting the ported change against current trunk shows the guard alone no longer restores focus in either the Escape or the Tab flow — the problem has moved deeper into the focus-capture chain. Closing this PR since it needs a fresh approach against the current code rather than a rebase; #55745 stays open, and I've added updated reproduction notes there. A follow-up PR (ideally starting with an e2e test for the Tab-back-into-canvas case) would be very welcome.

@youknowriad youknowriad closed this Sep 3, 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

3 participants