diff --git a/src/Portal.tsx b/src/Portal.tsx new file mode 100644 index 00000000..25620658 --- /dev/null +++ b/src/Portal.tsx @@ -0,0 +1,23 @@ +import { useEffect } from 'react' +import { createPortal } from 'react-dom' + +const Portal = ({ children }) => { + let modalRoot = document.getElementById('modal') + + if (!modalRoot) { + modalRoot = document.createElement('div') + modalRoot.setAttribute('id', 'modal') + document.body.appendChild(modalRoot) + } + + const modalElement = document.createElement('div') + + useEffect(() => { + modalRoot.appendChild(modalElement) + return () => modalRoot.removeChild(modalElement) + }) + + return createPortal(children, modalElement) +} + +export default Portal