-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gatsby-remark-images): make images blur up (#7800)
BREAKING CHANGE: html markup has been changed, high resolution image (img.gatsby-resp-image-image) is no longer nested inside span with low resolution placeholder (span.gatsby-resp-image-wrapper) - it's sibling of that span now. This might affect any custom styling that is applied to inline images
- Loading branch information
1 parent
3ed9735
commit 5a5254e
Showing
6 changed files
with
99 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
{ | ||
"presets": [ | ||
["babel-preset-gatsby-package"] | ||
["babel-preset-gatsby-package", { "browser": true }] | ||
] | ||
} |
This file contains 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 |
---|---|---|
@@ -1,3 +1,5 @@ | ||
/constants.js | ||
/gatsby-browser.js | ||
/index.js | ||
/__tests__/* | ||
tests |
76 changes: 27 additions & 49 deletions
76
packages/gatsby-remark-images/src/__tests__/__snapshots__/index.js.snap
This file contains 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 |
---|---|---|
@@ -1,119 +1,97 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`it handles goofy nesting properly 1`] = ` | ||
" | ||
<span | ||
"<span | ||
class=\\"gatsby-resp-image-wrapper\\" | ||
style=\\"position: relative; display: block; max-width: 300px; margin-left: auto; margin-right: auto;\\" | ||
> | ||
<span | ||
class=\\"gatsby-resp-image-background-image\\" | ||
style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url('url('data:image/png;base64, iVBORw)'); background-size: cover; display: block;\\" | ||
> | ||
<img | ||
></span> | ||
<img | ||
class=\\"gatsby-resp-image-image\\" | ||
style=\\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\\" | ||
style=\\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\\" | ||
alt=\\"test\\" | ||
title=\\"\\" | ||
src=\\"not-a-real-dir/images/this-image-already-has-a-link.jpeg\\" | ||
srcset=\\"not-a-real-dir/images/this-image-already-has-a-link.jpeg, not-a-real-dir/images/this-image-already-has-a-link.jpeg\\" | ||
sizes=\\"(max-width: 650px) 100vw, 650px\\" | ||
/> | ||
</span> | ||
</span> | ||
" | ||
</span>" | ||
`; | ||
|
||
exports[`it leaves images that are already linked alone 1`] = ` | ||
" | ||
<span | ||
"<span | ||
class=\\"gatsby-resp-image-wrapper\\" | ||
style=\\"position: relative; display: block; max-width: 300px; margin-left: auto; margin-right: auto;\\" | ||
> | ||
<span | ||
class=\\"gatsby-resp-image-background-image\\" | ||
style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url('url('data:image/png;base64, iVBORw)'); background-size: cover; display: block;\\" | ||
> | ||
<img | ||
></span> | ||
<img | ||
class=\\"gatsby-resp-image-image\\" | ||
style=\\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\\" | ||
style=\\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\\" | ||
alt=\\"img\\" | ||
title=\\"\\" | ||
src=\\"not-a-real-dir/image/my-image.jpg\\" | ||
srcset=\\"not-a-real-dir/image/my-image.jpg, not-a-real-dir/image/my-image.jpg\\" | ||
sizes=\\"(max-width: 650px) 100vw, 650px\\" | ||
/> | ||
</span> | ||
</span> | ||
" | ||
</span>" | ||
`; | ||
|
||
exports[`it leaves linked HTML img tags alone 1`] = ` | ||
"<a href=\\"https://example.org\\"> | ||
<span class=\\"gatsby-resp-image-wrapper\\" style=\\"position: relative; display: block; max-width: 300px; margin-left: auto; margin-right: auto;\\"> | ||
<span class=\\"gatsby-resp-image-background-image\\" style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url('url('data:image/png;base64, iVBORw)'); background-size: cover; display: block;\\"> | ||
<img class=\\"gatsby-resp-image-image\\" style=\\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\\" alt=\\"this image already has a link\\" title=\\"\\" src=\\"not-a-real-dir/images/this-image-already-has-a-link.jpeg\\" srcset=\\"not-a-real-dir/images/this-image-already-has-a-link.jpeg, not-a-real-dir/images/this-image-already-has-a-link.jpeg\\" sizes=\\"(max-width: 650px) 100vw, 650px\\"> | ||
</span> | ||
<span class=\\"gatsby-resp-image-background-image\\" style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url('url('data:image/png;base64, iVBORw)'); background-size: cover; display: block;\\"></span> | ||
<img class=\\"gatsby-resp-image-image\\" style=\\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\\" alt=\\"this image already has a link\\" title=\\"\\" src=\\"not-a-real-dir/images/this-image-already-has-a-link.jpeg\\" srcset=\\"not-a-real-dir/images/this-image-already-has-a-link.jpeg, not-a-real-dir/images/this-image-already-has-a-link.jpeg\\" sizes=\\"(max-width: 650px) 100vw, 650px\\"> | ||
</span> | ||
</a>" | ||
`; | ||
exports[`it leaves single-line linked HTML img tags alone 1`] = ` | ||
" | ||
<span class=\\"gatsby-resp-image-wrapper\\" style=\\"position: relative; display: block; max-width: 300px; margin-left: auto; margin-right: auto;\\"> | ||
<span class=\\"gatsby-resp-image-background-image\\" style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url('url('data:image/png;base64, iVBORw)'); background-size: cover; display: block;\\"> | ||
<img class=\\"gatsby-resp-image-image\\" style=\\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\\" alt=\\"this image already has a link\\" title=\\"\\" src=\\"not-a-real-dir/images/this-image-already-has-a-link.jpeg\\" srcset=\\"not-a-real-dir/images/this-image-already-has-a-link.jpeg, not-a-real-dir/images/this-image-already-has-a-link.jpeg\\" sizes=\\"(max-width: 650px) 100vw, 650px\\"> | ||
</span> | ||
</span> | ||
" | ||
"<span class=\\"gatsby-resp-image-wrapper\\" style=\\"position: relative; display: block; max-width: 300px; margin-left: auto; margin-right: auto;\\"> | ||
<span class=\\"gatsby-resp-image-background-image\\" style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url('url('data:image/png;base64, iVBORw)'); background-size: cover; display: block;\\"></span> | ||
<img class=\\"gatsby-resp-image-image\\" style=\\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\\" alt=\\"this image already has a link\\" title=\\"\\" src=\\"not-a-real-dir/images/this-image-already-has-a-link.jpeg\\" srcset=\\"not-a-real-dir/images/this-image-already-has-a-link.jpeg, not-a-real-dir/images/this-image-already-has-a-link.jpeg\\" sizes=\\"(max-width: 650px) 100vw, 650px\\"> | ||
</span>" | ||
`; | ||
exports[`it transforms HTML img tags 1`] = ` | ||
" | ||
<a class=\\"gatsby-resp-image-link\\" href=\\"not-a-real-dir/image/my-image.jpeg\\" style=\\"display: block\\" target=\\"_blank\\" rel=\\"noopener\\"> | ||
<span class=\\"gatsby-resp-image-wrapper\\" style=\\"position: relative; display: block; max-width: 300px; margin-left: auto; margin-right: auto;\\"> | ||
<span class=\\"gatsby-resp-image-background-image\\" style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url('url('data:image/png;base64, iVBORw)'); background-size: cover; display: block;\\"> | ||
<img class=\\"gatsby-resp-image-image\\" style=\\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\\" alt=\\"my image\\" title=\\"\\" src=\\"not-a-real-dir/image/my-image.jpeg\\" srcset=\\"not-a-real-dir/image/my-image.jpeg, not-a-real-dir/image/my-image.jpeg\\" sizes=\\"(max-width: 650px) 100vw, 650px\\"> | ||
</span> | ||
"<a class=\\"gatsby-resp-image-link\\" href=\\"not-a-real-dir/image/my-image.jpeg\\" style=\\"display: block\\" target=\\"_blank\\" rel=\\"noopener\\"> | ||
<span class=\\"gatsby-resp-image-wrapper\\" style=\\"position: relative; display: block; max-width: 300px; margin-left: auto; margin-right: auto;\\"> | ||
<span class=\\"gatsby-resp-image-background-image\\" style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url('url('data:image/png;base64, iVBORw)'); background-size: cover; display: block;\\"></span> | ||
<img class=\\"gatsby-resp-image-image\\" style=\\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\\" alt=\\"my image\\" title=\\"\\" src=\\"not-a-real-dir/image/my-image.jpeg\\" srcset=\\"not-a-real-dir/image/my-image.jpeg, not-a-real-dir/image/my-image.jpeg\\" sizes=\\"(max-width: 650px) 100vw, 650px\\"> | ||
</span> | ||
</a> | ||
" | ||
</a>" | ||
`; | ||
exports[`it transforms images in markdown 1`] = ` | ||
" | ||
<a | ||
"<a | ||
class=\\"gatsby-resp-image-link\\" | ||
href=\\"not-a-real-dir/images/my-image.jpeg\\" | ||
style=\\"display: block\\" | ||
target=\\"_blank\\" | ||
rel=\\"noopener\\" | ||
> | ||
<span | ||
<span | ||
class=\\"gatsby-resp-image-wrapper\\" | ||
style=\\"position: relative; display: block; max-width: 300px; margin-left: auto; margin-right: auto;\\" | ||
> | ||
<span | ||
class=\\"gatsby-resp-image-background-image\\" | ||
style=\\"padding-bottom: 133.33333333333331%; position: relative; bottom: 0; left: 0; background-image: url('url('data:image/png;base64, iVBORw)'); background-size: cover; display: block;\\" | ||
> | ||
<img | ||
></span> | ||
<img | ||
class=\\"gatsby-resp-image-image\\" | ||
style=\\"width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px white;\\" | ||
style=\\"width:100%;height:100%;margin:0;vertical-align:middle;position:absolute;top:0;left:0;box-shadow:inset 0px 0px 0px 400px white;\\" | ||
alt=\\"image\\" | ||
title=\\"\\" | ||
src=\\"not-a-real-dir/images/my-image.jpeg\\" | ||
srcset=\\"not-a-real-dir/images/my-image.jpeg, not-a-real-dir/images/my-image.jpeg\\" | ||
sizes=\\"(max-width: 650px) 100vw, 650px\\" | ||
/> | ||
</span> | ||
</span> | ||
</a> | ||
" | ||
</a>" | ||
`; |
This file contains 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,3 @@ | ||
exports.imageClass = `gatsby-resp-image-image` | ||
exports.imageWrapperClass = `gatsby-resp-image-wrapper` | ||
exports.imageBackgroundClass = `gatsby-resp-image-background-image` |
This file contains 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,35 @@ | ||
const { | ||
imageClass, | ||
imageBackgroundClass, | ||
imageWrapperClass, | ||
} = require(`./constants`) | ||
|
||
exports.onRouteUpdate = () => { | ||
const imageWrappers = document.querySelectorAll(`.${imageWrapperClass}`) | ||
|
||
// https://css-tricks.com/snippets/javascript/loop-queryselectorall-matches/ | ||
// for cross-browser looping through NodeList without polyfills | ||
for (let i = 0; i < imageWrappers.length; i++) { | ||
const imageWrapper = imageWrappers[i] | ||
|
||
const backgroundElement = imageWrapper.querySelector( | ||
`.${imageBackgroundClass}` | ||
) | ||
const imageElement = imageWrapper.querySelector(`.${imageClass}`) | ||
|
||
const onImageLoad = () => { | ||
backgroundElement.style.transition = `opacity 0.5s 0.5s` | ||
backgroundElement.style.opacity = 0 | ||
imageElement.style.transition = `opacity 0.5s` | ||
imageElement.style.opacity = 1 | ||
imageElement.removeEventListener(`load`, onImageLoad) | ||
} | ||
|
||
if (imageElement.complete) { | ||
backgroundElement.style.opacity = 0 | ||
} else { | ||
imageElement.style.opacity = 0 | ||
imageElement.addEventListener(`load`, onImageLoad) | ||
} | ||
} | ||
} |
This file contains 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