Sha256: daec3d00300405dd18121b8822bb4425a53f54fb46b778bfaf63a88aaf3633c1

Contents?: true

Size: 1.14 KB

Versions: 4

Compression:

Stored size: 1.14 KB

Contents

import React, { MouseEvent, useEffect } from "react";

import useModalStore from "../stores/useModalStore";

export default function Modal() {
  const component = useModalStore((state) => state.component);
  const close = useModalStore((state) => state.close);

  const handleClose = (evt: KeyboardEvent | MouseEvent) => {
    evt.stopPropagation();
    evt.preventDefault();
    close();
  };

  const handleKeypress = (evt: KeyboardEvent) => {
    if (component && (evt.key == "Escape" || evt.keyCode === 27)) {
      handleClose(evt);
    }
  };

  useEffect(() => {
    if (component) {
      document.body.classList.add("modal");
    } else {
      document.body.classList.remove("modal");
    }
  }, [component]);

  useEffect(() => {
    window.addEventListener("keypress", handleKeypress);
    return () => {
      window.removeEventListener("keypress", handleKeypress);
    };
  }, []);

  if (component) {
    return (
      <div className="modal-wrapper open">
        <div className="background" onClick={handleClose} />
        <div className="modal">{component}</div>
      </div>
    );
  } else {
    return <div className="modal-wrapper"></div>;
  }
}

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
pages_core-3.15.4 app/javascript/components/Modal.tsx
pages_core-3.15.3 app/javascript/components/Modal.tsx
pages_core-3.15.2 app/javascript/components/Modal.tsx
pages_core-3.15.1 app/javascript/components/Modal.tsx