source: imaps-frontend/src/components/Modals/Components/Modal.jsx@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 2.7 KB
RevLine 
[0c6b92a]1import React, { useRef, useState, useEffect } from "react";
2import styles from "../EntranceModal/EntranceModal.module.css";
3
4export default function Modal({ children, title, isOpen, toggleModal }) {
5 const modalRef = useRef(null);
6 const [dragging, setDragging] = useState(false);
7 const [position, setPosition] = useState({ x: "50%", y: "50%" });
8 const [offset, setOffset] = useState({ x: 0, y: 0 });
9
10 const handleMouseDown = (e) => {
11 if (!modalRef.current) return;
12 setDragging(true);
13 const rect = modalRef.current.getBoundingClientRect();
14 setOffset({
15 x: e.clientX - rect.left,
16 y: e.clientY - rect.top,
17 });
18 };
19
20 const handleMouseMove = (e) => {
21 if (!dragging) return;
22 setPosition({
23 x: e.clientX - offset.x,
24 y: e.clientY - offset.y,
25 });
26 };
27
28 const handleMouseUp = () => {
29 setDragging(false);
30 };
31
32 useEffect(() => {
33 if (dragging) {
34 window.addEventListener("mousemove", handleMouseMove);
35 window.addEventListener("mouseup", handleMouseUp);
36 } else {
37 window.removeEventListener("mousemove", handleMouseMove);
38 window.removeEventListener("mouseup", handleMouseUp);
39 }
40 return () => {
41 window.removeEventListener("mousemove", handleMouseMove);
42 window.removeEventListener("mouseup", handleMouseUp);
43 };
44 }, [dragging]);
45
46 if (isOpen) {
47 document.body.classList.add(styles.activeModal);
48 } else {
49 document.body.classList.remove(styles.activeModal);
50 }
51
52 return (
53 <>
54 {isOpen && (
55 <div className={styles.modal}>
56 <div onClick={toggleModal} className={styles.overlay}></div>
57 <div
58 ref={modalRef}
59 className={styles.modalContent}
60 style={{
61 top: typeof position.y === "string" ? position.y : `${position.y}px`,
62 left: typeof position.x === "string" ? position.x : `${position.x}px`,
63 transform:
64 typeof position.x === "string" && typeof position.y === "string"
65 ? "translate(-50%, -50%)"
66 : "none",
67 }}
68 >
69 <div
70 className={styles.draggableHeader}
71 onMouseDown={handleMouseDown}
72 >
73 <h2 className={styles.title}>{title}</h2>
74 </div>
75 <form className={styles.form}>
76 {children}
77 <button className={styles.closeModal} onClick={toggleModal}>
78 CLOSE
79 </button>
80 </form>
81 </div>
82 </div>
83 )}
84 </>
85 );
86}
Note: See TracBrowser for help on using the repository browser.