Your idea was really creative and interesting, to put the div inside the popup. I think i did the way you want, but it's a little hacky, i had to create a DOMElement with an append child, weird solution... it would be something like this:
import React, { useEffect, useRef, useState } from "react";
const Summary = () => {
const [popupVisible, setPopupVisible] = useState<boolean>(false);
const [element, setElement] = useState() as any;
const showElementOnPopUp = (e: any) => {
if (!popupVisible) setPopupVisible(true);
const elementClone = e.currentTarget.cloneNode(true);
elementClone.querySelector("div").style.display = "block";
setElement(elementClone);
};
return (
<div>
<div className="summary-table col-md-6" onClick={showElementOnPopUp}>
Contact 1
<div style={{ display: "none" }}>
<p>Number: 111111</p>
<p>Address: House 1</p>
</div>
</div>
<div className="summary-table col-md-6" onClick={showElementOnPopUp}>
Contact 2
<div style={{ display: "none" }}>
<p>Number: 222222</p>
<p>Address: House 2</p>
</div>
</div>
<div className="summary-table col-md-6" onClick={showElementOnPopUp}>
Contact 3
<div style={{ display: "none" }}>
<p>Number: 333333</p>
<p>Address: House 3</p>
</div>
</div>
{popupVisible && (
<Popup element={element} setPopupVisible={setPopupVisible}></Popup>
)}
</div>
);
};
const Popup = (props: any) => {
const { element, setPopupVisible } = props;
return (
<>
<h1>Popup Element</h1>
<div>
<DOMElementContainer element={element} />
</div>
<button
onClick={() => {
setPopupVisible(false);
}}
>
Close Popup
</button>
</>
);
};
const DOMElementContainer = ({ element }: any) => {
const ref = useRef() as any;
useEffect(() => {
if (!element) return;
ref.current.innerHTML = "";
ref.current.appendChild(element);
}, [element]);
return <div ref={ref}></div>;
};
export default Summary;
But i think a better approach would be this one, that better uses react state, and is more real, where you would get the list of contacts from some api:
import React, { useEffect, useRef, useState } from "react";
const Summary = () => {
const [popupVisible, setPopupVisible] = useState<boolean>(false);
const [contactList, setContactList] = useState() as any;
const [contactInfo, setContactInfo] = useState() as any;
const someApiCall = () => {
return Promise.resolve([
{ name: "Contact 1", number: "11111", address: "House 1" },
{ name: "Contact 2", number: "222222", address: "House 2" },
{ name: "Contact 3", number: "333333", address: "House 3" },
]);
};
useEffect(() => {
someApiCall().then((data) => {
console.log(data);
setContactList(data);
});
}, []);
return (
<div>
{contactList?.map((contact: any) => {
return (
<div
onClick={() => {
setContactInfo(contact);
setPopupVisible(true);
}}
>
{contact.name}
</div>
);
})}
{popupVisible && (
<Popup
contactInfo={contactInfo}
setPopupVisible={setPopupVisible}
></Popup>
)}
</div>
);
};
const Popup = (props: any) => {
const { contactInfo, setPopupVisible } = props;
return (
<>
<h1>Popup Element</h1>
<div>
<p>Name: {contactInfo.name}</p>
<p>Number: {contactInfo.number}</p>
<p>Address: {contactInfo.address}</p>
</div>
<button
onClick={() => {
setPopupVisible(false);
}}
>
Close Popup
</button>
</>
);
};
export default Summary;