-
Notifications
You must be signed in to change notification settings - Fork 455
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
571df87
commit c5027aa
Showing
3 changed files
with
69 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import React, { useRef, useEffect } from "react"; | ||
|
||
interface ModalProps { | ||
isOpen: boolean; | ||
onClose: () => void; | ||
children: React.ReactNode; | ||
} | ||
|
||
const Modal: React.FC<ModalProps> = ({ isOpen, onClose, children }) => { | ||
const modalRef = useRef<HTMLDivElement>(null); | ||
|
||
const handleOffClick = (event: MouseEvent) => { | ||
if (modalRef.current && !modalRef.current.contains(event.target as Node)) { | ||
onClose(); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
document.addEventListener("mousedown", handleOffClick); | ||
return () => { | ||
document.removeEventListener("mousedown", handleOffClick); | ||
}; | ||
}, [onClose]); | ||
|
||
if (!isOpen) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<div className="fixed inset-0 z-50 flex items-center justify-center bg-black bg-opacity-50"> | ||
<div | ||
ref={modalRef} | ||
className="bg-gray-800 rounded-lg shadow-xl w-full max-w-lg p-2" | ||
> | ||
<div className="flex justify-end"> | ||
<button | ||
onClick={onClose} | ||
className="text-gray-600 hover:text-gray-800 cursor-pointer" | ||
> | ||
<span className="text-3xl">×</span> | ||
</button> | ||
</div> | ||
{children} | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Modal; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters