-
Notifications
You must be signed in to change notification settings - Fork 44
/
ScrollElement.jsx
133 lines (123 loc) · 4.21 KB
/
ScrollElement.jsx
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
import React from 'react';
import ReactDOM from 'react-dom';
import PropTypes from 'prop-types';
import EventListener from './EventDispatcher';
import { noop, currentScrollTop, transformArguments, windowHeight, windowIsUndefined } from './util';
class ScrollElement extends React.Component {
static propTypes = {
component: PropTypes.any,
playScale: PropTypes.any,
id: PropTypes.string,
onChange: PropTypes.func,
onScroll: PropTypes.func,
location: PropTypes.string,
targetId: PropTypes.string,
replay: PropTypes.bool,
componentProps: PropTypes.object,
}
static defaultProps = {
component: 'div',
onChange: noop,
onScroll: noop,
playScale: 0.5,
replay: false,
componentProps: {},
}
static getDerivedStateFromProps(props, { prevProps, $self }) {
const nextState = {
prevProps: props,
};
if (prevProps && props !== prevProps) {
$self.scrollEventListener();
}
return nextState; // eslint-disable-line
}
constructor(props) {
super(props);
this.state = {
$self: this,
};
}
componentDidMount() {
if (windowIsUndefined) {
return;
}
this.dom = ReactDOM.findDOMNode(this);
const date = Date.now();
this.target = this.props.targetId && document.getElementById(this.props.targetId);
const length = EventListener._listeners.scroll ? EventListener._listeners.scroll.length : 0;
this.eventType = `scroll.scrollEvent${date}${length}`;
this.addScrollEvent();
}
componentWillUnmount() {
EventListener.removeEventListener(this.eventType, this.scrollEventListener, this.target);
}
getParam = (e) => {
this.clientHeight = this.target ? this.target.clientHeight : windowHeight();
const scrollTop = this.target ? this.target.scrollTop : currentScrollTop();
const domRect = this.dom.getBoundingClientRect();
const targetTop = this.target ? this.target.getBoundingClientRect().top : 0;
const offsetTop = domRect.top + scrollTop - targetTop;
this.elementShowHeight = scrollTop - offsetTop + this.clientHeight;
const playScale = transformArguments(this.props.playScale);
const playScaleEnterArray = /([\+\-]?[0-9#\.]+)(px|vh|%)?/.exec(String(playScale[0]));// eslint-disable-line
if (!playScaleEnterArray[2]) {
this.playHeight = this.clientHeight * parseFloat(playScale[0]);
} else if (playScaleEnterArray[2] === 'px') {
this.playHeight = parseFloat(playScaleEnterArray[1]);
} else {
this.playHeight = this.clientHeight * parseFloat(playScaleEnterArray[1]) / 100;
}
const leaveHeight = domRect.height;
const playScaleLeaveArray = /([\+\-]?[0-9#\.]+)(px|vh|%)?/.exec(String(playScale[1]));// eslint-disable-line
if (!playScaleLeaveArray[2]) {
this.leavePlayHeight = leaveHeight * parseFloat(playScale[1]);
} else if (playScaleLeaveArray[2] === 'px') {
this.leavePlayHeight = parseFloat(playScaleLeaveArray[1]);
} else {
this.leavePlayHeight = leaveHeight * parseFloat(playScaleLeaveArray[1]) / 100;
}
const enter = this.props.replay ? this.elementShowHeight >= this.playHeight
&& this.elementShowHeight <= this.clientHeight + this.leavePlayHeight :
this.elementShowHeight >= this.playHeight;
const enterOrLeave = enter ? 'enter' : 'leave';
const mode = this.enter !== enter || typeof this.enter !== 'boolean' ? enterOrLeave : null;
if (mode) {
this.props.onChange({ mode, id: this.props.id });
}
this.props.onScroll({
domEvent: e,
scrollTop,
showHeight: this.elementShowHeight,
offsetTop,
id: this.props.id,
});
this.enter = enter;
}
addScrollEvent = () => {
EventListener.addEventListener(this.eventType, this.scrollEventListener, this.target);
const scrollTop = currentScrollTop();
if (!scrollTop) {
this.scrollEventListener();
}
}
scrollEventListener = (e) => {
this.getParam(e);
}
render() {
const {
component,
playScale,
location,
targetId,
onScroll,
onChange,
replay,
componentProps,
...props
} = this.props;
return React.createElement(component, { ...props, ...componentProps });
}
}
ScrollElement.isScrollElement = true;
export default ScrollElement;