# All files / components/Modal Modal.js

0% Statements 0/4
0% Branches 0/4
0% Functions 0/3
0% Lines 0/4

    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

     import React from 'react';
    import PropTypes from 'prop-types';
    import styled from 'styled-components';
    import ModalActionContent from './styles/ModalActionContent';
    import ModalBlocker from './styles/ModalBlocker';
    import ModalContent from './styles/ModalContent';
    import ModalTitle from './styles/ModalTitle';
    import ModalWindow from './styles/ModalWindow';

    class Modal extends React.Component {
      static propTypes = {
        /**
         * Content to render in the modal.
         */
        children: PropTypes.node.isRequired,
        /**
         * An optional title for the top of the modal.
         */
        title: PropTypes.string,
        /**
         * Handles click events on the backdrop behind the modal. Use this to close it if desired.
         */
        onBlockerClicked: PropTypes.func,
        /**
         * Sets a natural CSS width for the modal window. Defaults to 'auto'.
         */
        naturalWidth: PropTypes.string,
        /**
         * Adds a class name to the modal window element.
         */
        className: PropTypes.string,
        /**
         * Adds an id to the modal window element.
         */
        id: PropTypes.string,
        /**
         * Content to render in a fixed position at the bottom of the modal - meant for action buttons, like close.
         */
        actionContent: PropTypes.node,
        /**
         * A component to render the action content area
         */
        ActionContent: PropTypes.func,
        /**
         * A component to render the full-screen blocker behind the modal
         */
        Blocker: PropTypes.func,
        /**
         * A component to render the content inside the modal window
         */
        Content: PropTypes.func,
        /**
         * A component to render the title at the top of the modal
         */
        Title: PropTypes.func,
        /**
         * A component to render the window container of the modal
         */
        Window: PropTypes.func,
      };

      static defaultProps = {
        title: null,
        onBlockerClicked: () => null,
        className: null,
        id: null,
        actionContent: null,
        ActionContent: ModalActionContent,
        Blocker: ModalBlocker,
        Content: ModalContent,
        Title: ModalTitle,
        Window: ModalWindow,
      };

      handleModalClicked = event => {
        // prevents click event bubbling to blocker and triggering blockerclicked callback
        event.stopPropagation();
      };

      render() {
        const {
          id,
          className,
          onBlockerClicked,
          title,
          children,
          actionContent,
          Blocker,
          Content,
          Window,
          ActionContent,
          Title,
        } = this.props;

        return (
          <Blocker onClick={onBlockerClicked}>
            <Window onClick={this.handleModalClicked} id={id} className={className}>
              {title ? <Title>{title}</Title> : null}
              <Content>{children}</Content>
              {actionContent && <ActionContent>{actionContent}</ActionContent>}
            </Window>
          </Blocker>
        );
      }
    }

    export default Modal;