diff --git a/src/components/Contributors/Contributors.jsx b/src/components/Contributors/Contributors.jsx index 5c9322238ffc..d02c5730c846 100644 --- a/src/components/Contributors/Contributors.jsx +++ b/src/components/Contributors/Contributors.jsx @@ -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 ; - } - - return ( -
@@ -134,7 +136,7 @@ export default class Support extends React.Component { href={ supporter.website || `https://opencollective.com/${supporter.slug}` }> {} diff --git a/src/components/VisibilitySensor/VisibilitySensor.jsx b/src/components/VisibilitySensor/VisibilitySensor.jsx new file mode 100644 index 000000000000..d00bc19727ad --- /dev/null +++ b/src/components/VisibilitySensor/VisibilitySensor.jsx @@ -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(); + } + } +} \ No newline at end of file