Skip to content

Commit f24a23d

Browse files
ascorbicLaurie
and
Laurie
authored
feat(gatsby-plugin-image): Make dimensions optional and default to constrained (#28662)
* Default to constrained * Size defaults * Fixed size defaults * Update proptypes * use fixed layout in tests * update e2e * update last e2e Co-authored-by: Laurie <[email protected]>
1 parent 140d123 commit f24a23d

File tree

12 files changed

+55
-43
lines changed

12 files changed

+55
-43
lines changed

e2e-tests/gatsby-static-image/src/pages/constrained.js

+4-16
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,16 @@ import Layout from "../components/layout"
66
const FluidPage = () => (
77
<Layout>
88
<div data-testid="image-fluid">
9-
<StaticImage
10-
src="../images/citrus-fruits.jpg"
11-
layout="constrained"
12-
alt="Citrus fruits"
13-
/>
9+
<StaticImage src="../images/citrus-fruits.jpg" alt="Citrus fruits" />
1410
</div>
1511
<div data-testid="image-fluid-png">
16-
<StaticImage
17-
src="../images/gatsby-icon.png"
18-
layout="constrained"
19-
alt="Gatsby icon"
20-
/>
12+
<StaticImage src="../images/gatsby-icon.png" alt="Gatsby icon" />
2113
</div>
2214
<div data-testid="image-fluid-relative">
23-
<StaticImage
24-
src="../../content/relative.jpg"
25-
layout="constrained"
26-
alt="Citrus fruits"
27-
/>
15+
<StaticImage src="../../content/relative.jpg" alt="Citrus fruits" />
2816
</div>
2917
<div data-testid="invalid-image">
30-
<StaticImage src="./does-not-exist.jpg" layout="constrained" />
18+
<StaticImage src="./does-not-exist.jpg" />
3119
</div>
3220
</Layout>
3321
)

e2e-tests/gatsby-static-image/src/pages/fixed.js

+3
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,23 @@ const FluidPage = () => (
99
<StaticImage
1010
src="../images/citrus-fruits.jpg"
1111
width={500}
12+
layout="fixed"
1213
alt="Citrus fruits"
1314
/>
1415
</div>
1516
<div data-testid="image-fixed-png">
1617
<StaticImage
1718
src="../images/gatsby-icon.png"
1819
width={500}
20+
layout="fixed"
1921
alt="Gatsby Icon"
2022
/>
2123
</div>
2224
<div data-testid="image-fixed-relative">
2325
<StaticImage
2426
src="../../content/relative.jpg"
2527
height={500}
28+
layout="fixed"
2629
alt="Citrus fruits"
2730
/>
2831
</div>

e2e-tests/path-prefix/src/pages/index.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@ import Layout from "../components/layout"
77
const IndexPage = () => (
88
<Layout>
99
<h1>Hi people</h1>
10-
<StaticImage src="../images/gatsby-icon.png" alt="Gatsby icon"/>
10+
<StaticImage
11+
src="../images/gatsby-icon.png"
12+
alt="Gatsby icon"
13+
layout="fixed"
14+
/>
1115
<p>Welcome to your new Gatsby site.</p>
1216
<p>Now go build something great.</p>
1317
<Link data-testid="page-2-link" to="/page-2/">

e2e-tests/production-runtime/src/components/header.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,11 @@ const Header = ({ siteTitle }) => (
1717
padding: `1.45rem 1.0875rem`,
1818
}}
1919
>
20-
<StaticImage src="../images/gatsby-icon.png" alt="Gatsby icon"/>
20+
<StaticImage
21+
src="../images/gatsby-icon.png"
22+
alt="Gatsby icon"
23+
layout="fixed"
24+
/>
2125
<h1 style={{ margin: 0 }}>
2226
<Link
2327
data-testid="index-link"

e2e-tests/visual-regression/src/pages/static-images/constrained.js

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ const Page = () => {
1010
<TestWrapper>
1111
<StaticImage
1212
src="../../images/cornwall.jpg"
13-
layout="constrained"
1413
alt="cornwall"
1514
maxWidth={1024}
1615
/>

e2e-tests/visual-regression/src/pages/static-images/fixed.js

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const Page = () => {
1414
width={240}
1515
height={100}
1616
alt="cornwall"
17+
layout="fixed"
1718
/>
1819
</TestWrapper>
1920
</Layout>

packages/gatsby-plugin-image/README.md

+4-4
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,15 @@ const width = 300
111111

112112
### API
113113

114-
The only required prop is `src`. The default type is `fixed`. The other props match those of [the new GatsbyImage component](#gatsbyimage). You can also pass in options which are forwarded to [`gatsbyImageData`](#graphql-resolver).
114+
The only required prop is `src`. The default type is `constrained`. The other props match those of [the new GatsbyImage component](#gatsbyimage). You can also pass in options which are forwarded to [`gatsbyImageData`](#graphql-resolver).
115115

116116
## GatsbyImage
117117

118118
Speedy, optimized images without the work.
119119

120120
GatsbyImage is a React component specially designed to give your users a great image experience. It combines speed and best practices.
121121

122-
Note: GatsbyImage is not a drop-in replacement for `<img>`. It's optimized for fixed width/height images and images that stretch the full-width of a container. You can also build your own GatsbyImage component with the utilities we export from this package.
122+
Note: GatsbyImage is not a drop-in replacement for `<img>`. It's optimized for fixed width/height images and images that stretch the full-width of a container.
123123

124124
## Table of Contents
125125

@@ -329,9 +329,9 @@ These arguments can be passed to the `gatsbyImageData()` resolver:
329329
- `NONE`: no placeholder. Set "background" to use a fixed background color.
330330
- `DOMINANT_COLOR`: a solid color, calculated from the dominant color of the image.
331331
- **layout**: The layout for the image.
332-
- `FIXED`: (default) A static image size, that does not resize according to the screen width
332+
- `CONSTRAINED`: (default) Resizes to fit its container, up to a maximum width, at which point it will remain fixed in size.
333+
- `FIXED`: A static image size, that does not resize according to the screen width
333334
- `FLUID`: The image resizes to fit its container. Pass a "sizes" option if it isn't going to be the full width of the screen.
334-
- `CONSTRAINED`: Resizes to fit its container, up to a maximum width, at which point it will remain fixed in size.
335335
- **outputPixelDensities**: A list of image pixel densities to generate, for high-resolution (retina) screens. It will never generate images larger than the source, and will always include a 1x image.
336336
Default is `[ 0.25, 0.5, 1, 2 ]`, for fluid/constrained images, and `[ 1, 2 ]` for fixed. In this case, an image with a fluid layout and maxWidth = 400 would generate images at 100, 200, 400 and 800px wide
337337
- **sizes**: The "[sizes](https://developer.mozilla.org/en-US/docs/Learn/HTML/Multimedia_and_embedding/Responsive_images)" attribute, passed to the `<img>` tag. This describes the display size of the image. This does not affect the generated images, but is used by the browser to decide which images to download. You can leave this blank for fixed images, or if the responsive image container will be the full width of the screen. In these cases we will generate an appropriate value. If, however, you are generating responsive images that are not the full width of the screen, you should provide a sizes property for best performance. You can alternatively pass this value to the component.

packages/gatsby-plugin-image/src/__tests__/image-utils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const args: IGatsbyImageHelperArgs = {
2424
filename: `afile.jpg`,
2525
generateImageSource,
2626
width: 400,
27+
layout: `fixed`,
2728
sourceMetadata: {
2829
width: 800,
2930
height: 600,

packages/gatsby-plugin-image/src/components/static-image.server.tsx

+11-16
Original file line numberDiff line numberDiff line change
@@ -82,31 +82,26 @@ const checkDimensionProps: PropTypes.Validator<number> = (
8282
propName: keyof IStaticImageProps & IPrivateProps,
8383
...rest
8484
) => {
85-
if (props.layout === `fluid` || props.layout === `constrained`) {
86-
if (propName === `maxWidth` && !props[propName]) {
87-
return new Error(
88-
`The prop "${propName}" is required when layout is "${props.layout}"`
89-
)
90-
}
91-
if ((propName === `width` || propName === `height`) && props[propName]) {
92-
return new Error(
93-
`"${propName}" ${props[propName]} may not be passed when layout is "${props.layout}"`
94-
)
95-
}
85+
if (
86+
props.layout !== `fixed` &&
87+
(propName === `width` || propName === `height`) &&
88+
props[propName]
89+
) {
90+
return new Error(
91+
`"${propName}" ${props[propName]} may not be passed when layout is "${
92+
props.layout || `constrained`
93+
}"`
94+
)
9695
} else {
9796
if (
97+
props.layout === `fixed` &&
9898
(propName === `maxWidth` || propName === `maxHeight`) &&
9999
props[propName]
100100
) {
101101
return new Error(
102102
`"${propName}" may not be passed when layout is "${props.layout}"`
103103
)
104104
}
105-
if (propName === `width` && !props[propName]) {
106-
return new Error(
107-
`The prop "${propName}" is required when layout is "${props.layout}"`
108-
)
109-
}
110105
}
111106
return PropTypes.number(props, propName, ...rest)
112107
}

packages/gatsby-plugin-image/src/image-utils.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ export function generateImageData(
142142
pluginName,
143143
sourceMetadata,
144144
generateImageSource,
145-
layout = `fixed`,
145+
layout = `constrained`,
146146
fit,
147147
options,
148148
width,
@@ -279,7 +279,7 @@ export function calculateImageSizes(args: IImageSizeArgs): IImageSizes {
279279
height,
280280
maxHeight,
281281
filename,
282-
layout = `fixed`,
282+
layout = `constrained`,
283283
sourceMetadata: imgDimensions,
284284
reporter = { warn },
285285
} = args

packages/gatsby-plugin-sharp/src/image-data.ts

+18-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ export async function generateImageData({
103103
cache,
104104
}: IImageDataArgs): Promise<IGatsbyImageData | undefined> {
105105
const {
106-
layout = `fixed`,
106+
layout = `constrained`,
107107
placeholder = `blurred`,
108108
tracedSVGOptions = {},
109109
transformOptions = {},
@@ -119,6 +119,23 @@ export async function generateImageData({
119119

120120
const metadata = await getImageMetadata(file, placeholder === `dominantColor`)
121121

122+
if (layout === `fixed` && !args.width && !args.height) {
123+
args.width = metadata.width
124+
}
125+
126+
if (
127+
layout !== `fixed` &&
128+
!args.maxWidth &&
129+
!args.maxHeight &&
130+
metadata.width
131+
) {
132+
if (layout === `constrained`) {
133+
args.maxWidth = metadata.width
134+
} else if (layout === `fluid`) {
135+
args.maxWidth = Math.round(metadata.width / 2)
136+
}
137+
}
138+
122139
const formats = new Set(args.formats)
123140
let useAuto = formats.has(``) || formats.has(`auto`) || formats.size === 0
124141

packages/gatsby-transformer-sharp/src/customize-schema.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ const imageNodeType = ({
398398
args: {
399399
layout: {
400400
type: ImageLayoutType,
401-
defaultValue: `fixed`,
401+
defaultValue: `constrained`,
402402
description: stripIndent`
403403
The layout for the image.
404404
FIXED: A static image sized, that does not resize according to the screen width

0 commit comments

Comments
 (0)