You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

62 lines
1.5 KiB

  1. import React from 'react';
  2. import PropTypes from 'prop-types';
  3. import { supportsPassiveEvents } from 'detect-passive-events';
  4. import { scrollTop } from '../scroll';
  5. export default class Column extends React.PureComponent {
  6. static propTypes = {
  7. children: PropTypes.node,
  8. label: PropTypes.string,
  9. bindToDocument: PropTypes.bool,
  10. };
  11. scrollTop () {
  12. const scrollable = this.props.bindToDocument ? document.scrollingElement : this.node.querySelector('.scrollable');
  13. if (!scrollable) {
  14. return;
  15. }
  16. this._interruptScrollAnimation = scrollTop(scrollable);
  17. }
  18. handleWheel = () => {
  19. if (typeof this._interruptScrollAnimation !== 'function') {
  20. return;
  21. }
  22. this._interruptScrollAnimation();
  23. }
  24. setRef = c => {
  25. this.node = c;
  26. }
  27. componentDidMount () {
  28. if (this.props.bindToDocument) {
  29. document.addEventListener('wheel', this.handleWheel, supportsPassiveEvents ? { passive: true } : false);
  30. } else {
  31. this.node.addEventListener('wheel', this.handleWheel, supportsPassiveEvents ? { passive: true } : false);
  32. }
  33. }
  34. componentWillUnmount () {
  35. if (this.props.bindToDocument) {
  36. document.removeEventListener('wheel', this.handleWheel);
  37. } else {
  38. this.node.removeEventListener('wheel', this.handleWheel);
  39. }
  40. }
  41. render () {
  42. const { label, children } = this.props;
  43. return (
  44. <div role='region' aria-label={label} className='column' ref={this.setRef}>
  45. {children}
  46. </div>
  47. );
  48. }
  49. }