Skip to content

Commit 20f1c5b

Browse files
committed
feat: Add the current page display featrue.
1 parent 9384c4e commit 20f1c5b

File tree

5 files changed

+88
-3
lines changed

5 files changed

+88
-3
lines changed

src/index.tsx

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from 'react';
1+
import React, { useEffect, useState } from 'react';
22
import { getParameters } from 'codesandbox-import-utils/lib/api/define';
33

44
export type CodeSandboxProps = React.FormHTMLAttributes<HTMLFormElement> & {
@@ -25,9 +25,49 @@ export type CodeSandboxProps = React.FormHTMLAttributes<HTMLFormElement> & {
2525
}>
2626
};
2727

28+
function request(files: CodeSandboxProps['files']) {
29+
return fetch('https://codesandbox.io/api/v1/sandboxes/define?json=1', {
30+
method: "POST",
31+
headers: {
32+
"Content-Type": "application/json",
33+
Accept: "application/json"
34+
},
35+
body: JSON.stringify({
36+
files: files,
37+
})
38+
}).
39+
then(x => x.json())
40+
}
41+
2842
const codeSandbox: React.FC<CodeSandboxProps> = (props) => {
2943
const { files = {}, embed, json, query, ...other } = props || {};
3044
const parameters = getParameters({ files } as any);
45+
const [url, setUrl] = useState<string>();
46+
useEffect(() => {
47+
if (!props.children) {
48+
request(files).then(data => {
49+
if (data && data.sandbox_id) {
50+
const cusUrl = `https://codesandbox.io/${embed ? 'embed': 's'}/${data.sandbox_id}?${query ? query : ''}`;
51+
setUrl(cusUrl);
52+
}
53+
});
54+
}
55+
}, [files]);
56+
if (!props.children) {
57+
return (
58+
<iframe
59+
src={url}
60+
style={{
61+
width: '100%',
62+
height: '100%',
63+
border: 0,
64+
overflow: 'hidden'
65+
}}
66+
allow="accelerometer; ambient-light-sensor; camera; encrypted-media; geolocation; gyroscope; hid; microphone; midi; payment; usb; vr; xr-spatial-tracking"
67+
sandbox="allow-forms allow-modals allow-popups allow-presentation allow-same-origin allow-scripts"
68+
/>
69+
)
70+
}
3171
return (
3272
<form action="https://codesandbox.io/api/v1/sandboxes/define" method="POST" target="_blank" {...other}>
3373
<input type="hidden" name="parameters" value={parameters} />

website/App.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ body {
99
}
1010

1111
.App-logo {
12-
height: 40vmin;
12+
height: 20vmin;
1313
}
1414

1515
.App-header p {

website/App.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import logo from './logo.svg';
55
import MDStr from '../README.md';
66
import Example from './Example';
77
import ExampleKKT from './ExampleKKT';
8+
import ExampleNoChild from './ExampleNoChild';
89
import './App.css';
910

1011
const App: React.FC = () => {
@@ -17,6 +18,7 @@ const App: React.FC = () => {
1718
<div>
1819
<Example />
1920
<ExampleKKT />
21+
<ExampleNoChild />
2022
</div>
2123
<span className="App-arrow"></span>
2224
</header>

website/Example.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ const code = `import React from 'react';
55
import ReactDOM from 'react-dom';
66
77
const element = (
8-
<h1> Hello, CodeSandbox! </h1>
8+
<h1> Hello, CodeSandbox! </h1>
99
);
1010
1111
ReactDOM.render(

website/ExampleNoChild.tsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
2+
import CodeSandbox from '../';
3+
4+
const code = `import React from 'react';
5+
import ReactDOM from 'react-dom';
6+
7+
const element = (
8+
<h1> Hello, CodeSandbox! </h1>
9+
);
10+
11+
ReactDOM.render(
12+
element,
13+
document.getElementById('root')
14+
);`;
15+
16+
const Example = () => {
17+
return (
18+
<div style={{ height: 400, width: 800 }}>
19+
<CodeSandbox
20+
embed
21+
query="view=split"
22+
files={{
23+
"package.json": {
24+
content: {
25+
dependencies: {
26+
react: "latest",
27+
"react-dom": "latest"
28+
}
29+
}
30+
},
31+
"index.js": {
32+
content: code
33+
},
34+
"index.html": {
35+
content: `<div id="root"></div>`
36+
}
37+
}}
38+
/>
39+
</div>
40+
)
41+
}
42+
43+
export default Example;

0 commit comments

Comments
 (0)