Skip to content

Navigation: Prevent duplicate blocks from scrolling into view after updates - #79097

Closed
Mustafabharmal wants to merge 2 commits into
WordPress:trunkfrom
Mustafabharmal:fix/navigation-block-selection-stealing
Closed

Mustafabharmal wants to merge 2 commits into
WordPress:trunkfrom
Mustafabharmal:fix/navigation-block-selection-stealing

Conversation

@Mustafabharmal

Copy link
Copy Markdown
Contributor

What?

Closes #79096

Prevents duplicate Navigation block instances from taking over the editor selection when they reference the same navigation entity.

Previously, editing one Navigation block could cause another Navigation block using the same menu to become selected and scroll into view unexpectedly.

Why?

Navigation blocks that reference the same menu share a single wp_navigation entity.

When one instance updates the shared entity, other instances synchronize their internal blocks through useBlockSync. During this synchronization, duplicate instances can restore the shared selection state and incorrectly assume ownership of the selection.

As a result:

  • The active selection moves to another Navigation block.
  • The editor scrolls unexpectedly.
  • The user's editing context is interrupted.

How?

Update the selection restoration logic in useBlockSync to avoid restoring selection when the editor is already actively selecting a valid block.

This ensures that duplicate block controllers do not take over selection ownership when synchronizing shared entity updates.

Selection restoration still occurs when necessary, such as when the previously selected block has been removed during synchronization.

Testing Instructions

  1. Open the Site Editor.
  2. Insert a Navigation block and create or select a menu.
  3. Duplicate the Navigation block so both instances reference the same menu.
  4. Place the duplicate far enough away that it is outside the viewport.
  5. Select the first Navigation block.
  6. Add a new menu item (for example, using "Add page").
  7. Verify the first Navigation block remains selected.
  8. Verify the editor does not scroll to the duplicate Navigation block.

Screenshots or screencast

Before:

  • Editing one Navigation block can cause another instance to become selected and scroll into view.
Before.Nav.Fix.mov

After:

  • Selection remains on the Navigation block being edited and no unexpected scrolling occurs.
After.Nav.Fix.mov

@Mustafabharmal
Mustafabharmal requested a review from ellatrix as a code owner June 10, 2026 19:56
@github-actions

Copy link
Copy Markdown

Warning: Type of PR label mismatch

To merge this PR, it requires exactly 1 label indicating the type of PR. Other labels are optional and not being checked here.

  • Required label: Any label starting with [Type].
  • Labels found: .

Read more about Type labels in Gutenberg. Don't worry if you don't have the required permissions to add labels; the PR reviewer should be able to help with the task.

@github-actions

github-actions Bot commented Jun 10, 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: Mustafabharmal <mustafabharmal@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Co-authored-by: youknowriad <youknowriad@git.wordpress.org>
Co-authored-by: talldan <talldanwp@git.wordpress.org>
Co-authored-by: jeryj <jeryj@git.wordpress.org>

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

Comment on lines +212 to +215
getBlockName( currentSelectionStartId )
) {
return;
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for the PR! It'd be great to have some test coverage for this. I suppose in test/e2e/specs/editor/various/post-content-focus-mode.spec.js would be appropriate.

Namely to cover the core bug case, but also other focus "stealers" like undo/redo. See comment here: #79096 (comment)

@youknowriad

Copy link
Copy Markdown
Contributor

Update the selection restoration logic in useBlockSync to avoid restoring selection when the editor is already actively selecting a valid block.

When you say "valid block" here, what does it mean exactly? I'm wondering if this can introduce an issue for things like undo/redo or entity navigation... when the editor has a valid block selected but it's not the right one that should be selected after the action.

jeryj added a commit that referenced this pull request Oct 15, 2026
Fixes #79096.

Two blocks can render the same entity — two Navigation blocks using one
menu, a Post Content block at a template root and another inside a
template part. Each is an inner block controller holding private clones,
but both clone the same blocks, so both build the same
external-to-internal ID mapping and both recognise the entity's
selection as their own.

An edit in one instance therefore reaches the other as an incoming
entity change. That controller re-clones and calls restoreSelection,
which converts the external ID to its own copy and dispatches
resetSelection. The canvas scrolled to the duplicate and the caret left
the block being typed in, reliably enough to reorder characters.

Guard the restoreSelection call instead: when the selection already sits
inside a different inner block controller, this sync is a consequence of
an edit made there, so leave it alone. The controller that made the edit
takes the outgoing branch and never reaches this code, so a selection
held by another controller is always one that has been dealt with. A
selection held by the root block list belongs to no controller and still
restores as before, which is what keeps entity navigation and undo
working.

The guard sits at the call site rather than inside restoreSelection: the
question is about the sync that just happened, not about the selection
object, and restoreSelection keeps its existing contract.

This reuses getBlockParents and areInnerBlocksControlled, already
selected in this hook, so there is no new store state, no new action or
selector, and nothing persisted into entity data or collaborative state.

Revives the approach in #79422 by @ramonjd, which asked the same question
with the same selectors and was closed by its author in favour of #79097.
Two differences: the check moved to the call site, and the WeakMap
remembering the last controller to hold the selection is left out.

Also removes two tests added with the failing coverage. They asserted
that undo returns the caret to the block whose edit was undone. Trunk
does not do that for any nested entity, with or without duplicates: a
controller records its selection against its own entity, but the only
consumer of a recorded selection reads the root record. That is a
separate, pre-existing gap and out of scope here.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
jeryj added a commit that referenced this pull request Nov 24, 2026
Fixes #79096.

Two blocks can render the same entity — two Navigation blocks using one
menu, a Post Content block at a template root and another inside a
template part. Each is an inner block controller holding private clones,
but both clone the same blocks, so both build the same
external-to-internal ID mapping and both recognise the entity's
selection as their own.

An edit in one instance therefore reaches the other as an incoming
entity change. That controller re-clones and calls restoreSelection,
which converts the external ID to its own copy and dispatches
resetSelection. The canvas scrolled to the duplicate and the caret left
the block being typed in, reliably enough to reorder characters.

Guard the restoreSelection call instead: when the selection already sits
inside a different inner block controller, this sync is a consequence of
an edit made there, so leave it alone. The controller that made the edit
takes the outgoing branch and never reaches this code, so a selection
held by another controller is always one that has been dealt with. A
selection held by the root block list belongs to no controller and still
restores as before, which is what keeps entity navigation and undo
working.

The guard sits at the call site rather than inside restoreSelection: the
question is about the sync that just happened, not about the selection
object, and restoreSelection keeps its existing contract.

This reuses getBlockParents and areInnerBlocksControlled, already
selected in this hook, so there is no new store state, no new action or
selector, and nothing persisted into entity data or collaborative state.

Revives the approach in #79422 by @ramonjd, which asked the same question
with the same selectors and was closed by its author in favour of #79097.
Two differences: the check moved to the call site, and the WeakMap
remembering the last controller to hold the selection is left out.

Also removes two tests added with the failing coverage. They asserted
that undo returns the caret to the block whose edit was undone. Trunk
does not do that for any nested entity, with or without duplicates: a
controller records its selection against its own entity, but the only
consumer of a recorded selection reads the root record. That is a
separate, pre-existing gap and out of scope here.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
jeryj added a commit that referenced this pull request Nov 24, 2026
Fixes #79096.

Two blocks can render the same entity — two Navigation blocks using one
menu, a Post Content block at a template root and another inside a
template part. Each is an inner block controller holding private clones,
but both clone the same blocks, so both build the same
external-to-internal ID mapping and both recognise the entity's
selection as their own.

An edit in one instance therefore reaches the other as an incoming
entity change. That controller re-clones and calls restoreSelection,
which converts the external ID to its own copy and dispatches
resetSelection. The canvas scrolled to the duplicate and the caret left
the block being typed in, reliably enough to reorder characters.

Guard the restoreSelection call instead: when the selection already sits
inside a different inner block controller, this sync is a consequence of
an edit made there, so leave it alone. The controller that made the edit
takes the outgoing branch and never reaches this code, so a selection
held by another controller is always one that has been dealt with. A
selection held by the root block list belongs to no controller and still
restores as before, which is what keeps entity navigation and undo
working.

The guard sits at the call site rather than inside restoreSelection: the
question is about the sync that just happened, not about the selection
object, and restoreSelection keeps its existing contract.

This reuses getBlockParents and areInnerBlocksControlled, already
selected in this hook, so there is no new store state, no new action or
selector, and nothing persisted into entity data or collaborative state.

Revives the approach in #79422 by @ramonjd, which asked the same question
with the same selectors and was closed by its author in favour of #79097.
Two differences: the check moved to the call site, and the WeakMap
remembering the last controller to hold the selection is left out.

Also removes two tests added with the failing coverage. They asserted
that undo returns the caret to the block whose edit was undone. Trunk
does not do that for any nested entity, with or without duplicates: a
controller records its selection against its own entity, but the only
consumer of a recorded selection reads the root record. That is a
separate, pre-existing gap and out of scope here.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
jeryj added a commit that referenced this pull request Dec 1, 2026
Fixes #79096.

Two blocks can render the same entity — two Navigation blocks using one
menu, a Post Content block at a template root and another inside a
template part. Each is an inner block controller holding private clones,
but both clone the same blocks, so both build the same
external-to-internal ID mapping and both recognise the entity's
selection as their own.

An edit in one instance therefore reaches the other as an incoming
entity change. That controller re-clones and calls restoreSelection,
which converts the external ID to its own copy and dispatches
resetSelection. The canvas scrolled to the duplicate and the caret left
the block being typed in, reliably enough to reorder characters.

Guard the restoreSelection call instead: when the selection already sits
inside a different inner block controller, this sync is a consequence of
an edit made there, so leave it alone. The controller that made the edit
takes the outgoing branch and never reaches this code, so a selection
held by another controller is always one that has been dealt with. A
selection held by the root block list belongs to no controller and still
restores as before, which is what keeps entity navigation and undo
working.

The guard sits at the call site rather than inside restoreSelection: the
question is about the sync that just happened, not about the selection
object, and restoreSelection keeps its existing contract.

This reuses getBlockParents and areInnerBlocksControlled, already
selected in this hook, so there is no new store state, no new action or
selector, and nothing persisted into entity data or collaborative state.

Revives the approach in #79422 by @ramonjd, which asked the same question
with the same selectors and was closed by its author in favour of #79097.
Two differences: the check moved to the call site, and the WeakMap
remembering the last controller to hold the selection is left out.

Also removes two tests added with the failing coverage. They asserted
that undo returns the caret to the block whose edit was undone. Trunk
does not do that for any nested entity, with or without duplicates: a
controller records its selection against its own entity, but the only
consumer of a recorded selection reads the root record. That is a
separate, pre-existing gap and out of scope here.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017RtcwXT84eF6zLdQXK9e1W
@jeryj

jeryj commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Thanks for the PR! This is fixed now that #82706 is merged.

@jeryj jeryj closed this Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Package] Block editor /packages/block-editor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adding an item to a duplicate Navigation Block scrolls the second one into view

4 participants