Skip to content

Commit f3471d8

Browse files
committed
Add component and throttle props to allow for more configuration of component
1 parent 9e32e39 commit f3471d8

File tree

1 file changed

+28
-11
lines changed

1 file changed

+28
-11
lines changed

src/index.js

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import React, { Component } from 'react';
22
import PropTypes from 'prop-types';
33
import ReactDOM from 'react-dom';
4-
import omit from 'lodash.omit';
4+
import omit from 'lomit';
55
import throttle from 'lodash.throttle';
66
import 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

141152
ScrollTrigger.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

148162
ScrollTrigger.defaultProps = {
163+
component: 'div',
164+
throttleResize: 100,
165+
throttleScroll: 100,
149166
triggerOnLoad: true,
150167
onEnter: () => {},
151168
onExit: () => {},

0 commit comments

Comments
 (0)