Skip to content

Commit efb793d

Browse files
committed
speed(homepage) refactor approach to use existing package
1 parent 8ba8d6a commit efb793d

File tree

5 files changed

+53
-60
lines changed

5 files changed

+53
-60
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,7 @@
149149
"react-dom": "^16.2.0",
150150
"react-g-analytics": "0.4.2",
151151
"react-hot-loader": "^4.0.0-beta.12",
152+
"react-intersection-observer": "^8.22.6",
152153
"react-markdown": "4.0.4",
153154
"react-router-dom": "^4.2.2",
154155
"react-tiny-popover": "3.4.2",
Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
import React from 'react';
2-
import VisibilitySensor from '../VisibilitySensor/VisibilitySensor';
2+
import { InView } from 'react-intersection-observer';
33
import SmallIcon from '../../assets/icon-square-small-slack.png';
44
import './Contributors.scss';
55

6-
export default class Contributors extends VisibilitySensor {
6+
export default class Contributors extends React.Component {
7+
state = {
8+
inView: false
9+
}
10+
11+
handleInView = (inView) => {
12+
if (!inView) {
13+
return;
14+
}
15+
this.setState({ inView });
16+
}
17+
718
render() {
8-
const { isVisible } = this.state;
19+
const { inView } = this.state;
920
const { contributors } = this.props;
1021

1122
if (!contributors.length) {
1223
return <noscript />;
1324
}
1425

1526
return (
16-
<div className="contributors" ref={ this.visibilityTarget }>
27+
<InView as="div"
28+
onChange={ this.handleInView }
29+
threshold={ 0 }
30+
triggerOnce
31+
className="contributors">
1732
<div className="contributors__list">
1833
{
1934
contributors.map(contributor => (
2035
<a key={ contributor }
2136
className="contributor"
2237
href={ `https://github.com/${contributor}` }>
2338
<img alt={ contributor }
24-
src={ isVisible ? `https://github.com/${contributor}.png?size=90` : SmallIcon } />
39+
src={ inView ? `https://github.com/${contributor}.png?size=90` : SmallIcon } />
2540
<span className="contributor__name"> {contributor}</span>
2641
</a>
2742
))
2843
}
2944
</div>
30-
</div>
45+
</InView>
3146
);
3247
}
3348
}

src/components/Support/Support.jsx

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// Import External Dependencies
22
import React from 'react';
3-
import VisibilitySensor from '../VisibilitySensor/VisibilitySensor';
3+
import { InView } from 'react-intersection-observer';
44

55
// Import Data
66
import Backers from './_supporters.json';
@@ -62,10 +62,22 @@ function formatMoney(number) {
6262
return str;
6363
}
6464

65-
export default class Support extends VisibilitySensor {
65+
export default class Support extends React.Component {
66+
state = {
67+
inView: false
68+
}
69+
70+
handleInView = (inView) => {
71+
if (!inView) {
72+
return;
73+
}
74+
this.setState({ inView });
75+
};
76+
6677
render() {
6778
let { rank } = this.props;
68-
const { isVisible } = this.state;
79+
80+
const { inView } = this.state;
6981

7082
let supporters = SUPPORTERS;
7183
let minimum, maximum, maxAge, limit, random;
@@ -110,7 +122,11 @@ export default class Support extends VisibilitySensor {
110122
}
111123

112124
return (
113-
<div className="support" ref={ this.visibilityTarget }>
125+
<InView as="div"
126+
onChange={ this.handleInView }
127+
threshold={ 0 }
128+
triggerOnce
129+
className="support">
114130
<div className="support__description">
115131
{ rank === 'backer' ? (
116132
<p>
@@ -136,7 +152,7 @@ export default class Support extends VisibilitySensor {
136152
href={ supporter.website || `https://opencollective.com/${supporter.slug}` }>
137153
{<img
138154
className={ `support__${rank}-avatar` }
139-
src={ (isVisible && supporter.avatar) ? supporter.avatar : SmallIcon }
155+
src={ (inView && supporter.avatar) ? supporter.avatar : SmallIcon }
140156
alt={ supporter.name || supporter.slug ? `${supporter.name || supporter.slug}'s avatar` : 'avatar' }
141157
onError={ this._handleImgError } />}
142158
</a>
@@ -148,7 +164,7 @@ export default class Support extends VisibilitySensor {
148164
Become a { rank === 'backer' ? 'backer' : 'sponsor' }
149165
</a>
150166
</div>
151-
</div>
167+
</InView>
152168
);
153169
}
154170

src/components/VisibilitySensor/VisibilitySensor.jsx

Lines changed: 0 additions & 47 deletions
This file was deleted.

yarn.lock

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
dependencies:
1010
regenerator-runtime "^0.12.0"
1111

12-
"@babel/runtime@^7.1.2":
12+
"@babel/runtime@^7.0.0", "@babel/runtime@^7.1.2":
1313
version "7.4.3"
1414
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.4.3.tgz#79888e452034223ad9609187a0ad1fe0d2ad4bdc"
1515
integrity sha512-9lsJwJLxDh/T3Q3SZszfWOTkk3pHbkmH+3KY+zwIDmsNlxsumuhS2TH3NIpktU4kNvfzy+k3eLT7aTJSPTo0OA==
@@ -9113,6 +9113,14 @@ react-hot-loader@^4.0.0-beta.12:
91139113
shallowequal "^1.0.2"
91149114
source-map "^0.7.3"
91159115

9116+
react-intersection-observer@^8.22.6:
9117+
version "8.22.6"
9118+
resolved "https://registry.yarnpkg.com/react-intersection-observer/-/react-intersection-observer-8.22.6.tgz#b03ad27a612bdbb5510f5fd21e997bdfc012d784"
9119+
integrity sha512-xhQK9jqU4yUUCQPne4rXt21ExnWTpbC3fhXlJlWOchFH2S9kaf0CJU5pTyCds2HKs43QE1+y4sGkBAoktnpdGg==
9120+
dependencies:
9121+
"@babel/runtime" "^7.0.0"
9122+
invariant "^2.0.0"
9123+
91169124
react-is@^16.7.0, react-is@^16.8.1:
91179125
version "16.8.6"
91189126
resolved "https://registry.yarnpkg.com/react-is/-/react-is-16.8.6.tgz#5bbc1e2d29141c9fbdfed456343fe2bc430a6a16"

0 commit comments

Comments
 (0)