diff --git a/components/ui/dialog.tsx b/components/ui/dialog.tsx index b2f1b98..78b84e3 100644 --- a/components/ui/dialog.tsx +++ b/components/ui/dialog.tsx @@ -10,7 +10,26 @@ const Dialog = DialogPrimitive.Root; const DialogTrigger = DialogPrimitive.Trigger; -const DialogPortal = DialogPrimitive.Portal; +// Only render the portal on the client. During SSR/static prerendering the +// `document` global doesn't exist, and React Dom's createPortal(children, +// document.body) inside Radix's Portal would throw "document is not defined". +const DialogPortal = ({ + children, + ...props +}: React.ComponentPropsWithoutRef) => { + const [mounted, setMounted] = React.useState(false); + + React.useEffect(() => { + setMounted(true); + }, []); + + if (!mounted) { + return null; + } + + return {children}; +}; +DialogPortal.displayName = DialogPrimitive.Portal.displayName; const DialogClose = DialogPrimitive.Close;