From 97aeb3ca5428228b0a2235d3fda39dceed3e6e61 Mon Sep 17 00:00:00 2001 From: Josiah Keller Date: Mon, 9 Jan 2017 20:18:08 -0500 Subject: [PATCH] Smoother transition when newtab image is slow Fixes #5309 --- js/about/newtab.js | 41 +++++++++++++++++++++++++++++++++++++++++ less/about/newtab.less | 26 ++++++++++++++++++-------- 2 files changed, 59 insertions(+), 8 deletions(-) diff --git a/js/about/newtab.js b/js/about/newtab.js index 4cceb7520f0..47c4ed5ce14 100644 --- a/js/about/newtab.js +++ b/js/about/newtab.js @@ -25,6 +25,8 @@ 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() @@ -32,6 +34,8 @@ class NewTabPage extends React.Component { showSiteRemovalNotification: false, backgroundImage: this.randomBackgroundImage, imageLoadFailed: false, + imageLoadCompleted: false, + imageLoadSlow: false, updatedStamp: undefined, showEmptyPage: true } @@ -53,6 +57,10 @@ class NewTabPage extends React.Component { }) }) } + componentDidMount () { + this.slowBackgroundTimeout = setTimeout(this.slowBackground.bind(this), IMAGE_SLOW_THRESHOLD) + } + get randomBackgroundImage () { const image = Object.assign({}, backgrounds[Math.floor(Math.random() * backgrounds.length)]) image.style = {backgroundImage: 'url(' + image.source + ')'} @@ -216,6 +224,30 @@ 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') : '') + } render () { // don't render if user prefers an empty page @@ -236,6 +268,15 @@ class NewTabPage extends React.Component { const gridLayout = this.gridLayout return
+
+ { + this.state.backgroundImage + ? + : null + } +
diff --git a/less/about/newtab.less b/less/about/newtab.less index 8a47a599af8..6dbc5b55357 100644 --- a/less/about/newtab.less +++ b/less/about/newtab.less @@ -20,12 +20,6 @@ body { background: #fff; } -body, .dynamicBackground { - opacity: 0; - animation: fadeIn 200ms; - animation-fill-mode: forwards; -} - .copyrightNotice { visibility: hidden !important; -webkit-user-select: none; @@ -50,14 +44,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 {