2017-06-04 00:39:38 +01:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2017-07-25 00:05:51 +01:00
|
|
|
import detectPassiveEvents from 'detect-passive-events';
|
2017-08-04 17:57:46 +01:00
|
|
|
import { scrollTop } from '../scroll';
|
2017-06-04 00:39:38 +01:00
|
|
|
|
2017-06-23 18:36:54 +01:00
|
|
|
export default class Column extends React.PureComponent {
|
2017-06-04 00:39:38 +01:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
children: PropTypes.node,
|
2018-08-23 16:26:21 +01:00
|
|
|
label: PropTypes.string,
|
2019-08-01 18:17:17 +01:00
|
|
|
bindToDocument: PropTypes.bool,
|
2017-06-04 00:39:38 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
scrollTop () {
|
2019-08-01 18:17:17 +01:00
|
|
|
const scrollable = this.props.bindToDocument ? document.scrollingElement : this.node.querySelector('.scrollable');
|
2017-06-04 00:39:38 +01:00
|
|
|
|
|
|
|
if (!scrollable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._interruptScrollAnimation = scrollTop(scrollable);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleWheel = () => {
|
|
|
|
if (typeof this._interruptScrollAnimation !== 'function') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._interruptScrollAnimation();
|
|
|
|
}
|
|
|
|
|
|
|
|
setRef = c => {
|
|
|
|
this.node = c;
|
|
|
|
}
|
|
|
|
|
2017-07-25 00:05:51 +01:00
|
|
|
componentDidMount () {
|
2019-08-01 18:17:17 +01:00
|
|
|
if (this.props.bindToDocument) {
|
|
|
|
document.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
|
|
|
|
} else {
|
|
|
|
this.node.addEventListener('wheel', this.handleWheel, detectPassiveEvents.hasSupport ? { passive: true } : false);
|
|
|
|
}
|
2017-07-25 00:05:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
2019-08-01 18:17:17 +01:00
|
|
|
if (this.props.bindToDocument) {
|
|
|
|
document.removeEventListener('wheel', this.handleWheel);
|
|
|
|
} else {
|
|
|
|
this.node.removeEventListener('wheel', this.handleWheel);
|
|
|
|
}
|
2017-07-25 00:05:51 +01:00
|
|
|
}
|
|
|
|
|
2017-06-04 00:39:38 +01:00
|
|
|
render () {
|
2018-08-23 16:26:21 +01:00
|
|
|
const { label, children } = this.props;
|
2017-06-04 00:39:38 +01:00
|
|
|
|
|
|
|
return (
|
2018-08-23 16:26:21 +01:00
|
|
|
<div role='region' aria-label={label} className='column' ref={this.setRef}>
|
2017-06-04 00:39:38 +01:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|