Skip to content

Commit ad7cd6b

Browse files
authored
fix(gatsby-image): Fix fluid not respecting maxWidth/maxHeight (#17006)
1 parent b9836da commit ad7cd6b

File tree

5 files changed

+25
-30
lines changed

5 files changed

+25
-30
lines changed

packages/gatsby-image/README.md

+4-24
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ Their fragments are:
225225
- `GatsbyImageSharpFluid_withWebp`
226226
- `GatsbyImageSharpFluid_withWebp_noBase64`
227227
- `GatsbyImageSharpFluid_withWebp_tracedSVG`
228+
- `GatsbyImageSharpFluidLimitPresentationSize`
228229

229230
### gatsby-source-contentful
230231

@@ -318,37 +319,16 @@ prop. e.g. `<Img fluid={fluid} />`
318319
### Avoiding stretched images using the fluid type
319320

320321
As mentioned previously, images using the _fluid_ type are stretched to
321-
match the container's width. In the case where the image's width is smaller than the available viewport, the image will stretch to match the container, potentially leading to unwanted problems and worsened image quality.
322+
match the container's width and height. In the case where the image's width or height is smaller than the available viewport, the image will stretch to match the container, potentially leading to unwanted problems and worsened image quality.
322323

323-
To counter this edge case one could wrap the _Img_ component in order to set a better, for that case, `maxWidth`:
324-
325-
```jsx
326-
const NonStretchedImage = props => {
327-
let normalizedProps = props
328-
if (props.fluid && props.fluid.presentationWidth) {
329-
normalizedProps = {
330-
...props,
331-
style: {
332-
...(props.style || {}),
333-
maxWidth: props.fluid.presentationWidth,
334-
margin: "0 auto", // Used to center the image
335-
},
336-
}
337-
}
338-
339-
return <Img {...normalizedProps} />
340-
}
341-
```
342-
343-
**Note:** The `GatsbyImageSharpFluid` fragment does not include `presentationWidth`.
344-
You will need to add it in your graphql query as is shown in the following snippet:
324+
To counter this edge case one could use the `GatsbyImageSharpFluidLimitPresentationSize` fragment to ask for additional presentation size properties.
345325

346326
```graphql
347327
{
348328
childImageSharp {
349329
fluid(maxWidth: 500, quality: 100) {
350330
...GatsbyImageSharpFluid
351-
presentationWidth
331+
...GatsbyImageSharpFluidLimitPresentationSize
352332
}
353333
}
354334
}

packages/gatsby-image/src/index.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ const getImageSrcKey = ({ fluid, fixed }) => {
8181

8282
/**
8383
* Returns the current src - Preferably with art-direction support.
84-
* @param currentData {{media?: string}[]} The fluid or fixed image array.
85-
* @return {{src: string, media?: string}}
84+
* @param currentData {{media?: string}[], maxWidth?: Number, maxHeight?: Number} The fluid or fixed image array.
85+
* @return {{src: string, media?: string, maxWidth?: Number, maxHeight?: Number}}
8686
*/
8787
const getCurrentSrcData = currentData => {
8888
if (isBrowser && hasArtDirectionSupport(currentData)) {
@@ -480,6 +480,8 @@ class Image extends React.Component {
480480
style={{
481481
position: `relative`,
482482
overflow: `hidden`,
483+
maxWidth: image.maxWidth ? `${image.maxWidth}px` : null,
484+
maxHeight: image.maxHeight ? `${image.maxHeight}px` : null,
483485
...style,
484486
}}
485487
ref={this.handleRef}
@@ -717,6 +719,8 @@ const fluidObject = PropTypes.shape({
717719
srcWebp: PropTypes.string,
718720
srcSetWebp: PropTypes.string,
719721
media: PropTypes.string,
722+
maxWidth: PropTypes.number,
723+
maxHeight: PropTypes.number,
720724
})
721725

722726
// If you modify these propTypes, please don't forget to update following files as well:

packages/gatsby-plugin-sharp/src/index.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -621,8 +621,8 @@ async function fluid({ file, args = {}, reporter, cache }) {
621621
srcSet,
622622
srcSetType,
623623
sizes: options.sizes,
624-
originalImg: originalImg,
625-
originalName: originalName,
624+
originalImg,
625+
originalName,
626626
density,
627627
presentationWidth,
628628
presentationHeight,

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,8 @@ const fluidNodeType = ({
270270
sizes: { type: new GraphQLNonNull(GraphQLString) },
271271
originalImg: { type: GraphQLString },
272272
originalName: { type: GraphQLString },
273-
presentationWidth: { type: GraphQLInt },
274-
presentationHeight: { type: GraphQLInt },
273+
presentationWidth: { type: new GraphQLNonNull(GraphQLInt) },
274+
presentationHeight: { type: new GraphQLNonNull(GraphQLInt) },
275275
},
276276
}),
277277
args: {

packages/gatsby-transformer-sharp/src/fragments.js

+11
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,17 @@ export const GatsbyImageSharpFluid = graphql`
115115
}
116116
`
117117

118+
/**
119+
* Presentation sizes to make sure a fluid container does not overflow
120+
* @type {Fragment}
121+
*/
122+
export const GatsbyImageSharpFluidLimitPresentationSize = graphql`
123+
fragment GatsbyImageSharpFluidLimitPresentationSize on ImageSharpFluid {
124+
maxHeight: presentationHeight
125+
maxWidth: presentationWidth
126+
}
127+
`
128+
118129
/**
119130
* Traced SVG fluid images
120131
* @type {Fragment}

0 commit comments

Comments
 (0)