Created
February 19, 2024 09:39
-
-
Save 4msar/bd921473bcadfef778705c84b146bb31 to your computer and use it in GitHub Desktop.
Render React Component to Iframe
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 React, { useRef } from "react" | |
import { createPortal } from "react-dom" | |
export const IFrame = ({ | |
children, | |
title, | |
...props | |
}: HTMLAttributes<HTMLIFrameElement> & { | |
children: React.ReactNode | |
}) => { | |
const contentRef = useRef<HTMLIFrameElement>(null) | |
const mountNode = contentRef?.current?.contentWindow?.document?.body | |
return ( | |
<iframe title={title} {...props} ref={contentRef}> | |
{mountNode && createPortal(children, mountNode)} | |
</iframe> | |
) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment