All files / lib/EventOverlay index.js

64.54% Statements 182/282
50.69% Branches 147/290
87.88% Functions 29/33
64.71% Lines 176/272
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684                          118x       287x                 78x 78x 209x         35x         2x       196x 196x 87x   18x 18x 18x 18x 18x 18x 18x   18x           66x           46x 92x   92x             20x 20x   20x 46x   46x     46x     20x               2x   2x 2x 2x 2x     2x 2x   2x 2x   2x         2x 2x                 16x   16x 16x 16x 16x     16x 16x   16x 16x   16x         16x 16x         118x 118x 118x   118x 111x     118x 118x 118x   118x       78x   78x 77x     78x                     35x       2x 2x   2x 2x                                                         21x 13x   13x                 81x   81x             81x   81x   81x   81x     81x       81x                     81x 81x 81x         81x 73x 73x   73x 73x     81x 8x 8x   8x 8x     81x       47x 47x 47x 47x 47x 47x 47x 47x   47x               47x               47x   10x 10x 10x   31x 31x 31x   2x 2x 2x     4x 4x 4x         2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x   2x 2x 2x 2x 2x 2x   2x       2x       2x           2x                                                                                                                                                                                             2x 2x     2x     2x                                         2x                     87x 87x 87x   87x 87x 87x 87x 87x   87x   81x 81x 81x   81x   81x 81x   81x                     81x 81x   81x   81x       13x                         405x 405x   405x                         405x                   211x           442x                         405x         89x                                         89x                                                                                                    
import React from 'react';
import PropTypes from 'prop-types';
import ReactDOM from 'react-dom';
import { omit } from 'lodash';
 
export default class EventOverlay extends React.Component {
  static displayName = 'EventOverlay';
 
  state = {
    visibleDirection: this.props.direction,
  };
 
  componentDidMount = () => {
    this.addHandlers();
  };
 
  componentDidUpdate = prevProps => {
    if (
      (
        this.props.isOpen
        &&
        prevProps.isOpen !== this.props.isOpen
      )
      ||
      prevProps.direction !== this.props.direction
    ) {
      this.addKeyHandlers();
      return this.forceUpdate(() => this.isVisible());
    } else if (
      !this.props.isOpen
      &&
      prevProps.isOpen !== this.props.isOpen
    ) {
      return this.removeKeyHandlers();
    }
  };
 
  componentWillUnmount = () => {
    this.removeHandlers();
  };
 
  isVisible = () => {
    const { direction, isOpen, anchorNode, isDynamic } = this.props;
    if (!isOpen) return;
    if (!isDynamic) return this.setPlacement();
 
    const element = ReactDOM.findDOMNode(this.container);
    const elementAnchor = ReactDOM.findDOMNode(anchorNode);
    const side = direction.split('-')[0];
    const alignment = direction.split('-')[1];
    const anchor = elementAnchor && elementAnchor.getBoundingClientRect();
    const elementBoundingRect = element.getBoundingClientRect();
    const elementParent = element.parentElement;
 
    ['top', 'bottom'].includes(side)
      ? this.setVerticalClass(alignment, anchor, elementBoundingRect, elementParent)
      : this.setHorizontalClass(alignment, anchor, elementBoundingRect, elementParent);
  }
 
  findParents = (ele, tempParentArr = []) => {
    return !ele.parentElement
      ? tempParentArr
      : this.findParents(ele.parentElement, tempParentArr.concat(ele));
  }
 
  findOverflow = (node, searchProps) => {
    return searchProps.reduce((agg, prop) => {
      let overflowElement = window.getComputedStyle(ReactDOM.findDOMNode(node))[prop];
 
      return !overflowElement || agg.includes(overflowElement)
        ? agg
        : (agg += overflowElement);
    }, '');
  };
 
  findScrollParent = (elementParents, searchProps) => {
    let overflowElement = null;
    let idx = 0;
    
    while (!overflowElement && elementParents[idx]) {
      let currentOverflowElement = this.findOverflow(elementParents[idx], searchProps);
 
      Iif (/(auto|scroll|hidden)/.test(currentOverflowElement)) {
        return (overflowElement = elementParents[idx]);
      }
      idx++;
    }
 
    return overflowElement ? overflowElement : window;
  };
 
  setHorizontalClass = (alignment, anchor, elementBoundingRect, elementParent) => {
    const {
      showArrow,
      checkOverflow,
      targetOffset,
    } = this.props;
    
    const windowRight = window.pageYOffset + window.innerWidth;
    const elementWidth = elementBoundingRect.width;
    const anchorRight = anchor.right;
    const arrowWidth = showArrow
      ? ReactDOM.findDOMNode(this.arrow).getBoundingClientRect().width
      : 0;
    const offsetWidth = targetOffset.horizontal || 0;
    const totalWidth = anchorRight + elementWidth + arrowWidth + offsetWidth;
 
    const elementParents = this.findParents(elementParent);
    const scrollParent = this.findScrollParent(elementParents, ['overflow', 'overflow-x']);
 
    const parentRight = (checkOverflow 
      && !!scrollParent.getBoundingClientRect 
      && scrollParent.getBoundingClientRect().right) 
      || windowRight;
 
      return totalWidth < parentRight && totalWidth < windowRight
      ? this.setState({ visibleDirection: `right-${alignment}` }, () => this.setPlacement())
      : this.setState({ visibleDirection: `left-${alignment}` }, () => this.setPlacement());
  };
 
  setVerticalClass = (alignment, anchor, elementBoundingRect, elementParent) => {
    const {
      showArrow,
      checkOverflow,
      targetOffset,
    } = this.props;
 
    const windowBottom = window.pageXOffset + window.innerHeight;
    const elementHeight = elementBoundingRect.height;
    const anchorBottom = anchor.bottom;
    const arrowHeight = showArrow
      ? ReactDOM.findDOMNode(this.arrow).getBoundingClientRect().height
      : 0;
    const offsetHeight = targetOffset.vertical || 0;
    const totalHeight = anchorBottom + elementHeight + arrowHeight + offsetHeight;
 
    const elementParents = this.findParents(elementParent);
    const scrollParent = this.findScrollParent(elementParents, ['overflow', 'overflow-y']);
 
    const parentBottom =(checkOverflow 
      && !!scrollParent.getBoundingClientRect 
      && scrollParent.getBoundingClientRect().bottom) 
      || windowBottom;
 
    return totalHeight < parentBottom && totalHeight < windowBottom
      ? this.setState({ visibleDirection: `bottom-${alignment}` }, () => this.setPlacement())
      : this.setState({ visibleDirection: `top-${alignment}` }, () => this.setPlacement());
  };
 
  addHandlers = () => {
    const { allowClickAway, closeOnClick } = this.props;
    this.handleResize = this.isVisible;
    this.handleScroll = this.isVisible;
 
    if(allowClickAway) {
      document.addEventListener('click', this.handleAllowClickAway, true);
    }
 
    closeOnClick && document.addEventListener('click', this.handleCloseOnClick, false);
    window.addEventListener('resize', this.handleResize, true);
    document.addEventListener('scroll', this.handleScroll, false);
 
    this.isVisible();
  };
 
  addKeyHandlers = () => {
    const { allowClickAway, checkOverflow } = this.props;
 
    if(allowClickAway) {
      document.addEventListener('keydown', this.handleKeyDown, false);
    }
 
    Iif(checkOverflow) {
      const element = ReactDOM.findDOMNode(this.container);
      const elementParent = element.parentElement;
      const elementParents = this.findParents(elementParent);
      const scrollParent = this.findScrollParent(elementParents, ['overflow', 'overflow-y', 'overflow-x']);
 
      scrollParent && scrollParent.addEventListener('scroll', this.handleScroll, false);
    }
  }
 
  removeKeyHandlers = () => {
    document.removeEventListener('keydown', this.handleKeyDown, false);
  }
 
  removeHandlers = () => {
    document.removeEventListener('click', this.handleAllowClickAway, true);
    document.removeEventListener('click', this.handleCloseOnClick, false);
 
    window.removeEventListener('resize', this.handleResize, true);
    document.removeEventListener('scroll', this.handleScroll, false);
  };
 
  handleCloseOnClick = e => {
    if (!this.props.isOpen) return;
    const { closeOnClick } = this.props;
    return (
        closeOnClick
        && this.container
        && ReactDOM.findDOMNode(this.container).contains(e.target)
        && this.handleClickAway(e)
    );
  };
 
  handleKeyDown = e => {
    if (!this.props.isOpen) return;
    if (e.keyCode === 27) return this.handleClickAway(e);
 
    const anchorNode = ReactDOM.findDOMNode(this.props.anchorNode);
 
    return (
      this.container
        && !anchorNode.contains(document.activeElement)
        && !ReactDOM.findDOMNode(this.container).contains(document.activeElement)
        && this.handleClickAway(e)
    );
  };
 
  handleAllowClickAway = e => {
    if (!this.props.isOpen) return;
    const anchorNode = ReactDOM.findDOMNode(this.props.anchorNode);
 
    return (
      this.container
        && !ReactDOM.findDOMNode(anchorNode).contains(e.target)
        && !ReactDOM.findDOMNode(this.container).contains(e.target)
        && this.handleClickAway(e)
    );
  };
 
  getAnchorPosition = node => {
    const rect = node.getBoundingClientRect();
 
    const anchorPosition = {
      top: rect.top,
      left: rect.left,
      width: node.offsetWidth,
      height: node.offsetHeight
    };
 
    anchorPosition.right =
      rect.right || anchorPosition.left + anchorPosition.width;
    anchorPosition.bottom =
      rect.bottom || anchorPosition.top + anchorPosition.height;
    anchorPosition.middle =
      anchorPosition.left + (anchorPosition.right - anchorPosition.left) / 2;
    anchorPosition.center =
      anchorPosition.top + (anchorPosition.bottom - anchorPosition.top) / 2;
 
    return anchorPosition;
  };
 
  getTargetPosition = targetNode => {
    return {
      top: 0,
      center: targetNode.offsetHeight / 2,
      bottom: targetNode.offsetHeight,
      left: 0,
      middle: targetNode.offsetWidth / 2,
      right: targetNode.offsetWidth
    };
  };
 
  getOrigin = () => {
    const side = this.state.visibleDirection.split('-')[0];
    const alignment = this.props.direction.split('-')[1];
    const origin = {
      anchor: {},
      target: {}
    };
 
    if (side === 'top' || side === 'bottom') {
      origin.anchor.vertical = side;
      origin.anchor.horizontal = alignment === 'center' ? 'middle' : alignment;
 
      origin.target.vertical = side === 'top' ? 'bottom' : 'top';
      origin.target.horizontal = alignment === 'center' ? 'middle' : alignment;
    }
 
    if (side === 'left' || side === 'right') {
      origin.anchor.vertical = alignment;
      origin.anchor.horizontal = side;
 
      origin.target.vertical = alignment;
      origin.target.horizontal = side === 'left' ? 'right' : 'left';
    }
 
    return origin;
  };
 
  setArrowPlacement = (anchor, container) => {
    const arrow = this.arrow;
    const { targetOffset } = this.props;
    const { visibleDirection } = this.state;
    const side = visibleDirection.split('-')[0];
    const verticalOffset = targetOffset.vertical || 0;
    const horizontalOffset = targetOffset.horizontal || 0;
    const isAnchorWider = anchor.width > container.right;
    const isAnchorTaller = anchor.height > container.bottom;
 
    const arrowLeft = isAnchorWider && !visibleDirection.includes('center')
      ? (
        visibleDirection.includes('left')
        ? container.middle + anchor.left
        : anchor.right - container.middle
      )
      : anchor.middle;
 
    const arrowTop = isAnchorTaller && !visibleDirection.includes('center')
      ? (
        visibleDirection.includes('top')
        ? container.center + anchor.top
        : anchor.bottom - container.center
      )
      : anchor.center;
 
    switch (side) {
      case 'top':
        arrow.style.left = `${arrowLeft}px`;
        arrow.style.top = `${anchor.top - verticalOffset}px`;
        break;
      case 'bottom':
        arrow.style.left = `${arrowLeft}px`;
        arrow.style.top = `${anchor.bottom + verticalOffset}px`;
        break;
      case 'left':
        arrow.style.left = `${anchor.left - horizontalOffset}px`;
        arrow.style.top = `${arrowTop}px`;
        break;
 
      case 'right':
        arrow.style.left = `${anchor.right + horizontalOffset}px`;
        arrow.style.top = `${arrowTop}px`;
        break;
    }
  };
 
  setBoundingBox = (side, targetNode, anchorPosition) => {
    const { checkOverflow, maxHeight, maxWidth, showArrow, targetOffset } = this.props;
 
    const arrowDims = showArrow && ReactDOM.findDOMNode(this.arrow).getBoundingClientRect();
    const element = ReactDOM.findDOMNode(this.container);
    const elementParent = element.parentElement;
    const documentScrollTop = document.documentElement.scrollTop;
    const documentBottom = document.documentElement.scrollHeight;
    const windowBottom = window.pageXOffset + window.innerHeight;
    const documentRight = document.documentElement.offsetWidth;
    const arrowHeight = arrowDims && arrowDims.height || 0;
    const arrowWidth = arrowDims && arrowDims.width || 0;
    const offsetHeight = targetOffset.vertical || 0;
    const offsetWidth = targetOffset.horizontal || 0;
 
    const elementParents = this.findParents(elementParent);
    const scrollParent = this.findScrollParent(elementParents, ['overflow', 'overflow-x']);
    const elementDims = element.getBoundingClientRect();
    const elementVerticalHeight = elementDims.height + offsetHeight;
    const elementVerticalWidth = elementDims.width + offsetWidth;
    const getAvailableTopSpace = top => (top + anchorPosition.top) - (this.elementHeight + arrowHeight);
 
    const parentDims = checkOverflow 
      && !!scrollParent.getBoundingClientRect 
      && scrollParent.getBoundingClientRect();
 
    Iif(targetNode && targetNode.style && !targetNode.style.bottom && elementVerticalHeight) {
      this.elementHeight = elementVerticalHeight;
    }
 
    Iif(targetNode && targetNode.style && !targetNode.style.right && elementVerticalWidth) {
      this.elementWidth = elementVerticalWidth;
      this.elementLeft = elementDims.left;
      this.elementRight = elementDims.right;
    }
 
    switch(side) {
      case 'top':
        if(!parentDims.top) {
          if(getAvailableTopSpace(documentScrollTop) < 0) {
            targetNode.style.top = `${arrowHeight - documentScrollTop}px`;
          }
          if(arrowHeight + this.elementHeight > anchorPosition.top) {
            targetNode.style.bottom = `${(windowBottom - anchorPosition.top + arrowHeight + offsetHeight)}px`;
          }
          if(this.elementWidth > documentRight) {
            targetNode.style.right = '0px';
          }
          if(this.elementLeft < 0) {
            targetNode.style.left = '0px';
          }
        } else {
          if((anchorPosition.top - parentDims.top - this.elementHeight - arrowHeight) < 0) {
            targetNode.style.top = `${parentDims.top + arrowHeight}px`;
            targetNode.style.bottom = `${(windowBottom - anchorPosition.top + arrowHeight + offsetHeight)}px`;
            targetNode.style.maxHeight = `${maxHeight || parentDims.height}px`;
          }
          if(this.elementWidth > parentDims.width || this.elementRight > parentDims.right) {
            targetNode.style.right = `${documentRight - parentDims.right}px`;
          }
          if(this.elementLeft < parentDims.left) {
            targetNode.style.left = `${parentDims.left}px`;
          }
          if(arrowDims && (arrowDims.top < parentDims.top || arrowDims.bottom + 1 > parentDims.bottom)) {
            this.arrow.style.visibility = 'hidden';
          } else if(arrowDims) {
            this.arrow.style.visibility = 'visible';
          }
        }
        break;
      case 'bottom':
        if(!parentDims.bottom) {
          if(this.elementHeight + arrowHeight + anchorPosition.bottom + documentScrollTop > documentBottom) {
            targetNode.style.bottom = `${documentScrollTop + windowBottom - documentBottom}px`;
          }
          if(this.elementWidth > documentRight) {
            targetNode.style.right = '0px';
          }
          if(elementDims.left < 0) {
            targetNode.style.left = '0px';
          }
        } else {
          if(this.elementHeight + arrowHeight + anchorPosition.bottom > parentDims.bottom) {
            targetNode.style.bottom = `${windowBottom - parentDims.bottom}px`;
            targetNode.style.maxHeight = `${maxHeight || parentDims.height}px`;
          }
          if(this.elementWidth > parentDims.width || this.elementRight > parentDims.right) {
            targetNode.style.right = `${documentRight - parentDims.right}px`;
          }
          if(this.elementLeft < parentDims.left) {
            targetNode.style.left = `${parentDims.left}px`;
          }
          if(arrowDims && (arrowDims.top < parentDims.top || arrowDims.bottom + 1 > parentDims.bottom)) {
            this.arrow.style.visibility = 'hidden';
          } else if(arrowDims) {
            this.arrow.style.visibility = 'visible';
          }
        }
        break;
      case 'left':
        if(!parentDims.left) {
          if(arrowWidth + offsetWidth + elementDims.width + anchorPosition.left > anchorPosition.left) {
            targetNode.style.left = `${arrowWidth}px`;
            targetNode.style.right = `${documentRight - anchorPosition.left + arrowWidth + offsetWidth}px`;
          }
          if(getAvailableTopSpace(documentScrollTop) < 0) {
            targetNode.style.top = `${-documentScrollTop}px`;
          }
          if(this.elementHeight + arrowHeight + anchorPosition.bottom + documentScrollTop > documentBottom) {
            targetNode.style.bottom = `${documentScrollTop + windowBottom - documentBottom}px`;
          }
        } else {
          if((anchorPosition.left - parentDims.left) < (this.elementWidth + arrowWidth)) {
            targetNode.style.left = `${parentDims.left + arrowWidth}px`;
            targetNode.style.right = `${(documentRight - anchorPosition.left + arrowWidth + offsetWidth)}px`;
            targetNode.style.maxWidth = `${maxWidth || parentDims.width}px`;
          }
          if((anchorPosition.top - parentDims.top - this.elementHeight) < 0) {
            targetNode.style.top = `${parentDims.top}px`;
          }
          if(this.elementHeight + anchorPosition.bottom > parentDims.bottom) {
            targetNode.style.bottom = `${windowBottom - parentDims.bottom}px`;
          }
          if(arrowDims && (arrowDims.top < parentDims.top || arrowDims.bottom > parentDims.bottom)) {
            this.arrow.style.visibility = 'hidden';
          } else if(arrowDims) {
            this.arrow.style.visibility = 'visible';
          }
        }
        break;
      case 'right':
        Eif(!parentDims.right) {
          Iif(arrowWidth + offsetWidth + elementDims.width + anchorPosition.right > documentRight) {
            targetNode.style.right = '0px';
          }
          Iif(getAvailableTopSpace(documentScrollTop) < 0) {
            targetNode.style.top = `${-documentScrollTop}px`;
          }
          Iif(this.elementHeight + arrowHeight + anchorPosition.bottom + documentScrollTop > documentBottom) {
            targetNode.style.bottom = `${documentScrollTop + windowBottom - documentBottom}px`;
          }
        } else {
          if((anchorPosition.right + this.elementWidth + arrowWidth) > parentDims.right) {
            targetNode.style.left = `${anchorPosition.right + offsetWidth}px`;
            targetNode.style.right = `${(documentRight - parentDims.right)}px`;
            targetNode.style.maxWidth = `${maxWidth || parentDims.width}px`;
          }
          if((anchorPosition.top - parentDims.top - this.elementHeight) < 0) {
            targetNode.style.top = `${parentDims.top}px`;
          }
          if(this.elementHeight + anchorPosition.bottom > parentDims.bottom) {
            targetNode.style.bottom = `${windowBottom - parentDims.bottom}px`;
          }
          if(arrowDims && (arrowDims.top < parentDims.top || arrowDims.bottom > parentDims.bottom)) {
            this.arrow.style.visibility = 'hidden';
          } else if(arrowDims) {
            this.arrow.style.visibility = 'visible';
          }
        }
        break;
    }
  }
 
  setPlacement = () => {
    const {
      anchorNode,
      isOpen,
      isContained,
      showArrow,
      targetOffset
    } = this.props;
    const { visibleDirection } = this.state;
    Iif (!isOpen) return;
 
    const anchorNodeFound = ReactDOM.findDOMNode(anchorNode);
    const side = visibleDirection.split('-')[0];
    const targetNode = this.container;
    const verticalOffset = targetOffset.vertical || 0;
    const horizontalOffset = targetOffset.horizontal || 0;
 
    if (!targetNode || !anchorNodeFound) return;
 
    anchorNodeFound.link = this.state.id;
    const anchorPosition = this.getAnchorPosition(anchorNodeFound);
    const targetPosition = this.getTargetPosition(targetNode);
 
    const origin = this.getOrigin();
 
    const anchorOrigin = origin.anchor;
    const targetOrigin = origin.target;
 
    const targetNodePosition = {
      top:
        anchorPosition[anchorOrigin.vertical] -
        targetPosition[targetOrigin.vertical] +
        (side === 'top' ? -verticalOffset : verticalOffset),
      left:
        anchorPosition[anchorOrigin.horizontal] -
        targetPosition[targetOrigin.horizontal] +
        (side === 'left' ? -horizontalOffset : horizontalOffset)
    };
 
    targetNode.style.top = `${targetNodePosition.top}px`;
    targetNode.style.left = `${targetNodePosition.left}px`;
 
    showArrow && this.setArrowPlacement(anchorPosition, targetPosition);
    
    isContained && this.setBoundingBox(side, targetNode, anchorPosition);
  };
 
  handleClickAway = e => {
    this.props.close && this.props.close(e);
  };
 
  render() {
    const {
      children,
      className,
      isOpen,
      maxHeight,
      maxWidth,
      showArrow,
      style,
      ...props
    } = this.props;
    const side = this.state.visibleDirection.split('-')[0];
 
    const otherProps = omit({...props}, [
      'allowClickAway',
      'anchorNode',
      'checkOverflow',
      'close',
      'closeOnClick',
      'direction',
      'isDynamic',
      'isContained',
      'targetOffset',
    ]);
 
    const contentNodes = (
      <div
         className={
          'cui-event-overlay' +
          `${(showArrow && ` cui-event-overlay--arrow`) || ''}` +
          `${(side && ` cui-event-overlay--${side}`) || ''}` +
          `${(className && ` ${className}`) || ''}`
        }
      >
        {showArrow && (
          <div
            ref={ref => this.arrow = ref}
            className='cui-event-overlay__arrow'
          />
        )}
        <div
          className='cui-event-overlay__children'
          ref={ref => this.container = ref}
          style={{
            ...maxWidth && { maxWidth: `${maxWidth}px` },
            ...maxHeight && { maxHeight: `${maxHeight}px` },
            ...style
          }}
          {...otherProps}
        >
          {children}
        </div>
      </div>
    );
 
    return isOpen && contentNodes;
  }
}
 
 
EventOverlay.defaultProps = {
  allowClickAway: true,
  anchorNode: null,
  children: null,
  checkOverflow: false,
  className: '',
  close: null,
  direction: 'bottom-left',
  isContained: false,
  isDynamic: false,
  isOpen: false,
  maxHeight: null,
  maxWidth: null,
  showArrow: false,
  style: null,
  targetOffset: {
    horizontal: 0,
    vertical: 0
  }
};
 
EventOverlay.propTypes = {
  /** @prop Allows use to click outside of EventOverlay | true */
  allowClickAway: PropTypes.bool,
  /** @prop Node in which the selection begins | null */
  anchorNode: PropTypes.object,
  /** @prop Set to Check if children nodes are overflowing the EventOverlay | false */
  checkOverflow: PropTypes.bool,
  /** @prop Children nodes to render inside the EventOverlay | null */
  children: PropTypes.node,
  /** @prop Optional css class string | '' */
  className: PropTypes.string,
  /** @prop Function to close EventOverlay | null */
  close: PropTypes.func,
  /** @prop Determines if the EventOverlay should close when clicked on | true */
  closeOnClick: PropTypes.bool,
  /** @prop Sets the direction in which the EventOverlay extends | 'bottom-left' */
  direction: PropTypes.oneOf([
    'top-center',
    'left-center',
    'right-center',
    'bottom-center',
    'top-left',
    'top-right',
    'bottom-left',
    'bottom-right',
    'left-top',
    'left-bottom',
    'right-top',
    'right-bottom'
  ]),
  /** @prop Determines if the contents of EventOverlay is contained within Component | false */
  isContained: PropTypes.bool,
  /** @prop Sets the EventOverlay to be a dynamic UI component | false */
  isDynamic: PropTypes.bool,
  /** @prop Sets the visibility of the EventOverlay | false */
  isOpen: PropTypes.bool,
  /** @prop Sets the max height of the EventOverlay | null */
  maxHeight: PropTypes.number,
  /** @prop Sets the max width of the EventOverlay | null */
  maxWidth: PropTypes.number,
  /** @prop Determines if the EventOverlay should show the open/close arrow | false */
  showArrow: PropTypes.bool,
  /** @prop Optional css styling | null */
  style: PropTypes.object,
  /** @prop Sets the target offset | { horizontal: 0, vertical: 0 } */
  targetOffset: PropTypes.shape({
    horizontal: PropTypes.number,
    vertical: PropTypes.number
  }),
};