2017-06-03 16:39:38 -07:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-11-04 09:21:05 -08:00
|
|
|
import { supportsPassiveEvents } from 'detect-passive-events';
|
2022-10-11 02:39:52 -07:00
|
|
|
import { scrollTop } from '../scroll';
|
2017-06-03 16:39:38 -07:00
|
|
|
|
2023-05-22 06:48:01 -07:00
|
|
|
const listenerOptions = supportsPassiveEvents ? { passive: true } : false;
|
|
|
|
|
2017-06-23 10:36:54 -07:00
|
|
|
export default class Column extends React.PureComponent {
|
2017-06-03 16:39:38 -07:00
|
|
|
|
|
|
|
static propTypes = {
|
|
|
|
children: PropTypes.node,
|
2017-07-30 09:36:28 -07:00
|
|
|
extraClasses: PropTypes.string,
|
2017-08-06 12:27:47 -07:00
|
|
|
name: PropTypes.string,
|
2018-08-28 03:01:04 -07:00
|
|
|
label: PropTypes.string,
|
2019-08-01 10:17:17 -07:00
|
|
|
bindToDocument: PropTypes.bool,
|
2017-06-03 16:39:38 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
scrollTop () {
|
2019-08-01 10:17:17 -07:00
|
|
|
const scrollable = this.props.bindToDocument ? document.scrollingElement : this.node.querySelector('.scrollable');
|
2017-06-03 16:39:38 -07:00
|
|
|
|
|
|
|
if (!scrollable) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._interruptScrollAnimation = scrollTop(scrollable);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleWheel = () => {
|
|
|
|
if (typeof this._interruptScrollAnimation !== 'function') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._interruptScrollAnimation();
|
2023-02-03 11:52:07 -08:00
|
|
|
};
|
2017-06-03 16:39:38 -07:00
|
|
|
|
|
|
|
setRef = c => {
|
|
|
|
this.node = c;
|
2023-02-03 11:52:07 -08:00
|
|
|
};
|
2017-06-03 16:39:38 -07:00
|
|
|
|
2017-07-24 16:05:51 -07:00
|
|
|
componentDidMount () {
|
2019-08-01 10:17:17 -07:00
|
|
|
if (this.props.bindToDocument) {
|
2023-05-22 06:48:01 -07:00
|
|
|
document.addEventListener('wheel', this.handleWheel, listenerOptions);
|
2019-08-01 10:17:17 -07:00
|
|
|
} else {
|
2023-05-22 06:48:01 -07:00
|
|
|
this.node.addEventListener('wheel', this.handleWheel, listenerOptions);
|
2019-08-01 10:17:17 -07:00
|
|
|
}
|
2017-07-24 16:05:51 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount () {
|
2019-08-01 10:17:17 -07:00
|
|
|
if (this.props.bindToDocument) {
|
2023-05-22 06:48:01 -07:00
|
|
|
document.removeEventListener('wheel', this.handleWheel, listenerOptions);
|
2019-08-01 10:17:17 -07:00
|
|
|
} else {
|
2023-05-22 06:48:01 -07:00
|
|
|
this.node.removeEventListener('wheel', this.handleWheel, listenerOptions);
|
2019-08-01 10:17:17 -07:00
|
|
|
}
|
2017-07-24 16:05:51 -07:00
|
|
|
}
|
|
|
|
|
2017-06-03 16:39:38 -07:00
|
|
|
render () {
|
2018-08-28 03:01:04 -07:00
|
|
|
const { children, extraClasses, name, label } = this.props;
|
2017-06-03 16:39:38 -07:00
|
|
|
|
|
|
|
return (
|
2018-08-28 03:01:04 -07:00
|
|
|
<div role='region' aria-label={label} data-column={name} className={`column ${extraClasses || ''}`} ref={this.setRef}>
|
2017-06-03 16:39:38 -07:00
|
|
|
{children}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|