-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
speed(homepage) offload images until in viewport
- Loading branch information
1 parent
198c534
commit 8ba8d6a
Showing
3 changed files
with
81 additions
and
24 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,25 +1,33 @@ | ||
import React from 'react'; | ||
import VisibilitySensor from '../VisibilitySensor/VisibilitySensor'; | ||
import SmallIcon from '../../assets/icon-square-small-slack.png'; | ||
import './Contributors.scss'; | ||
|
||
export default ({contributors}) => { | ||
if (!contributors.length) { | ||
return <noscript />; | ||
} | ||
|
||
return ( | ||
<div className="contributors"> | ||
<div className="contributors__list"> | ||
{ | ||
contributors.map(contributor => ( | ||
<a key={ contributor } | ||
className="contributor" | ||
href={ `https://github.com/${contributor}` }> | ||
<img alt={ contributor } src={ `https://github.com/${contributor}.png?size=90` } /> | ||
<span className="contributor__name"> {contributor}</span> | ||
</a> | ||
)) | ||
} | ||
export default class Contributors extends VisibilitySensor { | ||
render() { | ||
const { isVisible } = this.state; | ||
const { contributors } = this.props; | ||
|
||
if (!contributors.length) { | ||
return <noscript />; | ||
} | ||
|
||
return ( | ||
<div className="contributors" ref={ this.visibilityTarget }> | ||
<div className="contributors__list"> | ||
{ | ||
contributors.map(contributor => ( | ||
<a key={ contributor } | ||
className="contributor" | ||
href={ `https://github.com/${contributor}` }> | ||
<img alt={ contributor } | ||
src={ isVisible ? `https://github.com/${contributor}.png?size=90` : SmallIcon } /> | ||
<span className="contributor__name"> {contributor}</span> | ||
</a> | ||
)) | ||
} | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
); | ||
} | ||
} |
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
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,47 @@ | ||
import React from 'react'; | ||
|
||
export default class VisibilitySensor extends React.Component { | ||
state = { | ||
isVisible: false | ||
}; | ||
|
||
constructor(props) { | ||
super(props); | ||
this.visibilityTarget = React.createRef(); | ||
} | ||
|
||
componentWillUnmount() { | ||
this.stopListeningForVisibility(); | ||
} | ||
|
||
componentDidMount () { | ||
if (this.visibilityTarget.current) { | ||
this.startListeningForVisibility(); | ||
} | ||
} | ||
|
||
startListeningForVisibility = () => { | ||
if (typeof IntersectionObserver !== 'function') { | ||
// fall back to rendering images | ||
// browser doesnt support IntersectionObserver; | ||
return this.setState({ isVisible: true }); | ||
} | ||
|
||
this.observer = new IntersectionObserver(this.visibilityChanged, { | ||
threshold: 0.05 | ||
}); | ||
this.observer.observe(this.visibilityTarget.current); | ||
} | ||
|
||
stopListeningForVisibility = () => { | ||
this.observer && this.observer.unobserve(this.visibilityTarget.current); | ||
delete this.observer; | ||
} | ||
|
||
visibilityChanged = (entries) => { | ||
if (entries[0].isIntersecting) { | ||
this.setState({ isVisible: true }); | ||
this.stopListeningForVisibility(); | ||
} | ||
} | ||
} |