Skip to content

RichText: Fix persisting format boundary background color - #86070

Closed
Xmerlin7 wants to merge 1 commit into
WordPress:trunkfrom
Xmerlin7:fix/rich-text-boundary-leak
Closed

Xmerlin7 wants to merge 1 commit into
WordPress:trunkfrom
Xmerlin7:fix/rich-text-boundary-leak

Conversation

@Xmerlin7

@Xmerlin7 Xmerlin7 commented Oct 30, 2026 •

Copy link
Copy Markdown

What?

Closes #86064

This PR fixes a bug where the gray background color used to highlight rich text formatting boundaries (like italics) persists on the screen even after the user moves focus to a different block.

Why?

Currently, the useBoundaryStyle hook dynamically injects a <style id="rich-text-boundary-style"> tag into the document head to render the boundary color based on the current text color. However, the useEffect lacked a cleanup function. When a user clicked away and activeFormats became empty, the hook returned early but left the generated <style> tag in the DOM.

With the recent focus management architectural changes in WP 7.2 (where the editor canvas retains a global contenteditable="true" state), the native :focus selector in that orphaned stylesheet continued to match, causing the gray background to persist.

How?

I added a cleanup function to the useEffect within packages/rich-text/src/hook/use-boundary-style.ts. When the component unmounts or the active formats are cleared, the cleanup function checks the globalStyle element and safely empties its innerHTML, correctly removing the CSS rule from the DOM.

Testing Instructions

  1. Open a post or page in the block editor.
  2. Add a Paragraph block, type some text, and italicize a portion of it.
  3. Add a Heading block above or below the paragraph.
  4. Place your cursor inside the italicized text in the paragraph block so the gray formatting boundary background appears.
  5. Click directly into the Heading block.
  6. Observe the italicized text in the paragraph block. The gray background color should immediately disappear.

Testing Instructions for Keyboard

  1. Navigate into the paragraph block using Tab and Enter.
  2. Highlight a word using Shift + Arrow Keys and italicize it using Cmd/Ctrl + I.
  3. Press the Up or Down arrow keys to move the cursor focus out of the paragraph and into an adjacent block.
  4. Verify that the formatting boundary background color is successfully removed.

Screenshots or screencast

Before After
(The gray background remains visible when focus moves to another block) (The gray background vanishes cleanly when focus moves)
Bug1 Bug2

Use of AI Tools

I used an AI assistant as a pair-programming partner to help locate the specific useBoundaryStyle hook in the codebase using grep, and to assist in analyzing the React useEffect lifecycle to determine where the cleanup function needed to be injected. I manually reviewed, tested, and verified the final code changes locally.

@Xmerlin7
Xmerlin7 requested a review from ellatrix as a code owner October 30, 2026 19:11
@github-actions github-actions Bot added the [Package] Rich text /packages/rich-text label Oct 30, 2026
@github-actions

github-actions Bot commented Oct 30, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground

👋 Welcome

👋 Thanks for your first Pull Request and for helping build the future of Gutenberg and WordPress, @Xmerlin7! In case you missed it, we'd love to have you join us in our Slack community.

If you want to learn more about WordPress development in general, check out the Core Handbook full of helpful information.

🎉 Props

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @Xmerlin7.

Contributors, please read how to link your accounts to ensure your work is properly credited in WordPress releases.

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

Unlinked contributors: Xmerlin7.

Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: andreawetzel <awetz583@git.wordpress.org>

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

@github-actions github-actions Bot added the First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository label Oct 30, 2026
@Xmerlin7

Copy link
Copy Markdown
Author

Hello! Could a maintainer please add the [Type] Bug label for me?
@ellatrix @t-hamano

@Mamaduka Mamaduka added the [Type] Bug An existing feature does not function as intended label Nov 2, 2026
@Mamaduka

Mamaduka commented Nov 2, 2026

Copy link
Copy Markdown
Member

Thanks for the PR, @Xmerlin7!

Clearing the stylesheet fixes the repro steps, but the root cause is a stale data-rich-text-format-boundary attribute left in the deselected field's DOM. If the caret moves into formatted text in the heading, the shared rule comes back and highlights the old field again.

Created a PR with an alternative fix: #86168.

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

Labels

First-time Contributor Pull request opened by a first-time contributor to Gutenberg repository [Package] Rich text /packages/rich-text [Type] Bug An existing feature does not function as intended

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Italicized text remains highlighted when paragraph is no longer selected in 7.2-beta3

2 participants