Skip to content

Commit

Permalink
[gatsby-image] re: fade out base64 on full image load (#7539)
Browse files Browse the repository at this point in the history
* Make initial state be the same on both client and server

* Fix for browser env without Intersection Observer available

* Simpler implementation

* Typo
  • Loading branch information
alexkirsz authored and m-allanson committed Dec 12, 2018
1 parent 3ba82a3 commit 069918a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 14 deletions.
12 changes: 6 additions & 6 deletions packages/gatsby-image/src/__tests__/__snapshots__/index.js.snap
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ exports[`<Img /> should render fixed size images 1`] = `
alt=""
class="placeholder"
src="string_of_base64"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0; color: red;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 1; color: red;"
title="Title for the image"
/>
<div
style="width: 100px; opacity: 0; height: 100px;"
style="width: 100px; opacity: 1; height: 100px;"
title="Title for the image"
/>
<picture>
Expand All @@ -29,7 +29,7 @@ exports[`<Img /> should render fixed size images 1`] = `
alt="Alt text for the image"
height="100"
src="test_image.jpg"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 1;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0;"
title="Title for the image"
width="100"
/>
Expand All @@ -54,11 +54,11 @@ exports[`<Img /> should render fluid images 1`] = `
alt=""
class="placeholder"
src="string_of_base64"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0; color: red;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 1; color: red;"
title="Title for the image"
/>
<div
style="position: absolute; top: 0px; bottom: 0px; opacity: 0; right: 0px; left: 0px;"
style="position: absolute; top: 0px; bottom: 0px; opacity: 1; right: 0px; left: 0px;"
title="Title for the image"
/>
<picture>
Expand All @@ -74,7 +74,7 @@ exports[`<Img /> should render fluid images 1`] = `
<img
alt="Alt text for the image"
src="test_image.jpg"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 1;"
style="position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 0;"
title="Title for the image"
/>
</picture>
Expand Down
3 changes: 2 additions & 1 deletion packages/gatsby-image/src/__tests__/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,9 @@ describe(`<Img />`, () => {
expect(placeholderImageTag.getAttribute(`title`)).toEqual(
`Title for the 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%; opacity: 0; color: red;`
`position: absolute; top: 0px; left: 0px; width: 100%; height: 100%; opacity: 1; color: red;`
)
expect(placeholderImageTag.getAttribute(`class`)).toEqual(`placeholder`)
})
Expand Down
12 changes: 5 additions & 7 deletions packages/gatsby-image/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,36 +127,34 @@ class Image extends React.Component {
constructor(props) {
super(props)

// If this browser doesn't support the IntersectionObserver API
// we default to start downloading the image right away.
// default settings for browser without Intersection Observer available
let isVisible = true
let imgLoaded = true
let imgLoaded = false
let IOSupported = false
let fadeIn = props.fadeIn

// If this image has already been loaded before then we can assume it's
// already in the browser cache so it's cheap to just show directly.
const seenBefore = inImageCache(props)

// browser with Intersection Observer available
if (
!seenBefore &&
typeof window !== `undefined` &&
window.IntersectionObserver
) {
isVisible = false
imgLoaded = false
IOSupported = true
}

// Always don't render image while server rendering
// Never render image during SSR
if (typeof window === `undefined`) {
isVisible = false
imgLoaded = false
}

// Force render for critical images
if (props.critical) {
isVisible = true
imgLoaded = false
IOSupported = false
}

Expand Down

0 comments on commit 069918a

Please sign in to comment.