-
Notifications
You must be signed in to change notification settings - Fork 67
/
IntersectionObserver.js
187 lines (160 loc) · 6.02 KB
/
IntersectionObserver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import React from 'react';
import { findDOMNode } from 'react-dom';
import PropTypes from 'prop-types';
import { createObserver, observeElement, unobserveElement } from './observer';
import { shallowCompare } from './utils';
import Config from './config';
const observerOptions = ['root', 'rootMargin', 'threshold'];
const observableProps = ['root', 'rootMargin', 'threshold', 'disabled'];
const { hasOwnProperty, toString } = Object.prototype;
const getOptions = props => {
return observerOptions.reduce((options, key) => {
if (hasOwnProperty.call(props, key)) {
const rootIsString =
key === 'root' &&
toString.call(props[key]) === '[object String]';
options[key] = rootIsString
? document.querySelector(props[key])
: props[key];
}
return options;
}, {});
};
class IntersectionObserver extends React.Component {
static displayName = 'IntersectionObserver';
static propTypes = {
/**
* The element that is used as the target to observe.
*/
children: PropTypes.element,
/**
* The element that is used as the viewport for checking visibility of the target.
* Can be specified as string for selector matching within the document.
* Defaults to the browser viewport if not specified or if null.
*/
root: PropTypes.oneOfType(
[PropTypes.string].concat(
typeof HTMLElement === 'undefined'
? []
: PropTypes.instanceOf(HTMLElement)
)
),
/**
* Margin around the root. Can have values similar to the CSS margin property,
* e.g. "10px 20px 30px 40px" (top, right, bottom, left).
* If the root element is specified, the values can be percentages.
* This set of values serves to grow or shrink each side of the root element's
* bounding box before computing intersections.
* Defaults to all zeros.
*/
rootMargin: PropTypes.string,
/**
* Either a single number or an array of numbers which indicate at what percentage
* of the target's visibility the observer's callback should be executed.
* If you only want to detect when visibility passes the 50% mark, you can use a value of 0.5.
* If you want the callback run every time visibility passes another 25%,
* you would specify the array [0, 0.25, 0.5, 0.75, 1].
* The default is 0 (meaning as soon as even one pixel is visible, the callback will be run).
* A value of 1.0 means that the threshold isn't considered passed until every pixel is visible.
*/
threshold: PropTypes.oneOfType([
PropTypes.number,
PropTypes.arrayOf(PropTypes.number),
]),
/**
* Controls whether the element should stop being observed by its IntersectionObserver instance.
* Defaults to false.
*/
disabled: PropTypes.bool,
/**
* Function that will be invoked whenever the intersection value for this element changes.
*/
onChange: PropTypes.func.isRequired,
};
handleChange = event => {
this.props.onChange(event, this.externalUnobserve);
};
handleNode = target => {
const { children } = this.props;
if (children != null) {
/**
* Forward hijacked ref to user.
*/
const nodeRef = children.ref;
if (nodeRef) {
if (typeof nodeRef === 'function') {
nodeRef(target);
} else if (typeof nodeRef === 'object') {
nodeRef.current = target;
}
}
}
this.targetNode = target && findDOMNode(target);
};
observe = () => {
if (this.props.children == null || this.props.disabled) {
return false;
}
if (!this.targetNode) {
Config.errorReporter(
"ReactIntersectionObserver: Can't find DOM node in the provided children. Make sure to render at least one DOM node in the tree."
);
return false;
}
this.observer = createObserver(getOptions(this.props));
this.target = this.targetNode;
observeElement(this);
return true;
};
unobserve = target => {
unobserveElement(this, target);
};
externalUnobserve = () => {
this.unobserve(this.targetNode);
};
getSnapshotBeforeUpdate(prevProps) {
this.prevTargetNode = this.targetNode;
const relatedPropsChanged = observableProps.some(prop =>
shallowCompare(this.props[prop], prevProps[prop])
);
if (relatedPropsChanged) {
if (this.prevTargetNode) {
if (!prevProps.disabled) {
this.unobserve(this.prevTargetNode);
}
}
}
return relatedPropsChanged;
}
componentDidUpdate(_, __, relatedPropsChanged) {
let targetNodeChanged = false;
// check if we didn't unobserve previously due to a prop change
if (!relatedPropsChanged) {
targetNodeChanged = this.prevTargetNode !== this.targetNode;
// check we have a previous node we want to unobserve
if (targetNodeChanged && this.prevTargetNode != null) {
this.unobserve(this.prevTargetNode);
}
}
if (relatedPropsChanged || targetNodeChanged) {
this.observe();
}
}
componentDidMount() {
this.observe();
}
componentWillUnmount() {
if (this.targetNode) {
this.unobserve(this.targetNode);
}
}
render() {
const { children } = this.props;
return children != null
? React.cloneElement(React.Children.only(children), {
ref: this.handleNode,
})
: null;
}
}
export { IntersectionObserver as default, getOptions };