All files / src/px-drawer index.js

31.82% Statements 14/44
40.91% Branches 9/22
21.43% Functions 3/14
31.82% Lines 14/44
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 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156                            1x 1x                                                       1x 1x     1x                                                                                                                       1x   1x   1x 1x           1x         1x               1x                           2x         2x          
import React from 'react';
import PropTypes from 'prop-types';
import classnames from 'classnames';
 
 
import stylesheet from './style.scss';
 
 
 
/**
 * px-drawer component
 */
export default class Drawer extends React.Component {
  constructor(props){
    super(props);
    this.state = {
      open: props.open || false,
      docked: props.docked || false
    };
  }
 
  isOpen(){
    return this.state.open;
  }
 
  toggle(){
    if(this.isOpen()){
      this._close();
    } else {
      this._open();
    }
  }
 
  _open(){
    this.setState({open: true});
  }
 
  _close(){
    this.setState({open: false});
    this.sideNavContent.style.transform = '';
  }
 
  componentDidMount() {
    this.hasUnprefixedTransform = 'transform' in document.documentElement.style;
    Iif (this.hasUnprefixedTransform) {
      this._setupTouchHandlers();
    }
    Iif(this.sideNavContent){
      this.sideNavContent.addEventListener('click', (e) =>{
        if(!this.state.docked){
          this._close();
        }
      });
    }
 
  }
 
  componentWillReceiveProps(nextProps){
    this.setState(nextProps);
  }
 
  _setupTouchHandlers() {
    this.touchStartX = null;
    this.sideNavTransform = null;
    if(this.sideNavContent){
      this.sideNavContent.addEventListener('touchstart', this.onSideNavTouchStart.bind(this));
      this.sideNavContent.addEventListener('touchmove', this.onSideNavTouchMove.bind(this));
      this.sideNavContent.addEventListener('touchend', this.onSideNavTouchEnd.bind(this));  
    }
 
  }
 
  onSideNavTouchStart(e) {
    e.preventDefault();
    this.touchStartX = e.touches[0].pageX;
  }
 
  onSideNavTouchMove(e) {
    e.preventDefault();
    const newTouchX = e.touches[0].pageX;
    this.sideNavTransform = Math.min(0, newTouchX - this.touchStartX);
    this.sideNavContent.style.transform = 'translateX(' + this.sideNavTransform + 'px)';
  }
 
  onSideNavTouchEnd(e) {
    let TOUCH_SLOP = 8 * window.devicePixelRatio * 3;
    if (this.sideNavTransform < -TOUCH_SLOP) {
      this._close();
      return;
    }
    this._open();
  }
 
  render(){
    const {
      style,
      className = 'drawer',
      containerClassName,
      overlay,
      //docked,
      overlayClassName = 'drawer__overlay',
      overlayStyle,
      onOverlayClick,
 
      type = 'persistent',
      align = 'left',
      children
    } = this.props;
 
    const { open, docked } = this.state;
 
    const baseClassName = 'px-drawer';
    const baseClasses = classnames(
      baseClassName,
      {[`${baseClassName}--is-docked`]: docked},
      {[`${baseClassName}--is-open`]: open}
    );
 
    const overlayClasses = classnames(
      overlayClassName,
      {[`${overlayClassName}--is-open`]: (open) }
    );
 
    const drawerClasses = classnames(
      className,
      { [`${className}--${type}`]: true },
      { [`${className}--${align}`]: true },
      {[`${className}--is-open`]: open},
      {[`${className}--is-docked`]: docked},
    );
 
    return (
      <div className={baseClasses}>
        {!docked && <div id="overlay" style={overlayStyle} onClick={onOverlayClick} className={overlayClasses}></div>}
        <div id="drawer" className={drawerClasses} style={style} ref={(e) => {this.sideNavContent = e;}}>
          <div id="drawerContent" className="drawer__content">
            <div>{children}</div>
          </div>
        </div>
        <style jsx>{stylesheet}</style>
      </div>
    );
  }
}
 
Drawer.propTypes = {
  open: PropTypes.bool,
  docked: PropTypes.bool
};
 
Drawer.defaultProps = {
  opened: false,
  docked: true,
  align: 'left'
};