All files / src/components/Callout index.jsx

0% Statements 0/103
0% Branches 0/60
0% Functions 0/27
0% Lines 0/58
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 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131                                                                                                                                                                                                                                                                     
import styles from './style.postcss';
 
import React, { PureComponent } from 'react';
import PropTypes from 'prop-types';
import is from 'is_js';
import domAlign from 'dom-align';
import debounce from 'lodash.debounce';
import classnames from 'classnames';
import { CSSTransitionGroup } from 'react-transition-group';
 
const RESIZE_DEBOUNCE_MS = 30;
 
class Callout extends PureComponent {
  constructor() {
    super();
    this.state = { open: false };
    this._onClick = this._onClick.bind(this);
    this._onClickedOutside = this._onClickedOutside.bind(this);
  }
 
  componentDidMount() {
    this._updatePosition();
    document.body.addEventListener('click', this._onClickedOutside);
    document.body.addEventListener('touchstart', this._onClickedOutside);
    window.addEventListener('resize', this._onResizeHandler = debounce(() => {
      this._updatePosition();
    }, RESIZE_DEBOUNCE_MS, {
      leading: true,  // nicer iOS screen rotate
      trailing: true,
    }), false);
  }
 
  componentDidUpdate() {
    this._updatePosition();
  }
 
  componentWillUnmount() {
    document.body.removeEventListener('click', this._onClickedOutside);
    document.body.removeEventListener('touchstart', this._onClickedOutside);
    window.removeEventListener('resize', this._onResizeHandler);
  }
 
  _updatePosition() {
    if (! this._calloutNode) return;
    const { points, offset } = this.props;
    const notchPoints = ['tl', 'bl'];
    domAlign(this._notchNode, this._node.children[0], {
      points: notchPoints,
      offset: [0, 15],
      useCssTransform: true,
    });
    domAlign(this._calloutNode, this._node.children[0], {
      points,
      offset,
      useCssTransform: true,
    });
  }
 
  _onClickedOutside(evt) {
    if ((this._node && this._node.contains(evt.target)) ||
        (this._calloutNode && this._calloutNode.contains(evt.target))) {
      return;
    }
    const { onOpenStateChanged } = this.props;
    this.setState({ open: false }, () => {
      onOpenStateChanged && onOpenStateChanged(false);
    });
  }
 
  _onClick() {
    const newOpen = ! this.state.open;
    const { onOpenStateChanged } = this.props;
    this.setState({ open: newOpen }, () => {
      onOpenStateChanged && onOpenStateChanged(newOpen);
    });
  }
 
  close() {
    if (! this.state.open) return;
    this.setState({ open: false });
  }
 
  render() {
    const { popupClassName, content, children } = this.props;
    const newContent = React.cloneElement(content, {
      className: classnames(styles.Callout_popup, popupClassName),
      ref: (node) => { this._calloutNode = node; },
      key: 'Callout#popup',
    });
    return <div className={styles.Callout}
        ref={(node) => { this._node = node; }}>
      <div className={styles.Callout_trigger}
          onClick={this._onClick}
          role="button"
          tabIndex="0">
        {children}
      </div>
      <CSSTransitionGroup transitionName="popup"
          transitionEnterTimeout={200}
          transitionLeaveTimeout={200}>
        {this.state.open &&
          <div className={styles.Callout_notch}
              ref={(node) => { this._notchNode = node; }} /> }
        {this.state.open && newContent}
      </CSSTransitionGroup>
    </div>;
  }
}
 
Callout.defaultProps = {
  points: ['tr', 'bc'],
  offset: [65, 25],
};
 
Callout.propTypes = {
  popupClassName: PropTypes.string,
  children: PropTypes.node,
  content: PropTypes.node,
  points: PropTypes.arrayOf((arr, key) => arr.every((val) => {
    if (is.string(val) && val.length === 2 && key < 2) return true;
    return new Error(`'points' should use the format required by dom-align. got ${val}`);
  })),
  offset: PropTypes.arrayOf((arr, key) => arr.every((val) => {
    if (is.number(val) && key < 2) return true;
    return new Error(`'offset' should use the format required by dom-align. got ${val}`);
  })),
  onOpenStateChanged: PropTypes.func,
};
 
export default Callout;