Skip to content

Commit 41bb0df

Browse files
committed
Add useIntersectionObserver prop
1 parent e1e3eb3 commit 41bb0df

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

src/components/LazyLoadComponent.jsx

+10-4
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,14 @@ class LazyLoadComponent extends React.Component {
4545
return this.props.children;
4646
}
4747

48-
const { className, delayMethod, delayTime, height, placeholder,
49-
scrollPosition, style, threshold, width } = this.props;
50-
51-
if (this.isScrollTracked || isIntersectionObserverAvailable()) {
48+
const { className, delayMethod, delayTime, height,
49+
placeholder, scrollPosition, style, threshold,
50+
useIntersectionObserver, width } = this.props;
51+
52+
if (
53+
this.isScrollTracked ||
54+
(useIntersectionObserver && isIntersectionObserverAvailable())
55+
) {
5256
return (
5357
<PlaceholderWithoutTracking
5458
className={className}
@@ -80,12 +84,14 @@ class LazyLoadComponent extends React.Component {
8084
LazyLoadComponent.propTypes = {
8185
afterLoad: PropTypes.func,
8286
beforeLoad: PropTypes.func,
87+
useIntersectionObserver: PropTypes.bool,
8388
visibleByDefault: PropTypes.bool,
8489
};
8590

8691
LazyLoadComponent.defaultProps = {
8792
afterLoad: () => ({}),
8893
beforeLoad: () => ({}),
94+
useIntersectionObserver: true,
8995
visibleByDefault: false,
9096
};
9197

src/components/LazyLoadImage.jsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class LazyLoadImage extends React.Component {
3737
getLazyLoadImage(image) {
3838
const { beforeLoad, className, delayMethod, delayTime,
3939
height, placeholder, scrollPosition, style, threshold,
40-
visibleByDefault, width } = this.props;
40+
useIntersectionObserver, visibleByDefault, width } = this.props;
4141

4242
return (
4343
<LazyLoadComponent
@@ -50,6 +50,7 @@ class LazyLoadImage extends React.Component {
5050
scrollPosition={scrollPosition}
5151
style={style}
5252
threshold={threshold}
53+
useIntersectionObserver={useIntersectionObserver}
5354
visibleByDefault={visibleByDefault}
5455
width={width}>
5556
{image}
@@ -107,6 +108,7 @@ LazyLoadImage.propTypes = {
107108
effect: PropTypes.string,
108109
placeholderSrc: PropTypes.string,
109110
threshold: PropTypes.number,
111+
useIntersectionObserver: PropTypes.bool,
110112
visibleByDefault: PropTypes.bool,
111113
wrapperClassName: PropTypes.string,
112114
};
@@ -119,6 +121,7 @@ LazyLoadImage.defaultProps = {
119121
effect: '',
120122
placeholderSrc: '',
121123
threshold: 100,
124+
useIntersectionObserver: true,
122125
visibleByDefault: false,
123126
wrapperClassName: '',
124127
};

src/hoc/trackWindowScroll.js

+10-6
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@ const trackWindowScroll = (BaseComponent) => {
1616
constructor(props) {
1717
super(props);
1818

19-
if (isIntersectionObserverAvailable()) {
19+
this.useIntersectionObserver =
20+
props.useIntersectionObserver && isIntersectionObserverAvailable();
21+
if (this.useIntersectionObserver) {
2022
return;
2123
}
2224

@@ -47,7 +49,7 @@ const trackWindowScroll = (BaseComponent) => {
4749
}
4850

4951
componentDidUpdate() {
50-
if (typeof window === 'undefined' || isIntersectionObserverAvailable()) {
52+
if (typeof window === 'undefined' || this.useIntersectionObserver) {
5153
return;
5254
}
5355

@@ -62,7 +64,7 @@ const trackWindowScroll = (BaseComponent) => {
6264
}
6365

6466
addListeners() {
65-
if (typeof window === 'undefined' || isIntersectionObserverAvailable()) {
67+
if (typeof window === 'undefined' || this.useIntersectionObserver) {
6668
return;
6769
}
6870

@@ -91,7 +93,7 @@ const trackWindowScroll = (BaseComponent) => {
9193
}
9294

9395
removeListeners() {
94-
if (typeof window == 'undefined' || isIntersectionObserverAvailable()) {
96+
if (typeof window == 'undefined' || this.useIntersectionObserver) {
9597
return;
9698
}
9799

@@ -104,7 +106,7 @@ const trackWindowScroll = (BaseComponent) => {
104106
}
105107

106108
onChangeScroll() {
107-
if (isIntersectionObserverAvailable()) {
109+
if (this.useIntersectionObserver) {
108110
return;
109111
}
110112

@@ -118,7 +120,7 @@ const trackWindowScroll = (BaseComponent) => {
118120

119121
render() {
120122
const { delayMethod, delayTime, ...props } = this.props;
121-
const scrollPosition = isIntersectionObserverAvailable() ?
123+
const scrollPosition = this.useIntersectionObserver ?
122124
null : this.state.scrollPosition;
123125

124126
return (
@@ -133,11 +135,13 @@ const trackWindowScroll = (BaseComponent) => {
133135
ScrollAwareComponent.propTypes = {
134136
delayMethod: PropTypes.oneOf(['debounce', 'throttle']),
135137
delayTime: PropTypes.number,
138+
useIntersectionObserver: PropTypes.bool,
136139
};
137140

138141
ScrollAwareComponent.defaultProps = {
139142
delayMethod: 'throttle',
140143
delayTime: 300,
144+
useIntersectionObserver: true,
141145
};
142146

143147
return ScrollAwareComponent;

0 commit comments

Comments
 (0)