Created
May 27, 2023 21:08
-
-
Save epicbytes/cd4c3c8cdb05711a9f3e8b04d9250910 to your computer and use it in GitHub Desktop.
Portals in nextJS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
export const LayoutPopup = <T extends unknown>({ | |
library, | |
}: { | |
library: Record<keyof T, ComponentType<any>>; | |
}): JSX.Element => { | |
const modalRoot = useCreateDomElement(); | |
return ( | |
<> | |
{modalRoot && | |
createPortal(<PopupContainer<T> library={library} />, modalRoot)} | |
</> | |
); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { useEffect, useState } from "react" | |
export function useCreateDomElement() { | |
const [domElement, setDomElement] = useState<HTMLDivElement | null>(null) | |
useEffect(() => { | |
const element = document.createElement("div") | |
document.body.appendChild(element) | |
setDomElement(element) | |
return () => { | |
document.body.removeChild(element) | |
} | |
}, []) | |
return domElement | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment