forked from civiccc/react-waypoint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
152 lines (128 loc) · 4.16 KB
/
index.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
var PropTypes = React.PropTypes;
/**
* Calls a function when you scroll to the element.
*/
var Waypoint = React.createClass({
propTypes: {
onEnter: PropTypes.func,
onLeave: PropTypes.func,
// threshold is percentage of the height of the visible part of the
// scrollable parent (e.g. 0.1)
threshold: PropTypes.number,
},
_wasVisible: false,
/**
* @return {Object}
*/
getDefaultProps: function() {
return {
threshold: 0,
onEnter: function() {},
onLeave: function() {},
};
},
componentDidMount: function() {
this.scrollableParent = this._findScrollableParent();
this.scrollableParent.addEventListener('scroll', this._handleScroll);
window.addEventListener('resize', this._handleScroll);
this._handleScroll();
},
componentDidUpdate: function() {
// The element may have moved.
this._handleScroll();
},
componentWillUnmount: function() {
if (this.scrollableParent) {
// At the time of unmounting, the scrollable parent might no longer exist.
// Guarding against this prevents the following error:
//
// Cannot read property 'removeEventListener' of undefined
this.scrollableParent.removeEventListener('scroll', this._handleScroll);
window.removeEventListener('resize', this._handleScroll);
}
},
/**
* Traverses up the DOM to find a parent container which has an overflow style
* that allows for scrolling.
*
* @return {Object} the closest parent element with an overflow style that
* allows for scrolling. If none is found, the `window` object is returned
* as a fallback.
*/
_findScrollableParent: function() {
var node = this.getDOMNode();
while (node.parentNode) {
node = node.parentNode;
if (node === document) {
// This particular node does not have a computed style.
continue;
}
var style = window.getComputedStyle(node);
var overflowY = style.getPropertyValue('overflow-y') ||
style.getPropertyValue('overflow');
if (overflowY === 'auto' || overflowY === 'scroll') {
return node;
}
}
// A scrollable parent element was not found, which means that we need to do
// stuff on window.
return window;
},
_handleScroll: function() {
var isVisible = this._isVisible();
if (this._wasVisible === isVisible) {
// No change since last trigger
return;
}
if (isVisible) {
this.props.onEnter();
} else {
this.props.onLeave();
}
this._wasVisible = isVisible;
},
/**
* @param {Object} node
* @return {Number}
*/
_distanceToTopOfScrollableParent: function(node) {
if (this.scrollableParent !== window && !node.offsetParent) {
throw new Error(
'The scrollable parent of Waypoint needs to have positioning to ' +
'properly determine position of Waypoint (e.g. `position: relative;`)'
);
}
if (node.offsetParent === this.scrollableParent || !node.offsetParent) {
return node.offsetTop;
} else {
return node.offsetTop + this._distanceToTopOfScrollableParent(node.offsetParent);
}
},
/**
* @return {boolean} true if scrolled down almost to the end of the scrollable
* parent element.
*/
_isVisible: function() {
var waypointTop = this._distanceToTopOfScrollableParent(this.getDOMNode());
var contextHeight, contextScrollTop;
if (this.scrollableParent === window) {
contextHeight = window.innerHeight;
contextScrollTop = window.pageYOffset;
} else {
contextHeight = this.scrollableParent.offsetHeight;
contextScrollTop = this.scrollableParent.scrollTop;
}
var thresholdPx = contextHeight * this.props.threshold;
var isAboveBottom = contextScrollTop + contextHeight >= waypointTop - thresholdPx;
var isBelowTop = contextScrollTop <= waypointTop + thresholdPx;
return isAboveBottom && isBelowTop;
},
/**
* @return {Object}
*/
render: function() {
// We need an element that we can locate in the DOM to determine where it is
// rendered relative to the top of its context.
return React.createElement('span', { style: { fontSize: 0 } });
}
});