Skip to content

Gallery: Track new images in a ref instead of state - #86648

Merged
Mamaduka merged 2 commits into
trunkfrom
update/gallery-new-images-ref
Nov 12, 2026
Merged

Mamaduka merged 2 commits into
trunkfrom
update/gallery-new-images-ref

Conversation

@Mamaduka

@Mamaduka Mamaduka commented Nov 11, 2026 •

Copy link
Copy Markdown
Member

What?

Removes useGetNewImages, which called setState inside useMemo during render, and moves applying the gallery settings to new images into a useApplyGallerySettings hook 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 useEvent callback, so it reads the latest attributes without being an effect dependency, and new images are updated in one updateBlockAttributes call. buildImageAttributes reads records from a mediaById map.

Testing Instructions

  1. Open a post or page.
  2. Insert a Gallery block and add one image from the Media Library.
  3. Select the Gallery, set Resolution to Medium, and choose "Link images to media files" from the toolbar Link menu.
  4. Add a second image using the toolbar Add button. It uses the Medium size and links to its media file.
  5. Insert a second Gallery with Resolution set to Large, drag the second image into it, then drag it back. It takes Large, then Medium again.
  6. Give the first image a custom link, save, and reload. Each image keeps its own link and size.

Testing Instructions for Keyboard

Same as above.

Use of AI Tools

Assisted by Claude.

@Mamaduka Mamaduka added [Type] Code Quality Issues or PRs that relate to code quality [Package] Block library /packages/block-library [Block] Gallery Affects the Gallery Block - used to display groups of images labels Nov 11, 2026
@github-actions

github-actions Bot commented Nov 11, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

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

Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@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.

📦 Bundle size

Size Change: +184 B (0%)

Total Size: 8.64 MB

📦 View Changed
Filename Size Change
build/scripts/block-editor/index.min.js 546 kB +191 B (+0.04%)
build/scripts/block-library/index.min.js 427 kB -7 B (0%)

129e90d Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric 6a5706c trunk % Change
timeToFirstByte 43.65 ms +12.03% -5.15% 43.1 ms +15.31% -6.03% 1.28%
largestContentfulPaint 68 ms +5.88% -5.88% 66 ms +12.12% -3.03% 3.03%
lcpMinusTtfb 23.1 ms +4.76% -2.16% 23.65 ms +17.55% -5.29% -2.33%
wpBeforeTemplate 22.78 ms +13.92% -2.63% 22.48 ms +16.28% -2.94% 1.33%
wpTemplate 17.21 ms +4.82% -8.48% 17.28 ms +3.07% -10.19% -0.41%
wpTotal 40.22 ms +12.71% -5.3% 39.75 ms +14.52% -5.96% 1.18%
wpMemoryUsage 7.72 MB +0% -0% 7.69 MB +0% -0% 0.43%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric 6a5706c trunk % Change
timeToFirstByte 35 ms +7.86% -1.43% 35.05 ms +8.7% -1.71% -0.14%
largestContentfulPaint 64 ms +3.13% -0% 64 ms +3.13% -3.13% 0%
lcpMinusTtfb 28.9 ms +2.42% -7.96% 28.85 ms +3.47% -10.05% 0.17%
wpBeforeTemplate 19.48 ms +5.39% -1.54% 19.24 ms +15.07% -1.51% 1.25%
wpTemplate 12.77 ms +4.78% -1.8% 13.13 ms +2.13% -2.28% -2.74%
wpTotal 32.34 ms +7.17% -1.36% 32.32 ms +9.81% -1.64% 0.06%
wpMemoryUsage 6.41 MB +0% -0% 6.38 MB +0% -0% 0.53%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric 6a5706c trunk % Change
mediaProcessingJpeg 324.76 ms +0.79% -0.96% 323.29 ms +1.29% -0.51% 0.45%
mediaProcessingAvif 4874.62 ms +0.42% -0.03% 4822.93 ms +0.65% -0.1% 1.07%
mediaProcessingJpegToAvif 3400.31 ms +0.11% -0.36% 3394.71 ms +0.23% -0.34% 0.16%

media-upload

Metric 6a5706c trunk % Change
jpegUploadProcessing 1460 ms +2.02% -1.67% 1448.73 ms +0.44% -0.59% 0.78%
pngUploadProcessing 232.49 ms +3% -3.7% 240.49 ms +5.09% -4.04% -3.33%
largeJpegUploadProcessing 1429.01 ms +1.61% -0.31% 1431.79 ms +1.49% -0.55% -0.19%
multipleImageUploadProcessing 1502.94 ms +1.14% -1.23% 1512.28 ms +1.03% -1.17% -0.62%

post-editor

Metric 6a5706c trunk % Change
serverResponse 261.04 ms +3.41% -3.74% 274.57 ms +5.86% -11.78% -4.93%
firstPaint 285.35 ms +6.52% -6.75% 272.76 ms +10.45% -1.42% 4.62%
domContentLoaded 521.18 ms +2.45% -4.7% 540.38 ms +0.5% -3.68% -3.55%
loaded 522.03 ms +2.42% -4.73% 541.14 ms +0.5% -3.67% -3.53%
firstContentfulPaint 302.18 ms +11.28% -3.85% 305.99 ms +3.52% -1.22% -1.25%
firstBlock 1345.69 ms +6.1% -0.85% 1430.85 ms +1% -1.11% -5.95%
type 12.65 ms +7.67% -8.77% 12.35 ms +4.21% -11.66% 2.43%
typeWithoutInspector 12.51 ms +1.36% -14.71% 12.3 ms +1.87% -13.74% 1.71%
typeWithTopToolbar 15.65 ms +2.3% -8.24% 16.85 ms +6.65% -7.12% -7.12%
typeContainer 5.96 ms +6.88% -2.52% 6.29 ms +7.31% -3.97% -5.25%
focus 75.27 ms +9.83% -6.54% 71.19 ms +2.33% -3.99% 5.73%
firstFocus 170.95 ms +0% -0% 149.08 ms +0% -0% 14.67%
selectAll 235.26 ms +1.38% -0.48% 227.04 ms +2.17% -0.64% 3.62%
undo 19.45 ms +6.89% -8.17% 18.67 ms +6.21% -2.14% 4.18%
listViewOpen 48.53 ms +17.04% -8% 48.05 ms +10.72% -18.88% 1%
inserterOpen 27.83 ms +4.2% -11.21% 25.4 ms +4.65% -6.54% 9.57%
inserterHover 8.57 ms +3.38% -7.12% 7.57 ms +6.74% -5.68% 13.21%
inserterSearch 5.73 ms +12.04% -3.32% 5.98 ms +6.35% -10.37% -4.18%
loadPatterns 428.05 ms +2.53% -4.83% 402 ms +3.34% -1.97% 6.48%
wpTotal 253.93 ms +3.8% -3.6% 267.38 ms +5.76% -12.07% -5.03%
wpMemoryUsage 14.78 MB +0% -0% 14.74 MB +0% -0% 0.24%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric 6a5706c trunk % Change
serverResponse 263.48 ms +0.75% -1.75% 249.2 ms +3.19% -5.93% 5.73%
firstPaint 294.7 ms +5.04% -8.34% 280.84 ms +6.88% -5.51% 4.94%
domContentLoaded 545.51 ms +3.07% -1.08% 569.75 ms +2.51% -1.73% -4.25%
loaded 546.17 ms +3.09% -1.07% 570.55 ms +2.5% -1.72% -4.27%
firstContentfulPaint 310.85 ms +4.1% -2.32% 319.41 ms +1.42% -2.28% -2.68%
firstBlock 1681.96 ms +1.11% -4.29% 1700.91 ms +0.93% -1.78% -1.11%
type 12.72 ms +6.37% -4.32% 12.42 ms +2.74% -7.97% 2.42%
navigate 83.25 ms +4.5% -5.01% 70.92 ms +12.93% -4.95% 17.39%
loadPatterns 1034.76 ms +1.39% -9.27% 1012.02 ms +6.02% -11.88% 2.25%
loadPages 914.69 ms +3.27% -1.96% 980.13 ms +6.04% -1.3% -6.68%
wpTotal 256.88 ms +0.79% -1.71% 242.94 ms +3.26% -6.17% 5.74%
wpMemoryUsage 12.79 MB +0% -0% 12.76 MB +0% -0% 0.24%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

129e90d Run

🏁 Flaky tests

Some 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 /test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts, passed after 1 failed attempt.
Error: expect(received).toMatchObject(expected)

- Expected  - 2
+ Received  + 2

  Object {
    "attributeKey": "content",
    "blockIndex": 0,
    "content": "abcdef",
-   "endOffset": 3,
-   "offset": 3,
+   "endOffset": 0,
+   "offset": 0,
  }

Call Log:
- Timeout 5000ms exceeded while waiting on the predicate
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-undo-redo.spec.ts:363:5
can apply custom colors in /test/e2e/specs/editor/blocks/buttons.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 2
+ Received  + 2

  <!-- wp:buttons -->
- <div class="wp-block-buttons"><!-- wp:button {"style":{"color":{"text":"#ff0000","background":"#00ff00"},"elements":{"link":{"color":{"text":"#ff0000"}}}}} -->
+ <div class="wp-block-buttons"><!-- wp:button {"style":{"color":{"text":"#ff0000","background":"#0000ff"},"elements":{"link":{"color":{"text":"#ff0000"}}}}} -->
- <div class="wp-block-button"><a class="wp-block-button__link has-text-color has-background has-link-color wp-element-button" style="color:#ff0000;background-color:#00ff00">Content</a></div>
+ <div class="wp-block-button"><a class="wp-block-button__link has-text-color has-background has-link-color wp-element-button" style="color:#ff0000;background-color:#0000ff">Content</a></div>
  <!-- /wp:button --></div>
  <!-- /wp:buttons -->
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/buttons.spec.js:374:21
should undo without broken temporary state in /test/e2e/specs/editor/blocks/image.spec.js, passed after 1 failed attempt.
Error: expect(received).toBe(expected) // Object.is equality

- Expected  - 2
+ Received  + 2

- <!-- wp:image -->
- <figure class="wp-block-image"><img alt=""/></figure>
+ <!-- wp:image {"id":21,"sizeSlug":"full","linkDestination":"none"} -->
+ <figure class="wp-block-image size-full"><img src="http://localhost:8891/wp-content/uploads/2026/11/87ee2bcf-b1c1-489d-aada-f51e3515153b.png" alt="" class="wp-image-21"/></figure>
  <!-- /wp:image -->
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/blocks/image.spec.js:432:49
three users concurrently edit a large post with diverse blocks in /test/e2e/specs/editor/collaboration/collaboration-stress.spec.ts, passed after 1 failed attempt.
TimeoutError: locator.waitFor: Timeout 17500ms exceeded.
Call log:
  - waiting for getByRole('button', { name: /Collaborators list/ }) to be visible

    at map (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/fixtures/collaboration-utils.ts:193:7)
    at CollaborationUtils.waitForMutualDiscovery (/home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/fixtures/collaboration-utils.ts:188:10)
    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/editor/collaboration/collaboration-stress.spec.ts:416:28

129e90d Run

@Mamaduka Mamaduka self-assigned this Nov 11, 2026
@Mamaduka
Mamaduka force-pushed the update/gallery-new-images-ref branch from 971c11a to 6761b7d Compare November 11, 2026 08:27
@Mamaduka
Mamaduka force-pushed the update/gallery-new-images-ref branch from 6761b7d to 7a4d7f0 Compare November 11, 2026 14:28

@andrewserong andrewserong left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 ramonjd left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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 ) ) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Thanks, @ramonjd! I'll have a look.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in 129e90d.

@Mamaduka

Copy link
Copy Markdown
Member Author

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 useEffect witch hunt 😄

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>
@Mamaduka
Mamaduka force-pushed the update/gallery-new-images-ref branch from 7a4d7f0 to 3d11ccc Compare November 12, 2026 07:48
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>
Comment on lines +1960 to +1961
"packages/block-library/src/gallery/use-apply-gallery-settings.ts": {
"no-restricted-syntax": {

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is just repositioning the existing useEffect, not a new suppression.

@Mamaduka

Copy link
Copy Markdown
Member Author

Going to merge this. The static analysis issue was fixed in #86720.

@Mamaduka
Mamaduka merged commit 1f5aa58 into trunk Nov 12, 2026
91 of 93 checks passed
@Mamaduka
Mamaduka deleted the update/gallery-new-images-ref branch November 12, 2026 08:39
@github-actions github-actions Bot added this to the Gutenberg 24.5 milestone Nov 12, 2026
@andrewserong

Copy link
Copy Markdown
Contributor

I've got a couple more PR ideas to clean up the edit component.

Nice, thank you for digging in here! 🙇

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.

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 🙂

Vedant-Gandhi pushed a commit to Vedant-Gandhi/gutenberg that referenced this pull request Nov 30, 2026
Co-authored-by: Mamaduka <mamaduka@git.wordpress.org>
Co-authored-by: andrewserong <andrewserong@git.wordpress.org>
Co-authored-by: ramonjd <ramonopoly@git.wordpress.org>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Block] Gallery Affects the Gallery Block - used to display groups of images [Package] Block library /packages/block-library [Type] Code Quality Issues or PRs that relate to code quality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants