close popup when click outside

This commit is contained in:
Guillaume Dorce 2022-09-28 16:27:26 +02:00
parent 2f1f4d7ffd
commit e93a2417b7
1 changed files with 12 additions and 1 deletions

View File

@ -1,9 +1,20 @@
import { useState } from 'react'; import { useEffect, useState } from 'react';
import { FaEllipsisH } from 'react-icons/fa'; import { FaEllipsisH } from 'react-icons/fa';
const PopupMessage = ({ id }: { id: string }) => { const PopupMessage = ({ id }: { id: string }) => {
const [show, setShow] = useState(false); const [show, setShow] = useState(false);
useEffect(() => {
const handleClick = (e: any) => {
console.log(show && e.target.closest('.popup-btn'));
if ((show && !e.target.closest('.popup-btn') || (e.target.closest('.popup') && !e.target.closest('.popup-btn')))) {
setShow(false);
}
};
document.addEventListener('click', handleClick);
}, [id]);
return ( return (
<> <>
<div className="bg-grey-dark"> <div className="bg-grey-dark">