I have created a custom react modal component and I would like refactor to be able to track the outside clicks of modal content and to be able to close modal My code looks as it follows. And I search for a workaround to remove the create portal if click is outside of popup container,
import React from "react";
import ReactDOM from "react-dom";
const Hint = (props) => {
const ToggleContent = ({ toggle, content }) => {
const [isShown, setIsShown] = React.useState(false);
const hide = () => setIsShown(false);
const show = () => setIsShown(true);
return (
<React.Fragment>
{toggle(show)}
{isShown && content(hide)}
</React.Fragment>
);
};
const Modal = ({ children }) => {
return ReactDOM.createPortal(
<div className="modal">{children}</div>,
document.body
);
};
return (
<div className={"hint"}>
<ToggleContent
toggle={(show) => (
<div className={"hint__icon"} onClick={show}>
{props.hintLabel ? (
<div className={"hint__label-text"}>{props.hintLabel}</div>
) : null}
</div>
)}
content={(hide) => (
<div className={"modal__body"}>
<Modal>
<div className={"popup"}>
<div className={"popup__wrapper"}>
<div
className={"popup__content"}
dangerouslySetInnerHTML={{ __html: props.content }}
></div>
</div>
<button onClick={hide} type="button" className="close">
X
</button>
</div>
</Modal>
</div>
)}
/>
</div>
);
};
ReactDOM.render(
<Hint hintLabel="test" content="teasasd ajsdghajsdg" />,
document.getElementById("app")
);
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
<div id="app"></div>