All files / src/components/Modal index.tsx

61.54% Statements 16/26
18.92% Branches 7/37
66.67% Functions 4/6
61.54% Lines 16/26

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    5x                               5x                           14x 14x 14x   14x         14x 9x 9x     14x 9x           9x       14x 9x         14x                 14x                                              
import React, { FC, ReactNode, useCallback, useEffect, useLayoutEffect, useRef, useState } from 'react';
import { useTranslation } from 'react-i18next';
const styles = require('./Modal.module.css')
import { SvgIcon } from '../SvgIcon';
import { scrollLock, scrollUnlock } from '../../helpers/scrollLock';
 
export const Modal: FC<{
  isOpen: boolean;
  onClose: () => void;
  header?: ReactNode;
  footer?: ReactNode;
  overlayClose?: boolean;
  size?: 'normal' | 'wide';
  classes?: string;
  containerClasses?: string;
  headerClasses?: string;
  contentClasses?: string;
  existed?: boolean;
}> = ({
  isOpen,
  onClose,
  header,
  footer,
  overlayClose = true,
  children,
  size = 'normal',
  classes,
  containerClasses,
  headerClasses,
  contentClasses,
  existed= false
}) => {
  const { t } = useTranslation();
  const ref = useRef<HTMLDivElement>(null);
  const [exist, setExist] = useState(false);
 
  const open = useCallback(() => {
    scrollLock();
    ref.current && ref.current.classList.add(styles.open);
  }, []);
 
  const close = useCallback(() => {
    scrollUnlock();
    ref.current && ref.current.classList.replace(styles.open, styles.close);
  }, []);
 
  useEffect(() => {
    Iif (isOpen) {
      setExist(isOpen);
      if (exist && existed) {
        open();
      }
    } else {
      close();
    }
  }, [isOpen, close]);
 
  useLayoutEffect(() => {
    Iif (exist) {
      open();
    }
  }, [exist, open]);
 
  const animationEndHandler = useCallback(evt => {
    if (evt.animationName === styles.hide) {
      ref.current && ref.current.classList.remove(styles.close);
      if (!existed) {
        setExist(false);
      }
    }
  }, []);
 
  return exist ? (
    <div
      className={`${styles.Modal} ${classes ?? ''} ${size === 'wide' ? styles.wide : ''}`}
      onAnimationEnd={animationEndHandler}
      ref={ref}
    >
      {overlayClose && <div className={styles.ModalOverlay} onClick={onClose} aria-hidden="true" />}
      <div className={`${styles.ModalContainer} ${containerClasses ?? ''}`}>
        <div className={`${styles.ModalHeader} ${headerClasses ?? ''}`}>
          <button type="button" className={styles.ModalClose} onClick={onClose} aria-label={t('close')}>
            <SvgIcon icon={{ name: 'times', width: 24, height: 24, fill: '#144154' }} />
          </button>
          {header}
        </div>
        <div className={`${styles.ModalContent} ${contentClasses ?? ''}`}>
          {children}
        </div>
        {footer && <div className={styles.ModalFooter}>
          {footer}
        </div>}
      </div>
    </div>
  ) : null;
};