Skip to content
This repository has been archived by the owner on Dec 11, 2019. It is now read-only.

Commit

Permalink
Smoother transition when newtab image is slow
Browse files Browse the repository at this point in the history
Fixes #5309
  • Loading branch information
Josiah Keller committed Feb 14, 2017
1 parent 6cf4f1b commit 58bdb2d
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 9 deletions.
50 changes: 44 additions & 6 deletions js/about/newtab.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,16 @@ const ipc = window.chrome.ipcRenderer
require('../../less/about/newtab.less')
require('../../node_modules/font-awesome/css/font-awesome.css')

const IMAGE_SLOW_THRESHOLD = 25

class NewTabPage extends React.Component {
constructor () {
super()
this.state = {
showSiteRemovalNotification: false,
imageLoadFailed: false,
imageLoadCompleted: false,
imageLoadSlow: false,
updatedStamp: undefined,
showEmptyPage: true,
showImages: false,
Expand Down Expand Up @@ -61,6 +65,7 @@ class NewTabPage extends React.Component {
})
})
}

get showImages () {
return this.state.showImages && !!this.state.backgroundImage
}
Expand Down Expand Up @@ -227,6 +232,34 @@ class NewTabPage extends React.Component {
: this.fallbackImage
})
}
/**
* Displays background image once it loads
*/
onImageLoadCompleted () {
this.setState({
imageLoadCompleted: true
})
clearTimeout(this.slowBackgroundTimeout)
}
/**
* If background image is taking a long time to load, makes it fade in
*/
slowBackground () {
this.setState({
imageLoadSlow: true
})
}
/**
* Helper for background className
*/
get backgroundClassName () {
return 'backgroundContainer' +
(this.state.imageLoadCompleted ? (this.state.imageLoadSlow ? ' backgroundLoadedSlow' : ' backgroundLoaded') : '')
}

componentDidMount () {
this.slowBackgroundTimeout = setTimeout(this.slowBackground.bind(this), IMAGE_SLOW_THRESHOLD)
}

render () {
// don't render if user prefers an empty page
Expand All @@ -249,12 +282,17 @@ class NewTabPage extends React.Component {
backgroundProps.style = this.state.backgroundImage.style
gradientClassName = 'bgGradient'
}
return <div className='dynamicBackground' {...backgroundProps}>
{
this.showImages
? <img src={this.state.backgroundImage.source} onError={this.onImageLoadFailed.bind(this)} data-test-id='backgroundImage' />
: null
}
return <div className='dynamicBackground'>
<div className={this.backgroundClassName} {...backgroundProps}>
{
this.showImages
? <img src={this.state.backgroundImage.source}
onLoad={this.onImageLoadCompleted.bind(this)}
onError={this.onImageLoadFailed.bind(this)}
data-test-id='backgroundImage' />
: null
}
</div>
<div className={gradientClassName} />
<div className='content'>
<main>
Expand Down
22 changes: 19 additions & 3 deletions less/about/newtab.less
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ body {
background: #fff;
}

body, .dynamicBackground, bgGradient {
bgGradient {
opacity: 0;
animation: fadeIn 200ms;
animation-fill-mode: forwards;
Expand All @@ -45,14 +45,30 @@ ul {
}

.dynamicBackground {
display: flex;
flex: 1;
}

.backgroundContainer {
background-position: top center;
background-repeat: no-repeat;
background-size: cover;
display: flex;
flex: 1;
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
opacity: 0;
> img {
display: none;
}
&.backgroundLoaded {
opacity: 1;
}
&.backgroundLoadedSlow {
animation: fadeIn 200ms;
animation-fill-mode: forwards;
}
}

.gradient {
Expand Down

0 comments on commit 58bdb2d

Please sign in to comment.