useCopyToClipboard: Always call onSuccess callback#78387
Conversation
The native clipboard rewrite made the copy async, so when the trigger node unmounts as a side effect of the same click (e.g. a menu item that closes its menu), the `isActive` guard suppressed the `onSuccess` callback. Always run `onSuccess` on success and only guard focus restoration with `isActive`. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
Extend the unmount test for useCopyToClipboard to also assert that focus restoration (clearSelection) is skipped once the trigger node has unmounted, covering the other half of the isActive guard. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
useCopyToClipboard onSuccess even when the trigger unmounts on click|
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. |
|
This PR fixes a regression that occurs in the 7.0 release, so it would be good to backport it to at least a minor release. |
|
Size Change: -22 B (0%) Total Size: 7.97 MB 📦 View Changed
ℹ️ View Unchanged
|
|
Flaky tests detected in bba4cbd. 🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/26025527828
|
ciampo
left a comment
There was a problem hiding this comment.
Nice, thank you for working on this!
Co-authored-by: Marco Ciampini <[email protected]>
Each test now verifies a single behavior, making failures easier to diagnose. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
…restoreFocus removeAllRanges() never cleared anything in the current implementation: the Clipboard API path creates no document selection, and the textarea fallback uses a field selection (outside the Selection API) on an element that is removed before this runs. Drop it, and rename the function to restoreFocus to reflect what it actually does. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
What?
Follow up to #75723.
This calls the
onSuccesscallback ofuseCopyToClipboardeven when the trigger node unmounts before the asynchronous copy resolves.Why?
#75723 reimplemented
useCopyToClipboardon top of the asynchronous native Clipboard API. To avoid acting on an unmounted node when the promise resolves, anisActiveguard was added that early-returns after unmount.However, there are use cases where the element that triggered the copy disappears immediately. — for example, a
MenuIteminside aDropdownMenuthat closes the menu on click. In that case the guard suppressesonSuccessentirely, so consumers can no longer show a "Copied!" notice.How?
Slightly relaxes the restrictions of the
isActiveguard. While maintaining focus restoration, this PR ensures that theonSuccesscallback is always executed.Testing Instructions
Make the following change beforehand. This code adds a copy button to the post editor's options menu that unmounts when clicked.
Screenshot
Recording.2026-05-18.161257.mp4
Use of AI Tools
This PR was authored with the assistance of Claude Code, including the investigation, the fix, and the unit test. All changes were reviewed by the author.