Skip to content

fix: discard unsaved HTML block changes on cancel#78580

Merged
t-hamano merged 2 commits into
WordPress:trunkfrom
CookieDarb:fix/78548-html-block-modal-cancel-discard
May 25, 2026
Merged

fix: discard unsaved HTML block changes on cancel#78580
t-hamano merged 2 commits into
WordPress:trunkfrom
CookieDarb:fix/78548-html-block-modal-cancel-discard

Conversation

@CookieDarb
Copy link
Copy Markdown
Contributor

What?

Closes #78548

This PR conditionally renders the Custom HTML block's edit modal (HTMLEditModal) so that it is unmounted when closed.

Why?

Previously, the modal component remained mounted in the React tree even when closed, simply returning null internally. This caused React to retain the local state variables (editedHtml, editedCss, editedJs) representing the user's edits.

As a result, if a user modified the code in the modal, clicked "Cancel" to discard their changes, and reopened the modal, the modal still displayed their discarded changes.

How?

Updated packages/block-library/src/html/edit.js to conditionally render <HTMLEditModal> using the state flag isModalOpen: { isModalOpen && <HTMLEditModal ... /> }
This is done for both occurrences (the placeholder block view and the main edit block view).

Testing Instructions

  1. In the Gutenberg editor, insert a Custom HTML block.
  2. Click Edit code to open the code editing modal.
  3. Type some HTML code
  4. Click Cancel in the bottom-right corner of the modal.
  5. Click Edit code again to reopen the modal.
  6. Verify that the changes you typed in step 3 are discarded, and the modal shows the original unedited block content.

Testing Instructions for Keyboard

Screenshots or screencast

Before

Custom-HTML-Block-Before.mov

After

Custom-HTML-Block-After.mov

Use of AI Tools

AI assistance: Yes
Tool(s): Antigravity
Model(s): Gemini 3.5 Flash
Used for: Suggesting and implementing the fix. Final implementation was reviewed and edited by me.

@github-actions github-actions Bot added the [Package] Block library /packages/block-library label May 22, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 22, 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.

Unlinked Accounts

The following contributors have not linked their GitHub and WordPress.org accounts: @at-ric.

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: at-ric.

Co-authored-by: CookieDarb <[email protected]>
Co-authored-by: t-hamano <[email protected]>

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

Copy link
Copy Markdown
Contributor

@t-hamano t-hamano left a comment

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 looks good at first glance, but can we remove the isOpen prop from the HTMLEditModal component?

@t-hamano t-hamano added [Block] HTML Affects the the HTML Block Backport to WP Minor Release Pull request that needs to be backported to a WordPress minor release labels May 23, 2026
@github-project-automation github-project-automation Bot moved this to 🔎 Needs Review in WordPress 7.0 Editor Tasks May 23, 2026
@CookieDarb CookieDarb force-pushed the fix/78548-html-block-modal-cancel-discard branch from 5daf2a2 to 499e35f Compare May 23, 2026 14:23
@CookieDarb
Copy link
Copy Markdown
Contributor Author

Done! I've removed the redundant isOpen prop from the HTMLEditModal component.

@CookieDarb CookieDarb requested a review from t-hamano May 23, 2026 14:59
@t-hamano t-hamano added the [Type] Bug An existing feature does not function as intended label May 25, 2026
Copy link
Copy Markdown
Contributor

@t-hamano t-hamano left a comment

Choose a reason for hiding this comment

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

LGTM 👍

@t-hamano t-hamano merged commit 583eeb1 into WordPress:trunk May 25, 2026
44 of 45 checks passed
@github-project-automation github-project-automation Bot moved this from 🔎 Needs Review to ✅ Done in WordPress 7.0 Editor Tasks May 25, 2026
@github-actions github-actions Bot added this to the Gutenberg 23.3 milestone May 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Backport to WP Minor Release Pull request that needs to be backported to a WordPress minor release [Block] HTML Affects the the HTML Block [Package] Block library /packages/block-library [Type] Bug An existing feature does not function as intended

Projects

Development

Successfully merging this pull request may close these issues.

Custom HTML block - Discard changes won´t bring back unedited code

2 participants