~cytrogen/masto-fe

ref: 95b389e89d984644f986bf984f9bc38080f6fb32 masto-fe/app/javascript/flavours/glitch/components/column.jsx -rw-r--r-- 2.0 KiB
95b389e8 — Cytrogen Convert mastodon flavour SCSS to CSS custom properties (Phase 7) 15 days ago
                                                                                
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
import PropTypes from "prop-types";
import { PureComponent } from "react";

import { supportsPassiveEvents } from "detect-passive-events";

import { scrollTop } from "../scroll";

const listenerOptions = supportsPassiveEvents ? { passive: true } : false;

export default class Column extends PureComponent {

  static propTypes = {
    children: PropTypes.node,
    extraClasses: PropTypes.string,
    name: PropTypes.string,
    label: PropTypes.string,
    bindToDocument: PropTypes.bool,
  };

  scrollTop () {
    let scrollable = null;

    if (this.props.bindToDocument) {
      scrollable = document.scrollingElement;
    } else {
      scrollable = this.node.querySelector(".scrollable");

      // Some columns have nested `.scrollable` containers, with the outer one
      // being a wrapper while the actual scrollable content is deeper.
      if (scrollable.classList.contains("scrollable--flex")) {
        scrollable = scrollable?.querySelector(".scrollable") || scrollable;
      }
    }

    if (!scrollable) {
      return;
    }

    this._interruptScrollAnimation = scrollTop(scrollable);
  }

  handleWheel = () => {
    if (typeof this._interruptScrollAnimation !== "function") {
      return;
    }

    this._interruptScrollAnimation();
  };

  setRef = c => {
    this.node = c;
  };

  componentDidMount () {
    if (this.props.bindToDocument) {
      document.addEventListener("wheel", this.handleWheel, listenerOptions);
    } else {
      this.node.addEventListener("wheel", this.handleWheel, listenerOptions);
    }
  }

  componentWillUnmount () {
    if (this.props.bindToDocument) {
      document.removeEventListener("wheel", this.handleWheel, listenerOptions);
    } else {
      this.node.removeEventListener("wheel", this.handleWheel, listenerOptions);
    }
  }

  render () {
    const { children, extraClasses, name, label } = this.props;

    return (
      <div role='region' aria-label={label} data-column={name} className={`column ${extraClasses || ""}`} ref={this.setRef}>
        {children}
      </div>
    );
  }

}