-
Notifications
You must be signed in to change notification settings - Fork 295
/
BackToTopButton.tsx
122 lines (106 loc) · 3.03 KB
/
BackToTopButton.tsx
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
// @flow
import React, { Component } from 'react';
import classnames from 'classnames';
import { defineMessages, intlShape } from 'react-intl';
import { throttle } from 'lodash';
import styles from './BackToTopButton.scss';
const messages = defineMessages({
backToTopLabel: {
id: 'backToTopButton.label',
defaultMessage: '!!!Back to top',
description: '"backToTop" button label.',
},
});
type Props = {
scrollableElementClassName: string,
buttonTopPosition: number,
scrollTopToActivate: number,
isForceHidden?: boolean,
};
type State = {
isActive: boolean,
};
export default class BackToTopButton extends Component<Props, State> {
static contextTypes = {
intl: intlShape.isRequired,
};
static defaultProps = {
scrollTopToActivate: 20,
buttonTopPosition: 20,
isForceHidden: false,
};
state = {
isActive: false,
};
_isMounted = false;
scrollableDomElement: ?HTMLElement = null;
componentDidMount() {
this._isMounted = true;
setTimeout(() => {
if (this._isMounted) {
this.scrollableDomElement = document.querySelector(
`.${this.props.scrollableElementClassName}`
);
if (!this.scrollableDomElement) return false;
return this.scrollableDomElement.addEventListener(
'scroll',
throttle(this.getIsBackToTopActive, 300, {
leading: false,
trailing: true,
})
);
}
return null;
}, 0);
}
componentWillUnmount() {
if (this._isMounted) {
this._isMounted = false;
this.scrollableDomElement = document.querySelector(
`.${this.props.scrollableElementClassName}`
);
if (!this.scrollableDomElement) return false;
return this.scrollableDomElement.removeEventListener(
'scroll',
this.getIsBackToTopActive
);
}
return null;
}
getIsBackToTopActive = () => {
const { isActive } = this.state;
const { scrollTopToActivate } = this.props;
if (this.scrollableDomElement instanceof HTMLElement && this._isMounted) {
const scrollPosition = this.scrollableDomElement.scrollTop;
if (scrollPosition > scrollTopToActivate && !isActive) {
this.setState({ isActive: true });
} else if (scrollPosition <= scrollTopToActivate && isActive) {
this.setState({ isActive: false });
}
}
};
backToTop = () => {
if (this.scrollableDomElement instanceof HTMLElement) {
this.scrollableDomElement.scrollTop = 0;
}
};
render() {
const { intl } = this.context;
const { isActive } = this.state;
const { buttonTopPosition, isForceHidden } = this.props;
const componentStyles = classnames(styles.component, {
[styles.isActive]: isActive,
});
const top = isActive ? buttonTopPosition : buttonTopPosition - 10;
if (isForceHidden) return null;
return (
<button
style={{ top }}
className={componentStyles}
onClick={this.backToTop}
>
{intl.formatMessage(messages.backToTopLabel)}
</button>
);
}
}