forked from okonet/react-scroll-sync
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScrollSyncPane.js
More file actions
60 lines (48 loc) · 1.42 KB
/
ScrollSyncPane.js
File metadata and controls
60 lines (48 loc) · 1.42 KB
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
/* eslint react/no-find-dom-node: 0 */
import { Component } from 'react'
import ReactDOM from 'react-dom'
import PropTypes from 'prop-types'
/**
* ScrollSyncPane Component
*
* Wrap your content in it to keep its scroll position in sync with other panes
*
* @example ./example.md
*/
export default class ScrollSyncPane extends Component {
static propTypes = {
children: PropTypes.node.isRequired,
attachTo: PropTypes.object,
group: PropTypes.oneOfType([PropTypes.string, PropTypes.arrayOf(PropTypes.string)]),
enabled: PropTypes.bool
}
static defaultProps = {
group: 'default',
enabled: true
}
static contextTypes = {
registerPane: PropTypes.func,
unregisterPane: PropTypes.func
}
componentDidMount() {
if (this.props.enabled) {
this.node = this.props.attachTo || ReactDOM.findDOMNode(this)
this.context.registerPane(this.node, this.toArray(this.props.group))
}
}
componentWillReceiveProps(nextProps) {
if (this.props.enabled && this.props.group !== nextProps.group) {
this.context.unregisterPane(this.node, this.toArray(this.props.group))
this.context.registerPane(this.node, this.toArray(nextProps.group))
}
}
componentWillUnmount() {
if (this.props.enabled) {
this.context.unregisterPane(this.node, this.toArray(this.props.group))
}
}
toArray = groups => [].concat(groups)
render() {
return this.props.children
}
}