Fix: Title Generation: toolbar button disappears after toggling "Show template" off (normal editing mode), until reload#694
Merged
Conversation
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
6 tasks
dkotter
approved these changes
Jun 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What?
Closes #693
Why?
How?
Use of AI Tools
Testing Instructions
Screenshots or screencast
Screen.Recording.2026-06-10.at.1.23.34.PM.mov
Changelog Entry
AI Summary
Root cause
The Title Generation experiment attaches its "Generate"/"Regenerate" toolbar in normal editing mode via DOM injection in
TitleToolbarWrapper.tsx(aregisterPluginrender that manipulates the editor DOM directly, rather than rendering React into the editor tree).Two pieces of logic made the attachment a one-time, irreversible operation:
MutationObserverpermanently disconnected after the first attach. OnceisAttachedbecametrue, the observer callback calledobs.disconnect()and stopped watching the editor DOM entirely.findAndAttachToolbar()early-returns whileisAttachedistrue. Even if something re-triggered it, the guard at the top would bail out.When the user toggles Show template on, the editor swaps the post title region — the normal-mode
.editor-post-title__inputis replaced by thecore/post-titleblock view. Toggling it back off recreates a fresh.editor-post-title__input, and React discards the old subtree, taking our injected.ai-title-toolbar-wrapperwith it.At that point the toolbar is gone from the DOM, but:
isAttachedis stilltrue, andso nothing re-injects the toolbar onto the new title input. The component never unmounts during the toggle, so the effect's cleanup/re-run path never fires either. Only a full editor reload re-runs the
useEffectand re-attaches — which is why a refresh "fixes" it.What we fixed
In
src/experiments/title-generation/components/TitleToolbarWrapper.tsx:MutationObserverconnected for the editor's lifetime. Removed theobs.disconnect()branch so the observer keeps watching after the first attach. (It still disconnects in the effect's cleanup on real unmount.).ai-title-toolbar-wrapperstill exists in the editor document. If we believe we're attached but the wrapper is gone, we reset our state and re-attach to the newly rendered title input.resetAttachmentState()helper that cleanly tears down the stale attachment before re-attaching — unmounts the React root, removes the title/toolbar focus-blur listeners, removes the orphaned wrapper node, and clears theisAttached/reference state. This prevents leaked React roots and dangling event listeners across re-attachments.The re-attach is safe from infinite loops: inserting the wrapper triggers the observer again, but on that fire both
isAttachedandwrapperExistsaretrue, so neither the reset nor the attach branch runs. The existing retry logic infindAndAttachToolbar()handles the brief window where the new title input hasn't rendered yet.Result: toggling "Show template" off (or any editor interaction that recreates the title input) now re-injects the toolbar immediately, without requiring a page refresh.