Skip to content

Commit

Permalink
Update components/AlertInbox/Popup.tsx (#306)
Browse files Browse the repository at this point in the history
* Update components/AlertInbox/Popup.tsx

Added the ability to close the popup by clicking
off of it.

* Updated Popup.tsx

Removed console.log that created a lint
error

* Fixing the Lint-error

It was a tab-spacing on a comment
  • Loading branch information
lawtlee authored Oct 13, 2023
1 parent d35133c commit 7e92155
Show file tree
Hide file tree
Showing 2 changed files with 259 additions and 538 deletions.
22 changes: 20 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,6 +10,7 @@ export interface PopupProps {

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

const handleClose = () => {
setShow(false);
Expand All @@ -20,6 +21,23 @@ function Popup(props: PopupProps): JSX.Element {
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 +46,7 @@ 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 7e92155

Please sign in to comment.