Skip to content

Commit

Permalink
Update components/AlertInbox/Popup.tsx
Browse files Browse the repository at this point in the history
Added the ability to close the popup by clicking
off of it.
  • Loading branch information
lawtlee committed Oct 13, 2023
1 parent a37b5b0 commit 287d088
Show file tree
Hide file tree
Showing 2 changed files with 262 additions and 538 deletions.
25 changes: 23 additions & 2 deletions src/components/AlertInbox/Popup.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useState, useRef } from 'react';
import Close from '../../../public/Close.png';

export interface PopupProps {
Expand All @@ -10,16 +10,35 @@ export interface PopupProps {

function Popup(props: PopupProps): JSX.Element {
const [show, setShow] = useState(false);
const wrapperRef = useRef<HTMLDivElement>(null);

const handleClose = () => {
setShow(false);
props.onClose();
};

useEffect(() => {
console.log('here');
setShow(props.show);
}, [props.show]);

// Close the popup if click outside of popup
useEffect(() => {
function handleClickOutside(event: MouseEvent) {
if (
props.show == true &&
wrapperRef.current &&
!wrapperRef.current.contains(event.target as Element)
) {
handleClose();
}
}
document.addEventListener('mousedown', handleClickOutside); // Bind the event listener
return () => {
document.removeEventListener('mousedown', handleClickOutside); // Unbind the event listener on clean up
};
}, [wrapperRef, props.show]);

return (
<div
style={{
Expand All @@ -28,7 +47,9 @@ function Popup(props: PopupProps): JSX.Element {
}}
className="overlay"
>
<div className="popup">
<div className="popup"
ref={wrapperRef}
>
<h2>{props.title}</h2>
<div className="content">{props.children}</div>
<button
Expand Down
Loading

0 comments on commit 287d088

Please sign in to comment.