Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update components/AlertInbox/Popup.tsx #306

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading