All files / Rindu/components/ModalContainer ModalContainer.tsx

71.42% Statements 25/35
50% Branches 10/20
75% Functions 6/8
73.52% Lines 25/34

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 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                19x   19x   19x   19x 19x                                               1x 1x 1x                                                                                                                                                                                                                                                         1x       2x   2x 1x     1x     1x 1x     1x     2x 2x   2x                   2x       2x 2x           2x 1x     1x    
import {
  PropsWithChildren,
  ReactElement,
  ReactPortal,
  useEffect,
  useLayoutEffect,
  useRef,
  useState,
} from "react";
 
import { createPortal } from "react-dom";
 
import { Heading } from "components";
import { IModalContext } from "context/ModalContext";
import { useFocusTrap, useTranslations } from "hooks";
import { AsType } from "types/heading";
 
interface IModalProps {
  title: string;
  setModalData: IModalContext["setModalData"];
  minWidth?: string;
  maxWidth?: string;
  minHeight?: string;
  maxHeight?: string;
  modalRootId?: string;
  handleClose?: () => void;
}
 
function Modal({
  title,
  minWidth,
  maxWidth,
  maxHeight,
  minHeight,
  setModalData,
  children,
}: PropsWithChildren<
  Omit<IModalProps, "modalRootId" | "handleClose">
>): ReactElement {
  const { translations } = useTranslations();
  const modalContainerRef = useRef<HTMLDivElement | null>(null);
  useFocusTrap(modalContainerRef);
 
  return (
    <div className="modal-container">
      <main
        className="modal"
        role="dialog"
        aria-modal="true"
        aria-labelledby="globalModalTitle"
        aria-live="polite"
        aria-describedby="globalModalDescription"
        tabIndex={-1}
        ref={modalContainerRef}
      >
        <header>
          <Heading number={3} as={AsType.H1} id="globalModalTitle">
            {title || "Modal"}
          </Heading>
          <button
            type="button"
            tabIndex={0}
            onClick={(e) => {
              e.preventDefault();
              e.stopPropagation();
              setModalData(null);
            }}
            className="exitButton"
            title={translations.removeTracksModal.close}
          >
            <svg
              role="img"
              height="16"
              width="16"
              aria-hidden="true"
              aria-label="Cerrar"
              viewBox="0 0 16 16"
            >
              <path d="M1.47 1.47a.75.75 0 0 1 1.06 0L8 6.94l5.47-5.47a.75.75 0 1 1 1.06 1.06L9.06 8l5.47 5.47a.75.75 0 1 1-1.06 1.06L8 9.06l-5.47 5.47a.75.75 0 0 1-1.06-1.06L6.94 8 1.47 2.53a.75.75 0 0 1 0-1.06z"></path>
            </svg>
          </button>
        </header>
        {children}
      </main>
      <style jsx>{`
        .modal {
          min-width: ${minWidth ?? "300px"};
          max-width: ${maxWidth ?? "664px"};
          min-height: ${minHeight ?? "300px"};
          max-height: ${maxHeight ?? "80vh"};
        }
      `}</style>
      <style jsx>{`
        .modal-container {
          position: fixed;
          z-index: 9999999999;
          top: 50%;
          left: 50%;
          display: flex;
          align-items: center;
          justify-content: center;
          transform: translate(-50%, -50%);
          width: max-content;
        }
        .modal-container main:focus {
          outline: none;
        }
        :global(.overlay) {
          align-items: center;
          bottom: 0;
          display: flex;
          justify-content: center;
          left: 0;
          overflow: hidden;
          position: absolute;
          right: 0;
          top: 0;
          background-color: rgba(0, 0, 0, 0.7);
          z-index: 999999999999999999;
        }
        .modal {
          background-color: #181818;
          border-radius: 8px;
          box-shadow: 0 4px 4px rgba(0, 0, 0, 0.3);
          color: #fff;
          z-index: 100;
          padding: 32px 32px 24px;
        }
        header {
          display: flex;
          justify-content: space-between;
          margin-bottom: 1rem;
        }
        button {
          display: flex;
          align-items: center;
          align-self: end;
          background-color: transparent;
          border-radius: 32px;
          border: 0;
          color: hsla(0, 0%, 100%, 0.7);
          grid-area: close-button;
          height: 32px;
          justify-content: center;
          margin-inline-end: -8px;
          margin-top: -8px;
          width: 32px;
        }
        svg {
          fill: currentColor;
        }
        button:focus {
          outline: none;
        }
        button:hover,
        button:focus {
          background-color: hsla(0, 0%, 100%, 0.1);
        }
        button:active {
          background-color: hsla(0, 0%, 100%, 0.2);
        }
      `}</style>
    </div>
  );
}
 
export default function ModalContainer({
  handleClose,
  ...props
}: PropsWithChildren<IModalProps>): ReactPortal | null {
  const [targetNode, setTargetNode] = useState<Element>();
 
  useEffect(() => {
    const targetSelector = props.modalRootId
      ? `#${props.modalRootId}`
      : "#globalModal";
    setTargetNode(document.querySelector(targetSelector) as Element);
 
    function cleanup() {
      setTargetNode(undefined);
      Iif (handleClose) handleClose();
    }
 
    return cleanup;
  }, [props.modalRootId, handleClose]);
 
  useLayoutEffect(() => {
    const elementToBlur = document.querySelector<HTMLElement>(".container");
 
    const handleClickOutside = (event: MouseEvent) => {
      Iif (targetNode && !targetNode.contains(event.target as Node)) {
        Iif (handleClose) {
          handleClose();
        }
 
        props.setModalData(null);
      }
    };
 
    Iif (elementToBlur) {
      elementToBlur.addEventListener("mousedown", handleClickOutside);
    }
 
    return () => {
      Iif (elementToBlur) {
        elementToBlur.removeEventListener("mousedown", handleClickOutside);
      }
    };
  }, [props, targetNode, handleClose]);
 
  if (targetNode === undefined) {
    return null;
  }
 
  return createPortal(<Modal {...props} />, targetNode);
}