Skip to content

Commit df7e93d

Browse files
authored
fix(gatsby-image): When art-directing images, default the padding aspect ratio to first image without a media query when no media query matches (#21431)
* Default to image without a media query when no media queries match window. * update snapshots and change aspect ratio so padding is rational
1 parent c76becb commit df7e93d

3 files changed

Lines changed: 62 additions & 11 deletions

File tree

packages/gatsby-image/src/__tests__/__snapshots__/index.js.snap

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ exports[`<Image /> should render multiple fixed image variants 1`] = `
176176
aria-hidden="true"
177177
class="placeholder"
178178
itemprop="item-prop-for-the-image"
179-
src="other_string_of_base64"
179+
src="string_of_base64"
180180
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 1; transition-delay: 500ms; color: red;"
181181
title="Title for the image"
182182
/>
@@ -204,15 +204,15 @@ exports[`<Image /> should render multiple fixed image variants 1`] = `
204204
height="100"
205205
itemprop="item-prop-for-the-image"
206206
loading="lazy"
207-
src="test_image_2.jpg"
208-
srcset="some other srcSet"
207+
src="test_image.jpg"
208+
srcset="some srcSet"
209209
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 0; transition: opacity 500ms;"
210210
title="Title for the image"
211211
width="100"
212212
/>
213213
</picture>
214214
<noscript>
215-
&lt;picture&gt;&lt;source type='image/webp' media="only screen and (min-width: 768px)" srcset="some other srcSetWebp" /&gt;&lt;source media="only screen and (min-width: 768px)" srcset="some other srcSet" /&gt;&lt;source type='image/webp' srcset="some srcSetWebp" /&gt;&lt;source srcset="some srcSet" /&gt;&lt;img loading="lazy" width="100" height="100" srcset="some other srcSet" src="test_image_2.jpg" alt="Alt text for the image" title="Title for the image" style="position:absolute;top:0;left:0;opacity:1;width:100%;height:100%;object-fit:cover;object-position:center"/&gt;&lt;/picture&gt;
215+
&lt;picture&gt;&lt;source type='image/webp' media="only screen and (min-width: 768px)" srcset="some other srcSetWebp" /&gt;&lt;source media="only screen and (min-width: 768px)" srcset="some other srcSet" /&gt;&lt;source type='image/webp' srcset="some srcSetWebp" /&gt;&lt;source srcset="some srcSet" /&gt;&lt;img loading="lazy" width="100" height="100" srcset="some srcSet" src="test_image.jpg" alt="Alt text for the image" title="Title for the image" style="position:absolute;top:0;left:0;opacity:1;width:100%;height:100%;object-fit:cover;object-position:center"/&gt;&lt;/picture&gt;
216216
</noscript>
217217
</div>
218218
</div>
@@ -278,14 +278,14 @@ exports[`<Image /> should render multiple fluid image variants 1`] = `
278278
itemprop="item-prop-for-the-image"
279279
loading="lazy"
280280
sizes="(max-width: 600px) 100vw, 600px"
281-
src="test_image_2.jpg"
282-
srcset="some other srcSet"
281+
src="test_image.jpg"
282+
srcset="some srcSet"
283283
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 0; transition: opacity 500ms;"
284284
title="Title for the image"
285285
/>
286286
</picture>
287287
<noscript>
288-
&lt;picture&gt;&lt;source type='image/webp' media="only screen and (min-width: 768px)" srcset="some other srcSetWebp" sizes="(max-width: 600px) 100vw, 600px" /&gt;&lt;source media="only screen and (min-width: 768px)" srcset="some other srcSet" sizes="(max-width: 600px) 100vw, 600px" /&gt;&lt;source type='image/webp' srcset="some srcSetWebp" sizes="(max-width: 600px) 100vw, 600px" /&gt;&lt;source srcset="some srcSet" sizes="(max-width: 600px) 100vw, 600px" /&gt;&lt;img loading="lazy" sizes="(max-width: 600px) 100vw, 600px" srcset="some other srcSet" src="test_image_2.jpg" alt="Alt text for the image" title="Title for the image" style="position:absolute;top:0;left:0;opacity:1;width:100%;height:100%;object-fit:cover;object-position:center"/&gt;&lt;/picture&gt;
288+
&lt;picture&gt;&lt;source type='image/webp' media="only screen and (min-width: 768px)" srcset="some other srcSetWebp" sizes="(max-width: 600px) 100vw, 600px" /&gt;&lt;source media="only screen and (min-width: 768px)" srcset="some other srcSet" sizes="(max-width: 600px) 100vw, 600px" /&gt;&lt;source type='image/webp' srcset="some srcSetWebp" sizes="(max-width: 600px) 100vw, 600px" /&gt;&lt;source srcset="some srcSet" sizes="(max-width: 600px) 100vw, 600px" /&gt;&lt;img loading="lazy" sizes="(max-width: 600px) 100vw, 600px" srcset="some srcSet" src="test_image.jpg" alt="Alt text for the image" title="Title for the image" style="position:absolute;top:0;left:0;opacity:1;width:100%;height:100%;object-fit:cover;object-position:center"/&gt;&lt;/picture&gt;
289289
</noscript>
290290
</div>
291291
</div>

packages/gatsby-image/src/__tests__/index.js

Lines changed: 47 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ const fixedImagesShapeMock = [
3333
base64: `string_of_base64`,
3434
},
3535
{
36-
width: 100,
37-
height: 100,
36+
width: 300,
37+
height: 300,
3838
src: `test_image_2.jpg`,
3939
srcSet: `some other srcSet`,
4040
srcSetWebp: `some other srcSetWebp`,
@@ -45,15 +45,15 @@ const fixedImagesShapeMock = [
4545

4646
const fluidImagesShapeMock = [
4747
{
48-
aspectRatio: 1.5,
48+
aspectRatio: 2,
4949
src: `test_image.jpg`,
5050
srcSet: `some srcSet`,
5151
srcSetWebp: `some srcSetWebp`,
5252
sizes: `(max-width: 600px) 100vw, 600px`,
5353
base64: `string_of_base64`,
5454
},
5555
{
56-
aspectRatio: 2,
56+
aspectRatio: 3,
5757
src: `test_image_2.jpg`,
5858
srcSet: `some other srcSet`,
5959
srcSetWebp: `some other srcSetWebp`,
@@ -121,6 +121,7 @@ const setupImages = (
121121
describe(`<Image />`, () => {
122122
const OLD_MATCH_MEDIA = window.matchMedia
123123
beforeEach(() => {
124+
// None of the media conditions above match.
124125
window.matchMedia = jest.fn(media =>
125126
media === `only screen and (min-width: 1024px)`
126127
? {
@@ -280,6 +281,48 @@ describe(`<Image />`, () => {
280281
)
281282
})
282283

284+
it(`should select the image with no media query as mocked image of fluid variants provided.`, () => {
285+
const { container } = render(
286+
<Image
287+
backgroundColor
288+
className={`fluidArtDirectedImage`}
289+
style={{ display: `inline` }}
290+
title={`Title for the image`}
291+
alt={`Alt text for the image`}
292+
crossOrigin={`anonymous`}
293+
fluid={fluidImagesShapeMock.slice().reverse()}
294+
itemProp={`item-prop-for-the-image`}
295+
placeholderStyle={{ color: `red` }}
296+
placeholderClassName={`placeholder`}
297+
/>
298+
)
299+
const aspectPreserver = container.querySelector(`div div div`)
300+
expect(aspectPreserver.getAttribute(`style`)).toEqual(
301+
expect.stringMatching(/padding-bottom: 50%/)
302+
)
303+
})
304+
305+
it(`should select the image with no media query as mocked image of fixed variants provided.`, () => {
306+
const { container } = render(
307+
<Image
308+
backgroundColor
309+
className={`fixedArtDirectedImage`}
310+
style={{ display: `inline` }}
311+
title={`Title for the image`}
312+
alt={`Alt text for the image`}
313+
crossOrigin={`anonymous`}
314+
fixed={fixedImagesShapeMock.slice().reverse()}
315+
itemProp={`item-prop-for-the-image`}
316+
placeholderStyle={{ color: `red` }}
317+
placeholderClassName={`placeholder`}
318+
/>
319+
)
320+
const aspectPreserver = container.querySelector(`div div`)
321+
expect(aspectPreserver.getAttribute(`style`)).toEqual(
322+
expect.stringMatching(/width: 100px; height: 100px;/)
323+
)
324+
})
325+
283326
it(`should call onLoad and onError image events`, () => {
284327
const onLoadMock = jest.fn()
285328
const onErrorMock = jest.fn()

packages/gatsby-image/src/index.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ const getCurrentSrcData = currentData => {
9191
if (foundMedia !== -1) {
9292
return currentData[foundMedia]
9393
}
94+
95+
// No media matches, select first element without a media condition
96+
const noMedia = currentData.findIndex(
97+
image => typeof image.media === `undefined`
98+
)
99+
if (noMedia !== -1) {
100+
return currentData[noMedia]
101+
}
94102
}
95103
// Else return the first image.
96104
return currentData[0]

0 commit comments

Comments
 (0)