Skip to content

Commit d824cfd

Browse files
author
catchme
committed
update code to the latest version of react, remove useless params and function
1 parent 2d164aa commit d824cfd

File tree

3 files changed

+89
-104
lines changed

3 files changed

+89
-104
lines changed

example-umd/main.js

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,39 @@
11
'use strict';
22

3-
var Example = React.createClass({
4-
getInitialState: function () {
5-
return { msg: '' };
6-
},
3+
class Example extends React.Component {
4+
constructor(props) {
5+
super(props);
6+
this.state = {
7+
msg: ''
8+
};
79

8-
onChange: function (isVisible) {
10+
this.onChange = this.onChange.bind(this);
11+
}
12+
13+
onChange(isVisible) {
914
this.setState({
1015
msg: 'Element is now ' + (isVisible ? 'visible' : 'hidden')
1116
});
12-
},
13-
14-
render: function () {
15-
var self = this;
17+
}
1618

19+
render() {
1720
return (
1821
React.createElement("div", null,
1922
React.createElement("p", {className: "msg"}, this.state.msg),
2023
React.createElement("div", {className: "before"}),
2124
React.createElement(VisibilitySensor, {
22-
containment: this.props.containment,
23-
onChange: this.onChange,
24-
minTopValue: this.props.minTopValue,
25-
partialVisibility: this.props.partialVisibility
26-
},
25+
containment: this.props.containment,
26+
onChange: this.onChange,
27+
minTopValue: this.props.minTopValue,
28+
partialVisibility: this.props.partialVisibility
29+
},
2730
React.createElement("div", {className: "sensor"})
2831
),
2932
React.createElement("div", {className: "after"})
3033
)
31-
);
34+
)
3235
}
33-
});
36+
}
3437

3538
ReactDOM.render(React.createElement(Example), document.getElementById('example'));
3639

example/main.js

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22

33
var React = require('react');
44
var ReactDOM = require('react-dom')
5-
var createReactClass = require('create-react-class')
65
var VisibilitySensor = require('../');
76

8-
var Example = createReactClass({
9-
getInitialState: function () {
10-
return { msg: '' };
11-
},
7+
class Example extends React.Component {
8+
constructor(props) {
9+
super(props);
10+
this.state = {
11+
msg: ''
12+
};
1213

13-
onChange: function (isVisible) {
14+
this.onChange = this.onChange.bind(this);
15+
}
16+
17+
onChange(isVisible) {
1418
this.setState({
1519
msg: 'Element is now ' + (isVisible ? 'visible' : 'hidden')
1620
});
17-
},
18-
19-
render: function () {
20-
var self = this;
21+
}
2122

23+
render() {
2224
return (
2325
<div>
2426
<p className='msg'>{this.state.msg}</p>
@@ -36,9 +38,9 @@ var Example = createReactClass({
3638
</VisibilitySensor>
3739
<div className='after'></div>
3840
</div>
39-
);
41+
)
4042
}
41-
});
43+
}
4244

4345
ReactDOM.render(React.createElement(Example), document.getElementById('example'));
4446

visibility-sensor.js

Lines changed: 56 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
var React = require('react');
44
var ReactDOM = require('react-dom');
55
var PropTypes = require('prop-types');
6-
var createReactClass = require('create-react-class');
76
var isVisibleWithOffset = require('./lib/is-visible-with-offset')
87

98
var containmentPropType = PropTypes.any;
@@ -12,36 +11,24 @@ if (typeof window !== 'undefined') {
1211
containmentPropType = PropTypes.instanceOf(window.Element);
1312
}
1413

15-
function throttle (callback, limit) {
16-
var wait = false;
17-
return function () {
18-
if (!wait) {
19-
wait = true;
20-
setTimeout(function () {
21-
callback();
22-
wait = false;
23-
}, limit);
24-
}
25-
}
26-
}
14+
class VisibilitySensor extends React.Component {
15+
displayName = 'VisibilitySensor';
2716

28-
function debounce(func, wait) {
29-
var timeout;
30-
return function() {
31-
var context = this, args = arguments;
32-
var later = function() {
33-
timeout = null;
34-
func.apply(context, args);
17+
constructor(props) {
18+
super(props);
19+
this.state = {
20+
isVisible: null,
21+
visibilityRect: {}
3522
};
36-
clearTimeout(timeout);
37-
timeout = setTimeout(later, wait);
38-
};
39-
}
4023

41-
module.exports = createReactClass({
42-
displayName: 'VisibilitySensor',
24+
this.getContainer = this.getContainer.bind(this);
25+
this.addEventListener = this.addEventListener.bind(this);
26+
this.startWatching = this.startWatching.bind(this);
27+
this.stopWatching = this.stopWatching.bind(this);
28+
this.check = this.check.bind(this);
29+
}
4330

44-
propTypes: {
31+
static propTypes = {
4532
onChange: PropTypes.func,
4633
active: PropTypes.bool,
4734
partialVisibility: PropTypes.oneOfType([
@@ -76,60 +63,51 @@ module.exports = createReactClass({
7663
PropTypes.func,
7764
]),
7865
minTopValue: PropTypes.number,
79-
},
80-
81-
getDefaultProps: function () {
82-
return {
83-
active: true,
84-
partialVisibility: false,
85-
minTopValue: 0,
86-
scrollCheck: false,
87-
scrollDelay: 250,
88-
scrollThrottle: -1,
89-
resizeCheck: false,
90-
resizeDelay: 250,
91-
resizeThrottle: -1,
92-
intervalCheck: true,
93-
intervalDelay: 100,
94-
delayedCall: false,
95-
offset: {},
96-
containment: null,
97-
children: React.createElement('span')
98-
};
99-
},
66+
}
10067

101-
getInitialState: function () {
102-
return {
103-
isVisible: null,
104-
visibilityRect: {}
105-
};
106-
},
68+
static defaultProps = {
69+
active: true,
70+
partialVisibility: false,
71+
minTopValue: 0,
72+
scrollCheck: false,
73+
scrollDelay: 250,
74+
scrollThrottle: -1,
75+
resizeCheck: false,
76+
resizeDelay: 250,
77+
resizeThrottle: -1,
78+
intervalCheck: true,
79+
intervalDelay: 100,
80+
delayedCall: false,
81+
offset: {},
82+
containment: null,
83+
children: React.createElement('span')
84+
}
10785

108-
componentDidMount: function () {
86+
componentDidMount() {
10987
this.node = ReactDOM.findDOMNode(this);
11088
if (this.props.active) {
11189
this.startWatching();
11290
}
113-
},
91+
}
11492

115-
componentWillUnmount: function () {
93+
componentWillUnmount() {
11694
this.stopWatching();
117-
},
95+
}
11896

119-
componentWillReceiveProps: function (nextProps) {
97+
componentWillReceiveProps(nextProps) {
12098
if (nextProps.active && !this.props.active) {
12199
this.setState(this.getInitialState());
122100
this.startWatching();
123101
} else if (!nextProps.active) {
124102
this.stopWatching();
125103
}
126-
},
104+
}
127105

128-
getContainer: function () {
106+
getContainer() {
129107
return this.props.containment || window;
130-
},
108+
}
131109

132-
addEventListener: function (target, event, delay, throttle) {
110+
addEventListener(target, event, delay, throttle) {
133111
if (!this.debounceCheck) {
134112
this.debounceCheck = {};
135113
}
@@ -165,9 +143,9 @@ module.exports = createReactClass({
165143

166144
target.addEventListener(event, info.fn);
167145
this.debounceCheck[event] = info;
168-
},
146+
}
169147

170-
startWatching: function () {
148+
startWatching() {
171149
if (this.debounceCheck || this.interval) { return; }
172150

173151
if (this.props.intervalCheck) {
@@ -194,9 +172,9 @@ module.exports = createReactClass({
194172

195173
// if dont need delayed call, check on load ( before the first interval fires )
196174
!this.props.delayedCall && this.check();
197-
},
175+
}
198176

199-
stopWatching: function () {
177+
stopWatching() {
200178
if (this.debounceCheck) {
201179
// clean up event listeners and their debounce callers
202180
for (var debounceEvent in this.debounceCheck) {
@@ -215,12 +193,12 @@ module.exports = createReactClass({
215193
this.debounceCheck = null;
216194

217195
if (this.interval) { this.interval = clearInterval(this.interval); }
218-
},
196+
}
219197

220198
/**
221199
* Check if the element is within the visible viewport
222200
*/
223-
check: function () {
201+
check () {
224202
var el = this.node;
225203
var rect;
226204
var containmentRect;
@@ -275,8 +253,8 @@ module.exports = createReactClass({
275253
// check for partial visibility
276254
if (this.props.partialVisibility) {
277255
var partialVisible =
278-
rect.top <= containmentRect.bottom && rect.bottom >= containmentRect.top &&
279-
rect.left <= containmentRect.right && rect.right >= containmentRect.left;
256+
rect.top <= containmentRect.bottom && rect.bottom >= containmentRect.top &&
257+
rect.left <= containmentRect.right && rect.right >= containmentRect.left;
280258

281259
// account for partial visibility on a single edge
282260
if (typeof this.props.partialVisibility === 'string') {
@@ -292,7 +270,7 @@ module.exports = createReactClass({
292270

293271
// Deprecated options for calculating offset.
294272
if (typeof offset.direction === 'string' &&
295-
typeof offset.value === 'number') {
273+
typeof offset.value === 'number') {
296274
console.warn('[notice] offset.direction and offset.value have been deprecated. They still work for now, but will be removed in next major version. Please upgrade to the new syntax: { %s: %d }', offset.direction, offset.value)
297275

298276
isVisible = isVisibleWithOffset(offset, rect, containmentRect);
@@ -306,13 +284,13 @@ module.exports = createReactClass({
306284
visibilityRect: visibilityRect
307285
};
308286
this.setState(state);
309-
if (this.props.onChange) this.props.onChange(isVisible, visibilityRect);
287+
if (this.props.onChange) this.props.onChange(isVisible);
310288
}
311289

312290
return state;
313-
},
291+
}
314292

315-
render: function () {
293+
render() {
316294
if (this.props.children instanceof Function) {
317295
return this.props.children({
318296
isVisible: this.state.isVisible,
@@ -321,4 +299,6 @@ module.exports = createReactClass({
321299
}
322300
return React.Children.only(this.props.children);
323301
}
324-
});
302+
}
303+
304+
module.exports = VisibilitySensor

0 commit comments

Comments
 (0)