-
Notifications
You must be signed in to change notification settings - Fork 1.2k
fix(core): size gallery images to their grid cell, not the viewport #3103
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
ascorbic
merged 1 commit into
emdash-cms:main
from
dchaudhari7177:fix/2930-gallery-sizes
Sep 14, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "emdash": patch | ||
| --- | ||
|
|
||
| Fixes the Portable Text `Gallery` component telling browsers each image fills the viewport. Its `sizes` attribute now describes one grid cell, using the gallery's column count and its two-column layout at 640px and below, so browsers stop downloading oversized images for multi-column galleries. Pass the new `sizes` prop to `Gallery` when it renders in a container narrower than the viewport. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| /** | ||
| * The gallery's `sizes` must describe a grid cell, not the viewport: each | ||
| * image renders in one of `columns` cells (two at 640px and below), and a | ||
| * `100vw` estimate lets the browser pick a far larger srcset candidate than | ||
| * the cell needs (#2930). | ||
| */ | ||
| import { experimental_AstroContainer as AstroContainer } from "astro/container"; | ||
| import { describe, expect, test } from "vitest"; | ||
|
|
||
| import Gallery from "../../src/components/Gallery.astro"; | ||
|
|
||
| const testMediaProviders = [ | ||
| { | ||
| id: "mock-gallery-images", | ||
| name: "Mock Gallery Images", | ||
| capabilities: { list: false, upload: false, delete: false, metadata: false }, | ||
| createProvider: () => ({ | ||
| id: "mock-gallery-images", | ||
| name: "Mock Gallery Images", | ||
| capabilities: { list: false, upload: false, delete: false, metadata: false }, | ||
| getEmbed: (_value: unknown, options: { width?: number; height?: number } = {}) => ({ | ||
| type: "image", | ||
| src: `https://img.example.com/original?w=${options.width ?? "auto"}`, | ||
| getSrc: ({ width, height }: { width?: number; height?: number } = {}) => | ||
| `https://img.example.com/render?w=${width ?? "auto"}&h=${height ?? "auto"}`, | ||
| }), | ||
| }), | ||
| }, | ||
| ]; | ||
|
|
||
| const providerGlobal = globalThis as typeof globalThis & { | ||
| __emdashTestMediaProviders?: typeof testMediaProviders; | ||
| }; | ||
| providerGlobal.__emdashTestMediaProviders = [ | ||
| ...(providerGlobal.__emdashTestMediaProviders ?? []), | ||
| ...testMediaProviders, | ||
| ]; | ||
|
|
||
| const locals = { | ||
| emdash: { getPublicMediaUrl: (k: string) => `/_emdash/api/media/file/${k}` }, | ||
| }; | ||
|
|
||
| const imgTags = (html: string) => html.match(/<img\b[^>]*>/g) ?? []; | ||
| const attr = (tag: string, name: string) => | ||
| tag.match(new RegExp(`\\b${name}="([^"]*)"`))?.[1]?.replaceAll("&", "&"); | ||
|
|
||
| function providerImage(key: string) { | ||
| return { | ||
| _type: "image" as const, | ||
| _key: key, | ||
| asset: { _ref: `provider-${key}`, provider: "mock-gallery-images" }, | ||
| alt: key, | ||
| width: 1600, | ||
| height: 1200, | ||
| }; | ||
| } | ||
|
|
||
| async function renderGallery(props: Record<string, unknown>) { | ||
| const container = await AstroContainer.create(); | ||
| return container.renderToString(Gallery, { props, locals }); | ||
| } | ||
|
|
||
| describe("Gallery sizes", () => { | ||
| test("provider images are sized to one of three default columns", async () => { | ||
| const html = await renderGallery({ | ||
| node: { _type: "gallery", _key: "g", images: [providerImage("a"), providerImage("b")] }, | ||
| }); | ||
| const tags = imgTags(html); | ||
|
|
||
| expect(tags).toHaveLength(2); | ||
| for (const tag of tags) { | ||
| expect(attr(tag, "srcset")).toContain("https://img.example.com/render?w=640"); | ||
| expect(attr(tag, "sizes")).toBe( | ||
| "(max-width: 640px) calc((100vw - 1rem) / 2), calc((100vw - 2rem) / 3)", | ||
| ); | ||
| } | ||
| }); | ||
|
|
||
| test("the slot follows the block's column count", async () => { | ||
| const html = await renderGallery({ | ||
| node: { _type: "gallery", _key: "g", columns: 4, images: [providerImage("a")] }, | ||
| }); | ||
|
|
||
| expect(attr(imgTags(html)[0]!, "sizes")).toBe( | ||
| "(max-width: 640px) calc((100vw - 1rem) / 2), calc((100vw - 3rem) / 4)", | ||
| ); | ||
| }); | ||
|
|
||
| test("locally stored images with dimensions get the same cell estimate", async () => { | ||
| const html = await renderGallery({ | ||
| node: { | ||
| _type: "gallery", | ||
| _key: "g", | ||
| images: [ | ||
| { | ||
| _type: "image", | ||
| _key: "local", | ||
| asset: { _ref: "media-1", url: "/_emdash/api/media/file/local.jpg" }, | ||
| alt: "local", | ||
| width: 1600, | ||
| height: 1200, | ||
| }, | ||
| ], | ||
| }, | ||
| }); | ||
| const tag = imgTags(html)[0]!; | ||
|
|
||
| expect(attr(tag, "data-astro-image")).toBe("constrained"); | ||
| expect(attr(tag, "sizes")).toBe( | ||
| "(max-width: 640px) calc((100vw - 1rem) / 2), calc((100vw - 2rem) / 3)", | ||
| ); | ||
| }); | ||
|
|
||
| test("a consumer-provided sizes wins", async () => { | ||
| const html = await renderGallery({ | ||
| node: { _type: "gallery", _key: "g", images: [providerImage("a")] }, | ||
| sizes: "(max-width: 640px) 45vw, 220px", | ||
| }); | ||
|
|
||
| expect(attr(imgTags(html)[0]!, "sizes")).toBe("(max-width: 640px) 45vw, 220px"); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion] The test header comment references issue
#2930. Per AGENTS.md comment discipline, issue/PR references should not live in code comments — they become stale narrative once the change merges; that context belongs in the commit message and PR description.Keep the explanatory prose about what the test reproduces; just remove the issue number.