11import React , { Component } from 'react' ;
22import PropTypes from 'prop-types' ;
33import ReactDOM from 'react-dom' ;
4- import omit from 'lodash.omit ' ;
4+ import omit from 'lomit ' ;
55import throttle from 'lodash.throttle' ;
66import cleanProps from 'clean-react-props' ;
77
@@ -10,11 +10,11 @@ class ScrollTrigger extends Component {
1010 constructor ( props ) {
1111 super ( props ) ;
1212
13- this . onScroll = throttle ( this . onScroll . bind ( this ) , 100 , {
13+ this . onScroll = throttle ( this . onScroll . bind ( this ) , props . throttleScroll , {
1414 trailing : false ,
1515 } ) ;
1616
17- this . onResize = throttle ( this . onResize . bind ( this ) , 100 , {
17+ this . onResize = throttle ( this . onResize . bind ( this ) , props . throttleResize , {
1818 trailing : false ,
1919 } ) ;
2020
@@ -35,6 +35,18 @@ class ScrollTrigger extends Component {
3535 }
3636 }
3737
38+ componentWillReceiveProps ( nextProps ) {
39+ if ( nextProps . throttleScroll !== this . props . throttleScroll ) {
40+ this . onScroll = throttle ( this . onScroll . bind ( this , nextProps . throttleScroll ) ) ;
41+ addEventListener ( 'scroll' , this . onScroll ) ;
42+ }
43+
44+ if ( nextProps . throttleResize !== this . props . throttleResize ) {
45+ this . onResize = throttle ( this . onResize . bind ( this , nextProps . throttleResize ) ) ;
46+ addEventListener ( 'resize' , this . onResize ) ;
47+ }
48+ }
49+
3850 componentWillUnmount ( ) {
3951 removeEventListener ( 'resize' , this . onResize ) ;
4052 removeEventListener ( 'scroll' , this . onScroll ) ;
@@ -123,29 +135,34 @@ class ScrollTrigger extends Component {
123135 render ( ) {
124136 const {
125137 children,
138+ component,
126139 } = this . props ;
127140
128- return (
129- < div
130- { ...omit ( cleanProps ( this . props ) , [ 'onProgress' ] ) }
131- ref = { ( element ) => {
141+ return React . createElement ( component , {
142+ ...omit ( cleanProps ( this . props ) , [ 'onProgress' ] ) ,
143+ ref : ( element ) => {
132144 this . element = element ;
133- } }
134- >
135- { children }
136- </ div >
145+ } ,
146+ } ,
147+ children ,
137148 ) ;
138149 }
139150}
140151
141152ScrollTrigger . propTypes = {
153+ component : PropTypes . node ,
154+ throttleResize : PropTypes . number ,
155+ throttleScroll : PropTypes . number ,
142156 triggerOnLoad : PropTypes . bool ,
143157 onEnter : PropTypes . func ,
144158 onExit : PropTypes . func ,
145159 onProgress : PropTypes . func ,
146160} ;
147161
148162ScrollTrigger . defaultProps = {
163+ component : 'div' ,
164+ throttleResize : 100 ,
165+ throttleScroll : 100 ,
149166 triggerOnLoad : true ,
150167 onEnter : ( ) => { } ,
151168 onExit : ( ) => { } ,
0 commit comments