Skip to content
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

feat(gatsby-transformer-sharp): add inside and outside fit options #14852

Merged
merged 27 commits into from
Mar 10, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
d4ff46e
add inside and outside fit options
VGoose Apr 16, 2019
7fc1d8d
add fit and background options to resize and fixed
VGoose Apr 16, 2019
8db98f7
Modifying gatsby-image to use `contain` rather than `cover` for objec…
soundguy66 Jun 17, 2019
d588081
Updating gatsby-plugin-sharp documentation for clarity and to show th…
soundguy66 Jun 17, 2019
63947c3
Merge branch 'master' into topics/sharp-inside-outside - I believe I'…
soundguy66 Jun 18, 2019
bfe4171
Fixing some things that got blown away in the conflict merge.
soundguy66 Jun 18, 2019
c12bca3
Merge branch 'master' into topics/sharp-inside-outside
soundguy66 Jun 18, 2019
384d9e2
Undoing all of the merge errors from the last 2 merges
soundguy66 Jun 18, 2019
84efaba
Updating e2e test to match cover->contain change for gatsby-image.
soundguy66 Jun 18, 2019
2cd7ef3
Updating packages/gasby-image readme to reflect PR change. Updating d…
soundguy66 Jun 18, 2019
b0cbfb1
Merge branch 'master' into topics/sharp-inside-outside
soundguy66 Jun 21, 2019
25cec48
Fixing the snapshots for gatsby-image.
soundguy66 Jun 21, 2019
ab4b32e
Merge branch 'master' into topics/sharp-inside-outside to resolve con…
soundguy66 Jun 21, 2019
de9cea9
Merge remote-tracking branch 'upstream/master' into topics/sharp-insi…
Aug 14, 2019
2f587c9
Merge branch 'master' into topics/sharp-inside-outside
chris-hammond Aug 23, 2019
2f484f8
WIP
soundguy66 Oct 30, 2019
d1e3dfb
WIP
soundguy66 Oct 30, 2019
9137696
Cleanup.
soundguy66 Oct 30, 2019
4901fd1
More cleanup.
soundguy66 Oct 30, 2019
31f69c4
Merge branch 'master' into wip-inside-outside
soundguy66 Oct 30, 2019
7bebda8
Removing changes to gatsby-image.
soundguy66 Oct 30, 2019
db8d1ad
Cleaning up some weird auto-formatting.
soundguy66 Oct 30, 2019
75260f6
More missed merges and auto-formatted fixes.
soundguy66 Oct 30, 2019
c6ee761
Hopefully the last commit!
soundguy66 Oct 30, 2019
f3696d9
Merge branch 'master' into topics/sharp-inside-outside
soundguy66 Jan 27, 2020
479c416
cleanup calculations
wardpeet Mar 9, 2020
c8bede8
Merge branch 'master' into topics/sharp-inside-outside
wardpeet Mar 10, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 18 additions & 15 deletions docs/docs/gatsby-image.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ file(relativePath: { eq: "images/default.jpg" }) {
}
```

Read more in the [gatsby-plugin-sharp](/packages/gatsby-plugin-sharp/?=#fixed) README.
Read more about fixed image queries in the [gatsby-plugin-sharp](/packages/gatsby-plugin-sharp/#fixed) README.

### Images that stretch across a _fluid_ container

Expand Down Expand Up @@ -177,18 +177,17 @@ In a query, you can specify options for fluid images.
- `maxHeight`(int)
- `quality` (int, default: 50)
- `srcSetBreakpoints` (array of int, default: [])
- `fit` (string, default: `[sharp.fit.cover][6]`)
- `background` (string, default: `rgba(0,0,0,1)`)

#### Returns

- `base64` (string)
- `src` (string)
- `width` (int)
- `height` (int)
- `aspectRatio` (float)
- `src` (string)
- `srcSet` (string)
- `srcSetType` (string)
- `sizes` (string)
- `originalImg` (string)

This is where fragments like `GatsbyImageSharpFluid` come in handy, as they'll return all the above items in one line without having to type them all out:

Expand All @@ -204,7 +203,7 @@ file(relativePath: { eq: "images/default.jpg" }) {
}
```

Read more in the [gatsby-plugin-sharp](/packages/gatsby-plugin-sharp/?=#fluid) README.
Read more about fluid image queries in the [gatsby-plugin-sharp](/packages/gatsby-plugin-sharp/#fluid) README.

### Resized images

Expand Down Expand Up @@ -240,15 +239,19 @@ allImageSharp {
}
```

Read more about resized image queries in the [gatsby-plugin-sharp](/packages/gatsby-plugin-sharp/#resize) README.

### Shared query parameters

In addition to `gatsby-plugin-sharp` settings in `gatsby-config.js`, there are additional query options that apply to both _fluid_ and _fixed_ images:
In addition to `gatsby-plugin-sharp` settings in `gatsby-config.js`, there are additional query options that apply to _fluid_, _fixed_, and _resized_ images:

- `grayscale` (bool, default: false)
- `duotone` (bool|obj, default: false)
- `toFormat` (string, default: \`\`)
- `cropFocus` (string, default: `[sharp.strategy.attention][6]`)
- `pngCompressionSpeed` (int, default: 4)
- [`grayscale`](/packages/gatsby-plugin-sharp/#grayscale) (bool, default: false)
- [`duotone`](/packages/gatsby-plugin-sharp/#duotone) (bool|obj, default: false)
- [`toFormat`](/packages/gatsby-plugin-sharp/#toformat) (string, default: \`\`)
- [`cropFocus`](/packages/gatsby-plugin-sharp/#cropfocus) (string, default: `ATTENTION`)
- [`fit`](/packages/gatsby-plugin-sharp/#fit) (string, default: `COVER`)
- [`pngCompressionSpeed`](/packages/gatsby-plugin-sharp/#pngcompressionspeed) (int, default: 4)
- [`rotate`](/packages/gatsby-plugin-sharp/#rotate) (int, default: 0)

Here's an example of using the `duotone` option with a fixed image:

Expand Down Expand Up @@ -284,13 +287,13 @@ fixed(
</figcaption>
</figure>

Read more in the [`gatsby-plugin-sharp`](/packages/gatsby-plugin-sharp) README.
Read more about shared image query parameters in the [`gatsby-plugin-sharp`](/packages/gatsby-plugin-sharp/#shared-options) README.

## Image query fragments

GraphQL includes a concept called "query fragments", which are a part of a query that can be reused. To ease building with `gatsby-image`, Gatsby image processing plugins which support `gatsby-image` ship with fragments which you can easily include in your queries.

> Note: using fragments in your queries depends on which data source(s) you have configured. Read more in the [gatsby-image](/packages/gatsby-image#fragments) README.
> Note: using fragments in your queries depends on which data source(s) you have configured. Read more about image query fragments in the [gatsby-image](/packages/gatsby-image/#fragments) README.

### Common fragments with `gatsby-transformer-sharp`

Expand Down Expand Up @@ -366,7 +369,7 @@ After you've made a query, you can pass additional options to the gatsby-image c
| `onStartLoad` | `func` | A callback that is called when the full-size image starts loading, it gets the parameter `{ wasCached: <boolean> }` provided. |
| `onError` | `func` | A callback that is called when the image fails to load. |
| `Tag` | `string` | Which HTML tag to use for wrapping elements. Defaults to `div`. |
| `objectFit` | `string` | Passed to the `object-fit-images` polyfill when importing from `gatsby-image/withIEPolyfill`. Defaults to `cover`. |
| `objectFit` | `string` | Passed to the `object-fit-images` polyfill when importing from `gatsby-image/withIEPolyfill`. Defaults to `contain`. |
| `objectPosition` | `string` | Passed to the `object-fit-images` polyfill when importing from `gatsby-image/withIEPolyfill`. Defaults to `50% 50%`. |
| `loading` | `string` | Set the browser's native lazy loading attribute. One of `lazy`, `eager` or `auto`. Defaults to `lazy`. |
| `critical` | `bool` | Opt-out of lazy-loading behavior. Defaults to `false`. Deprecated, use `loading` instead. |
Expand Down
2 changes: 1 addition & 1 deletion e2e-tests/gatsby-image/cypress/integration/image.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ describe(`Production gatsby-image`, () => {
`left: 0px`,
`width: 100%`,
`height: 100%`,
`object-fit: cover`,
`object-fit: contain`,
])
})
})
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-image/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,7 @@ While you could achieve a similar effect with plain CSS media queries, `gatsby-i
| `onStartLoad` | `func` | A callback that is called when the full-size image starts loading, it gets the parameter { wasCached: boolean } provided. |
| `onError` | `func` | A callback that is called when the image fails to load. |
| `Tag` | `string` | Which HTML tag to use for wrapping elements. Defaults to `div`. |
| `objectFit` | `string` | Passed to the `object-fit-images` polyfill when importing from `gatsby-image/withIEPolyfill`. Defaults to `cover`. |
| `objectFit` | `string` | Passed to the `object-fit-images` polyfill when importing from `gatsby-image/withIEPolyfill`. Defaults to `contain`. |
| `objectPosition` | `string` | Passed to the `object-fit-images` polyfill when importing from `gatsby-image/withIEPolyfill`. Defaults to `50% 50%`. |
| `loading` | `string` | Set the browser's native lazy loading attribute. One of `lazy`, `eager` or `auto`. Defaults to `lazy`. |
| `critical` | `bool` | Opt-out of lazy-loading behavior. Defaults to `false`. Deprecated, use `loading` instead. |
Expand Down
30 changes: 15 additions & 15 deletions packages/gatsby-image/src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ exports[`<Image /> should have a transition-delay of 1sec 1`] = `
alt=""
class="placeholder"
src="string_of_base64"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 1; transition-delay: 1000ms; color: red;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: contain; object-position: center; opacity: 1; transition-delay: 1000ms; color: red;"
title="Title for the image"
/>
<picture>
Expand All @@ -34,13 +34,13 @@ exports[`<Image /> should have a transition-delay of 1sec 1`] = `
loading="lazy"
src="test_image.jpg"
srcset="some srcSet"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 0; transition: opacity 1000ms;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: contain; object-position: center; opacity: 0; transition: opacity 1000ms;"
title="Title for the image"
width="100"
/>
</picture>
<noscript>
&lt;picture&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;
&lt;picture&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:contain;object-position:center"/&gt;&lt;/picture&gt;
</noscript>
</div>
</div>
Expand All @@ -60,7 +60,7 @@ exports[`<Image /> should render fixed size images 1`] = `
alt=""
class="placeholder"
src="string_of_base64"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 1; transition-delay: 500ms; color: red;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: contain; object-position: center; opacity: 1; transition-delay: 500ms; color: red;"
title="Title for the image"
/>
<picture>
Expand All @@ -80,13 +80,13 @@ exports[`<Image /> should render fixed size images 1`] = `
loading="lazy"
src="test_image.jpg"
srcset="some srcSet"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 0; transition: opacity 500ms;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: contain; object-position: center; opacity: 0; transition: opacity 500ms;"
title="Title for the image"
width="100"
/>
</picture>
<noscript>
&lt;picture&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;
&lt;picture&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:contain;object-position:center"/&gt;&lt;/picture&gt;
</noscript>
</div>
</div>
Expand All @@ -109,7 +109,7 @@ exports[`<Image /> should render fluid images 1`] = `
alt=""
class="placeholder"
src="string_of_base64"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 1; transition-delay: 500ms; color: red;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: contain; object-position: center; opacity: 1; transition-delay: 500ms; color: red;"
title="Title for the image"
/>
<picture>
Expand All @@ -131,12 +131,12 @@ exports[`<Image /> should render fluid images 1`] = `
sizes="(max-width: 600px) 100vw, 600px"
src="test_image.jpg"
srcset="some srcSet"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 0; transition: opacity 500ms;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: contain; object-position: center; opacity: 0; transition: opacity 500ms;"
title="Title for the image"
/>
</picture>
<noscript>
&lt;picture&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;
&lt;picture&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:contain;object-position:center"/&gt;&lt;/picture&gt;
</noscript>
</div>
</div>
Expand Down Expand Up @@ -164,7 +164,7 @@ exports[`<Image /> should render multiple fixed image variants 1`] = `
alt=""
class="placeholder"
src="other_string_of_base64"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 1; transition-delay: 500ms; color: red;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: contain; object-position: center; opacity: 1; transition-delay: 500ms; color: red;"
title="Title for the image"
/>
</picture>
Expand Down Expand Up @@ -193,13 +193,13 @@ exports[`<Image /> should render multiple fixed image variants 1`] = `
loading="lazy"
src="test_image_2.jpg"
srcset="some other srcSet"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 0; transition: opacity 500ms;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: contain; object-position: center; opacity: 0; transition: opacity 500ms;"
title="Title for the image"
width="100"
/>
</picture>
<noscript>
&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;
&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:contain;object-position:center"/&gt;&lt;/picture&gt;
</noscript>
</div>
</div>
Expand Down Expand Up @@ -230,7 +230,7 @@ exports[`<Image /> should render multiple fluid image variants 1`] = `
alt=""
class="placeholder"
src="string_of_base64"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 1; transition-delay: 500ms; color: red;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: contain; object-position: center; opacity: 1; transition-delay: 500ms; color: red;"
title="Title for the image"
/>
</picture>
Expand Down Expand Up @@ -263,12 +263,12 @@ exports[`<Image /> should render multiple fluid image variants 1`] = `
sizes="(max-width: 600px) 100vw, 600px"
src="test_image_2.jpg"
srcset="some other srcSet"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 0; transition: opacity 500ms;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: contain; object-position: center; opacity: 0; transition: opacity 500ms;"
title="Title for the image"
/>
</picture>
<noscript>
&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;
&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:contain;object-position:center"/&gt;&lt;/picture&gt;
</noscript>
</div>
</div>
Expand Down
2 changes: 1 addition & 1 deletion packages/gatsby-image/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ describe(`<Image />`, () => {
)
// No Intersection Observer in JSDOM, so placeholder img will be visible (opacity 1) by default
expect(placeholderImageTag.getAttribute(`style`)).toEqual(
`position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: cover; object-position: center; opacity: 1; transition-delay: 500ms; color: red;`
`position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; object-fit: contain; object-position: center; opacity: 1; transition-delay: 500ms; color: red;`
)
expect(placeholderImageTag.getAttribute(`class`)).toEqual(`placeholder`)
})
Expand Down
4 changes: 2 additions & 2 deletions packages/gatsby-image/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ const noscriptImg = props => {

const sources = generateNoscriptSources(props.imageVariants)

return `<picture>${sources}<img ${loading}${width}${height}${sizes}${srcSet}${src}${alt}${title}${crossOrigin}${draggable}style="position:absolute;top:0;left:0;opacity:1;width:100%;height:100%;object-fit:cover;object-position:center"/></picture>`
return `<picture>${sources}<img ${loading}${width}${height}${sizes}${srcSet}${src}${alt}${title}${crossOrigin}${draggable}style="position:absolute;top:0;left:0;opacity:1;width:100%;height:100%;object-fit:contain;object-position:center"/></picture>`
}

// Earlier versions of gatsby-image during the 2.x cycle did not wrap
Expand Down Expand Up @@ -263,7 +263,7 @@ const Img = React.forwardRef((props, ref) => {
left: 0,
width: `100%`,
height: `100%`,
objectFit: `cover`,
objectFit: `contain`,
objectPosition: `center`,
...style,
}}
Expand Down
Loading