프레임 (Frame)
컴포넌트를 iframe 안에서 렌더링하는 데 사용하는 유틸리티예요. 콘텐츠의 크기를 추적해서 CSS 변수로 노출하고, head prop으로 스크립트와 스타일을 주입할 수 있습니다.
출처: 문서
본문
사용법 (Usage)
Frame 컴포넌트는 컴포넌트를 iframe 안에서 렌더링하는 데 사용됩니다.
- 콘텐츠의 크기를 추적하여 CSS 변수로 노출합니다.
headprop을 지원하여 스크립트와 스타일을 주입할 수 있습니다.- 마운트와 언마운트 콜백을 지원합니다.
import { Frame } from '@ark-ui/react'
예제 (Examples)
기본 (Basic)
Frame 컴포넌트로 컴포넌트를 감싸면 iframe 안에서 렌더링됩니다.
import { Frame } from '@ark-ui/react/frame'
export const Basic = () => {
return (
<Frame
title="Custom Frame"
style={{ border: '1px solid #ccc', width: '100%', height: 'var(--height)' }}
head={<style>{'body { background-color: #f0f0f0; }'}</style>}
>
<div style={{ padding: '40px' }}>
<h1>Hello from inside the frame!</h1>
<p>This content is rendered within our custom frame component using a Portal.</p>
</div>
</Frame>
)
}
스크립트 주입 (Injecting Script)
onMount prop을 사용하면 iframe 안에 스크립트를 주입할 수 있어요.
import { Frame } from '@ark-ui/react/frame'
import { useRef } from 'react'
export const Script = () => {
const ref = useRef<HTMLIFrameElement>(null)
return (
<Frame
ref={ref}
title="Custom Frame"
onMount={() => {
const doc = ref.current?.contentDocument
if (!doc) return
const script = doc.createElement('script')
script.innerHTML = 'console.log("Hello from inside the frame!")'
doc.body.appendChild(script)
}}
style={{ border: '1px solid #ccc', width: '100%', height: 'var(--height)' }}
>
<div style={{ padding: '40px' }}>
<h1>Hello from inside the frame!</h1>
<p>This content is rendered within our custom frame component using a Portal.</p>
</div>
</Frame>
)
}
커스텀 src doc (Custom src doc)
srcDoc prop을 사용하면 iframe에서 사용할 페이지의 HTML 콘텐츠를 지정할 수 있어요.
import { Frame } from '@ark-ui/react/frame'
const srcDoc = `<html><head>
<link href="//use.fontawesome.com/releases/v5.15.1/css/all.css" rel="stylesheet" />
<link href="//fonts.googleapis.com/css?family=Open+Sans:400,300,600,700" rel="stylesheet" type="text/css" />
<base target=_blank>
</head><body style='overflow: hidden'><div></div></body></html>`
export const SrcDoc = () => {
return (
<Frame title="Custom Frame" style={{ border: '1px solid #ccc', maxWidth: '800px', width: '100%' }} srcDoc={srcDoc}>
<h1 style={{ fontFamily: 'Open Sans, sans-serif' }}>Hello from inside the frame!</h1>
<p>This content is rendered within our custom frame component using a Portal.</p>
<p>The frame has custom initial content, including Font Awesome and Open Sans font.</p>
</Frame>
)
}
API 레퍼런스 (API Reference)
Props
Frame
Props
head
Type: string | number | bigint | boolean | ReactElement<unknown, string | JSXElementConstructor<any>> | Iterable<ReactNode> | ReactPortal | Promise<...>
Required: false
Default Value: undefined
Description: 프레임의 <head>에 삽입할 추가 콘텐츠
onMount
Type: () => void
Required: false
Default Value: undefined
Description: 프레임이 마운트될 때 실행되는 콜백 함수
onUnmount
Type: () => void
Required: false
Default Value: undefined
Description: 프레임이 언마운트될 때 실행되는 콜백 함수