33var React = require ( 'react' ) ;
44var ReactDOM = require ( 'react-dom' ) ;
55var PropTypes = require ( 'prop-types' ) ;
6- var createReactClass = require ( 'create-react-class' ) ;
76var isVisibleWithOffset = require ( './lib/is-visible-with-offset' )
87
98var 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