All files / components/Modal Modal.jsx

27.85% Statements 39/140
100% Branches 0/0
0% Functions 0/2
27.85% Lines 39/140

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 1411x 1x 1x 1x 1x 1x 1x                                                                                                                                                                                                           1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
import React, { useRef, useState, useEffect } from 'react';
import ReactModal from 'react-modal';
import PropTypes from 'prop-types';
 
import { CloseXIcon2 } from '../../lib/SvgComponents.jsx';
 
const Modal = ({
  children,
  onRequestClose,
  title,
  primary,
  secondary,
  appElement,
  isOpen,
  ...props
}) => {
  const [overflowSeparator, setOverflowSeparator] = useState(false);
  const closeButtonRef = useRef(null);
  const contentRef = useRef(null);

  useEffect(() => {
    ReactModal.setAppElement(appElement);
  }, []);

  return (
    <ReactModal
      onRequestClose={onRequestClose}
      portalClassName="qpp-c-modal"
      className="qpp-c-modal__content"
      overlayClassName="qpp-c-modal__overlay"
      bodyOpenClassName="qpp-c-modal__body-open"
      isOpen={isOpen}
      onAfterOpen={() => {
        // Add heading separator based on overflow
        if (
          contentRef.current?.scrollHeight >
          document.documentElement.clientHeight * 0.6
        ) {
          setOverflowSeparator(true);
        } else {
          setOverflowSeparator(false);
        }

        // Set default focus
        if (closeButtonRef?.current) {
          closeButtonRef.current.focus();
        }
      }}
      aria={{
        labelledby: 'modal-heading',
        describedby: 'modal-body',
      }}
      {...props}
    >
      <div
        className={`qpp-c-modal__header ${
          overflowSeparator ? 'qpp-c-modal__header-sep' : ''
        }`}
      >
        <h3 id="modal-heading">{title}</h3>
        <button
          className="closex qpp-c-button qpp-c-button--text qpp-u-color--black"
          aria-label="close modal"
          data-testid="modal-close-btn"
          onClick={onRequestClose}
          ref={closeButtonRef}
        >
          <CloseXIcon2 />
        </button>
      </div>
      <div
        id="modal-body"
        className="qpp-c-modal__body"
        ref={contentRef}
        tabIndex={overflowSeparator ? '0' : undefined}
      >
        {children}
      </div>
      {(primary || secondary) && (
        <div className="qpp-c-modal__footer">
          {secondary && (
            <button
              className={`qpp-c-button qpp-c-button--secondary ${
                primary ? 'qpp-u-margin-right--16' : ''
              }`}
              loading={secondary.loading}
              onClick={secondary.onClick || onRequestClose}
              aria-label={secondary.title}
              data-testid="modal-secondary-btn"
            >
              {secondary.title}
            </button>
          )}
          {primary && (
            <button
              className="qpp-c-button"
              loading={primary.loading}
              onClick={primary.onClick}
              data-testid="modal-primary-btn"
              aria-label={primary.title}
            >
              {primary.title}
            </button>
          )}
        </div>
      )}
    </ReactModal>
  );
};
 
Modal.propTypes = {
  title: PropTypes.string,
  children: PropTypes.node.isRequired,
  primary: PropTypes.shape({
    title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
    onClick: PropTypes.func,
    loading: PropTypes.bool,
    loadingText: PropTypes.string,
  }),
  secondary: PropTypes.shape({
    title: PropTypes.oneOfType([PropTypes.string, PropTypes.element]),
    onClick: PropTypes.func,
    loading: PropTypes.bool,
    loadingText: PropTypes.string,
  }),
  onRequestClose: PropTypes.func,
  appElement: PropTypes.string,
  isOpen: PropTypes.bool,
};
 
Modal.defaultProps = {
  onRequestClose: () => {},
  title: undefined,
  primary: null,
  secondary: null,
  appElement: 'body > *:not(.qpp-c-modal)',
  isOpen: false,
};
 
export default Modal;