1
1
import React , { useEffect , useState } from 'react' ;
2
2
import { getParameters } from 'codesandbox-import-utils/lib/api/define' ;
3
3
4
- export type CodeSandboxProps = React . FormHTMLAttributes < HTMLFormElement > & {
4
+ export type CodeSandboxBase = {
5
5
/**
6
6
* Whether we should redirect to the embed instead of the editor.
7
7
*/
@@ -23,9 +23,11 @@ export type CodeSandboxProps = React.FormHTMLAttributes<HTMLFormElement> & {
23
23
content ?: string | Record < string , any > ;
24
24
isBinary ?: boolean ;
25
25
} >
26
- } ;
26
+ }
27
+
28
+ export type CodeSandboxProps < T > = React . FormHTMLAttributes < T > & CodeSandboxBase ;
27
29
28
- function request ( files : CodeSandboxProps [ 'files' ] ) {
30
+ function request < T > ( files : CodeSandboxProps < T > [ 'files' ] ) {
29
31
return fetch ( 'https://codesandbox.io/api/v1/sandboxes/define?json=1' , {
30
32
method : "POST" ,
31
33
headers : {
@@ -35,11 +37,10 @@ function request(files: CodeSandboxProps['files']) {
35
37
body : JSON . stringify ( {
36
38
files : files ,
37
39
} )
38
- } ) .
39
- then ( x => x . json ( ) )
40
+ } ) . then ( x => x . json ( ) ) ;
40
41
}
41
42
42
- const codeSandbox : React . FC < CodeSandboxProps > = ( props ) => {
43
+ const CodeSandbox : React . FC < CodeSandboxProps < HTMLFormElement | HTMLIFrameElement > > = ( props ) => {
43
44
const { files = { } , embed, json, query, ...other } = props || { } ;
44
45
const parameters = getParameters ( { files } as any ) ;
45
46
const [ url , setUrl ] = useState < string > ( ) ;
@@ -52,16 +53,20 @@ const codeSandbox: React.FC<CodeSandboxProps> = (props) => {
52
53
}
53
54
} ) ;
54
55
}
56
+ // eslint-disable-next-line react-hooks/exhaustive-deps
55
57
} , [ files ] ) ;
56
58
if ( ! props . children ) {
57
59
return (
58
60
< iframe
61
+ { ...other }
62
+ title = { other . title || 'Example.' }
59
63
src = { url }
60
64
style = { {
61
65
width : '100%' ,
62
66
height : '100%' ,
63
67
border : 0 ,
64
- overflow : 'hidden'
68
+ overflow : 'hidden' ,
69
+ ...other . style ,
65
70
} }
66
71
allow = "accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
67
72
sandbox = "allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
@@ -77,6 +82,7 @@ const codeSandbox: React.FC<CodeSandboxProps> = (props) => {
77
82
< button type = "submit" > { props . children } </ button >
78
83
</ form >
79
84
)
85
+
80
86
}
81
87
82
- export default codeSandbox ;
88
+ export default CodeSandbox ;
0 commit comments