Repository navigation
Gallery: Track new images in a ref instead of state - #86648
Conversation
🤖 PR meta 🤖🎉 PropsIf 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. Props Bot commandsYou can trigger Props Bot actions by commenting on this PR:
Updated as activity occurs, without notifying anyone named here. Add the 📦 Bundle sizeSize Change: +184 B (0%) Total Size: 8.64 MB 📦 View Changed
⚡ PerformanceShow the resultsClient side metrics exclude the server response time. front-end-block-theme
front-end-classic-theme
media-processing
media-upload
post-editor
site-editor
🏁 Flaky testsSome tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information. Undo restores the post selection when another synced entity is loaded in
|
971c11a to
6761b7d
Compare
6761b7d to
7a4d7f0
Compare
andrewserong
left a comment
There was a problem hiding this comment.
Thanks for tidying this up, this is smoke-testing well for me 👍
One question I had looking over the change is whether it'd be worth placing this new logic in a hook in a separate file? The edit.jsx for the gallery block is very long, and when I've been making changes to it recently, I've been trying to find places where we can split out logic (and possibly test it in isolation) to attempt to make the component a bit easier to reason about. Is that worth doing here?
Not a blocker at all, as on balance the logic here is quite small and a fair bit of this PR's diff is just renaming things.
LGTM!
ramonjd
left a comment
There was a problem hiding this comment.
Nice clean up.
I went through the test steps and the gallery is working as described. Nothing major stands out.
The settings swap regression I noticed is not a blocker.
| const handledImages = handledImagesRef.current; | ||
| const newImages = []; | ||
| innerBlockImages.forEach( ( block ) => { | ||
| if ( handledImages.has( block.clientId ) ) { |
There was a problem hiding this comment.
Before if I dragged an image from one gallery (A) to another (B), B's settings apply to that image (e.g., image size), then when you drag that image back to the original gallery A, it will receive A's settings.
2026-11-12.13.35.25.mp4
Now, moving an image from gallery A to gallery B and back to A now keeps B’s settings
2026-11-12.13.31.37.mp4
|
Thanks, @andrewserong! I've got a couple more PR ideas to clean up the edit component. That's what motivated my recent Gallery PRs, not just a I'll see what's the best way to push them. Would you prefer stacked PRs? I usually split work to make reviews easier, but I'm happy to take another approach. |
Drop useGetNewImages (setState in useMemo during render). One effect applies gallery settings to new images once their attachment loads, tracking handled client IDs in a ref. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
7a4d7f0 to
3d11ccc
Compare
Move the effect to useApplyGallerySettings. Forget images that left the gallery so ones moved back or restored by undo get the settings again, as before. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| "packages/block-library/src/gallery/use-apply-gallery-settings.ts": { | ||
| "no-restricted-syntax": { |
There was a problem hiding this comment.
This is just repositioning the existing useEffect, not a new suppression.
|
Going to merge this. The static analysis issue was fixed in #86720. |
Nice, thank you for digging in here! 🙇
Whatever works best for you! Personally I like PRs that don't depend on one another where we can, but also happy to review things in a stack if it helps your workflow. I typically lean toward not using a stack myself, just so that I'm not waiting on approvals of everything in the stack, and can merge work in small iterations. But as a reviewer, I don't mind either way 🙂 |
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org> Co-authored-by: andrewserong <andrewserong@git.wordpress.org> Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
What?
Removes
useGetNewImages, which calledsetStateinsideuseMemoduring render, and moves applying the gallery settings to new images into auseApplyGallerySettingshook that records handled images in a ref. No user-visible change.Why?
An image block uploads its own file and sets its own size and link when the upload finishes, so the gallery can only apply its settings once the attachment record loads, and no gallery handler sees that moment. Which images were already handled doesn't affect render output, so it belongs in a ref, not state.
How?
Images parsed from saved content are marked handled and skipped, and images that leave the gallery are dropped from the handled set, so an image moved back or restored by undo gets the settings again. The apply step is a
useEventcallback, so it reads the latest attributes without being an effect dependency, and new images are updated in oneupdateBlockAttributescall.buildImageAttributesreads records from amediaByIdmap.Testing Instructions
Testing Instructions for Keyboard
Same as above.
Use of AI Tools
Assisted by Claude.