11import React , { useEffect , useState } from 'react' ;
22import { getParameters } from 'codesandbox-import-utils/lib/api/define' ;
33
4- export type CodeSandboxProps = React . FormHTMLAttributes < HTMLFormElement > & {
4+ export type CodeSandboxBase = {
55 /**
66 * Whether we should redirect to the embed instead of the editor.
77 */
@@ -23,9 +23,11 @@ export type CodeSandboxProps = React.FormHTMLAttributes<HTMLFormElement> & {
2323 content ?: string | Record < string , any > ;
2424 isBinary ?: boolean ;
2525 } >
26- } ;
26+ }
27+
28+ export type CodeSandboxProps < T > = React . FormHTMLAttributes < T > & CodeSandboxBase ;
2729
28- function request ( files : CodeSandboxProps [ 'files' ] ) {
30+ function request < T > ( files : CodeSandboxProps < T > [ 'files' ] ) {
2931 return fetch ( 'https://codesandbox.io/api/v1/sandboxes/define?json=1' , {
3032 method : "POST" ,
3133 headers : {
@@ -35,11 +37,10 @@ function request(files: CodeSandboxProps['files']) {
3537 body : JSON . stringify ( {
3638 files : files ,
3739 } )
38- } ) .
39- then ( x => x . json ( ) )
40+ } ) . then ( x => x . json ( ) ) ;
4041}
4142
42- const codeSandbox : React . FC < CodeSandboxProps > = ( props ) => {
43+ const CodeSandbox : React . FC < CodeSandboxProps < HTMLFormElement | HTMLIFrameElement > > = ( props ) => {
4344 const { files = { } , embed, json, query, ...other } = props || { } ;
4445 const parameters = getParameters ( { files } as any ) ;
4546 const [ url , setUrl ] = useState < string > ( ) ;
@@ -52,16 +53,20 @@ const codeSandbox: React.FC<CodeSandboxProps> = (props) => {
5253 }
5354 } ) ;
5455 }
56+ // eslint-disable-next-line react-hooks/exhaustive-deps
5557 } , [ files ] ) ;
5658 if ( ! props . children ) {
5759 return (
5860 < iframe
61+ { ...other }
62+ title = { other . title || 'Example.' }
5963 src = { url }
6064 style = { {
6165 width : '100%' ,
6266 height : '100%' ,
6367 border : 0 ,
64- overflow : 'hidden'
68+ overflow : 'hidden' ,
69+ ...other . style ,
6570 } }
6671 allow = "accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
6772 sandbox = "allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
@@ -77,6 +82,7 @@ const codeSandbox: React.FC<CodeSandboxProps> = (props) => {
7782 < button type = "submit" > { props . children } </ button >
7883 </ form >
7984 )
85+
8086}
8187
82- export default codeSandbox ;
88+ export default CodeSandbox ;
0 commit comments