Skip to content

Commit 3bc7b11

Browse files
author
Ives van Hoorne
committed
Add managerState to Provider
1 parent 88016ff commit 3bc7b11

File tree

16 files changed

+202
-112
lines changed

16 files changed

+202
-112
lines changed

packages/app/src/sandbox/compile.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ async function compile({
130130
isModuleView = false,
131131
template,
132132
entry,
133-
showOpenInCodeSandbox = true,
133+
showOpenInCodeSandbox = false,
134134
skipEval = false,
135135
}) {
136136
dispatch({

packages/react-sandpack/gulpfile.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,27 @@ const gulp = require('gulp');
22
const concat = require('gulp-concat');
33
const sass = require('gulp-sass');
44
const cssimport = require('gulp-cssimport');
5+
const prettier = require('gulp-plugin-prettier');
56

67
gulp.task('sass', () =>
78
gulp
89
.src('./src/**/*.scss')
910
.pipe(sass().on('error', sass.logError))
1011
.pipe(cssimport({ includePaths: ['../../node_modules'] }))
1112
.pipe(concat('styles.css'))
13+
.pipe(prettier.format())
1214
.pipe(gulp.dest('./dist/'))
1315
);
1416

17+
gulp.task('copy', () => {
18+
gulp
19+
.src('./src/**/*.scss')
20+
.pipe(cssimport({ includePaths: ['../../node_modules'] }))
21+
.pipe(concat('styles.scss'))
22+
.pipe(prettier.format())
23+
.pipe(gulp.dest('./dist/'));
24+
});
25+
1526
gulp.task('sass:watch', () => gulp.watch('./src/**/*.scss', ['sass']));
27+
28+
gulp.task('build', ['sass', 'copy']);

packages/react-sandpack/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
"gulp": "^3.9.1",
104104
"gulp-concat": "^2.6.1",
105105
"gulp-cssimport": "^6.0.1",
106+
"gulp-plugin-prettier": "^1.1.0",
106107
"gulp-sass": "^3.1.0",
107108
"husky": "^0.14.0",
108109
"jest": "^22.0.2",

packages/react-sandpack/src/components/BrowserPreview/BrowserPreview.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,13 @@ export interface Props {
1313

1414
export default class BrowserPreview extends React.PureComponent<Props> {
1515
render() {
16-
const { className, style } = this.props;
16+
const { className = '', style, ...props } = this.props;
1717

1818
return (
1919
<div
2020
className={`${cn('BrowserPreview', 'container')} ${className}`}
2121
style={style}
22+
{...props}
2223
>
2324
<Navigator />
2425
<Preview />

packages/react-sandpack/src/components/CodeEditor/CodeMirror/CodeMirror.tsx

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,15 @@ class CodeMirror extends React.PureComponent<Props> {
3434
};
3535

3636
render() {
37-
const { codeMirrorOptions, style = {}, className = '' } = this.props;
37+
const { codeMirrorOptions, ...props } = this.props;
3838
const { openedPath, files } = this.props.sandpack;
3939

4040
return (
4141
<CodeMirrorComponent
42-
className={className}
43-
style={style}
4442
codeMirrorOptions={codeMirrorOptions}
4543
onBeforeChange={this.onChange}
4644
value={files[openedPath].code}
45+
{...props}
4746
/>
4847
);
4948
}

packages/react-sandpack/src/components/FileExplorer/FileExplorer.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export interface Props {
1212

1313
export default class FileExplorer extends React.PureComponent<Props> {
1414
render() {
15-
const { style, className = '' } = this.props;
15+
const { className = '', ...props } = this.props;
1616
return (
1717
<SandpackConsumer>
1818
{sandpack => (
1919
<div
2020
className={`${className} ${cn('FileExplorer', 'container')}`}
21-
style={style}
21+
{...props}
2222
>
2323
<ModuleList
2424
selectFile={sandpack.openFile}

packages/react-sandpack/src/components/Navigator/Navigator.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
import * as React from 'react';
22
import { listen, dispatch } from 'codesandbox-api';
3-
import RefreshIcon from './RefreshIcon';
3+
import classNames from 'classnames';
44

5+
import RefreshIcon from './RefreshIcon';
56
import withSandpack from '../../utils/with-sandpack';
67
import { ISandpackContext } from '../../types';
78
import cn from '../../utils/cn';
89

910
interface Props {
1011
sandpack: ISandpackContext;
12+
className?: string;
1113
}
1214

1315
interface State {
@@ -98,9 +100,13 @@ class Navigator extends React.Component<Props, State> {
98100

99101
render() {
100102
const { browserPath } = this.state;
103+
const { sandpack, className, ...props } = this.props;
101104

102105
return (
103-
<div className={cn('Navigator', 'container')}>
106+
<div
107+
className={classNames(className, cn('Navigator', 'container'))}
108+
{...props}
109+
>
104110
<button className={cn('Navigator', 'button')} onClick={this.onRefresh}>
105111
<RefreshIcon />
106112
</button>
Lines changed: 23 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,16 @@
11
import * as React from 'react';
22
import { getParameters } from 'codesandbox-import-utils/lib/api/define';
3-
import { IFileProps } from '../../types';
3+
import { IFiles } from '../../types';
44

5-
export default class OpenInCodeSandbox extends React.Component<IFileProps> {
6-
static defaultProps = {
7-
entry: '/index.js',
8-
};
9-
10-
getFileParameters = () => {
11-
const { files, dependencies } = this.props;
12-
const paramFiles = { ...files };
13-
14-
const packageJSON = {
15-
main: this.props.entry,
16-
dependencies,
17-
};
18-
paramFiles['/package.json'] = {
19-
code: JSON.stringify(packageJSON, null, 2),
20-
};
5+
import SandpackConsumer from '../SandpackConsumer';
216

22-
const normalized = Object.keys(paramFiles).reduce(
7+
export default class OpenInCodeSandbox extends React.Component {
8+
getFileParameters = (files: IFiles) => {
9+
const normalized = Object.keys(files).reduce(
2310
(prev, next) => ({
2411
...prev,
2512
[next.replace('/', '')]: {
26-
content: paramFiles[next].code,
13+
content: files[next].code,
2714
isBinary: false,
2815
},
2916
}),
@@ -34,21 +21,24 @@ export default class OpenInCodeSandbox extends React.Component<IFileProps> {
3421
};
3522

3623
render() {
37-
const { files, dependencies, ...props } = this.props;
3824
return (
39-
<form
40-
action="https://codesandbox.io/api/v1/sandboxes/define"
41-
method="POST"
42-
target="_blank"
43-
{...props}
44-
>
45-
<input
46-
type="hidden"
47-
name="parameters"
48-
value={this.getFileParameters()}
49-
/>
50-
<input type="submit" value="Open in CodeSandbox" />
51-
</form>
25+
<SandpackConsumer>
26+
{sandpack => (
27+
<form
28+
action="https://codesandbox.io/api/v1/sandboxes/define"
29+
method="POST"
30+
target="_blank"
31+
{...this.props}
32+
>
33+
<input
34+
type="hidden"
35+
name="parameters"
36+
value={this.getFileParameters(sandpack.files)}
37+
/>
38+
<input type="submit" value="Open in CodeSandbox" />
39+
</form>
40+
)}
41+
</SandpackConsumer>
5242
);
5343
}
5444
}
Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
import * as React from 'react';
2+
import { ISandpackContext } from '../../types';
23
import SandpackConsumer from '../SandpackConsumer';
4+
import withSandpack from '../../utils/with-sandpack';
35

46
export interface PreviewProps {
5-
browser: HTMLIFrameElement;
7+
sandpack: ISandpackContext;
68
}
79

8-
class Preview extends React.PureComponent<PreviewProps> {
10+
class Preview extends React.Component<PreviewProps> {
911
container?: HTMLDivElement;
1012

1113
setContainerElement = (el: HTMLDivElement) => {
1214
this.container = el;
1315
};
1416

1517
initializeFrame = () => {
16-
const { browser } = this.props;
18+
const { browserFrame } = this.props.sandpack;
1719

18-
if (browser && this.container) {
19-
browser.style.width = '100%';
20-
browser.style.height = '500px';
21-
browser.style.visibility = 'visible';
22-
browser.style.position = 'relative';
20+
if (browserFrame && this.container) {
21+
browserFrame.style.width = '100%';
22+
browserFrame.style.height = '500px';
23+
browserFrame.style.visibility = 'visible';
24+
browserFrame.style.position = 'relative';
2325

24-
this.container.appendChild(browser);
26+
this.container.appendChild(browserFrame);
2527
}
2628
};
2729

2830
componentDidUpdate(prevProps: PreviewProps) {
29-
if (prevProps.browser !== this.props.browser) {
31+
if (prevProps.sandpack.browserFrame !== this.props.sandpack.browserFrame) {
3032
this.initializeFrame();
3133
}
3234
}
@@ -36,12 +38,4 @@ class Preview extends React.PureComponent<PreviewProps> {
3638
}
3739
}
3840

39-
export default class ContainerElement extends React.PureComponent {
40-
render() {
41-
return (
42-
<SandpackConsumer>
43-
{state => <Preview browser={state.browserFrame} />}
44-
</SandpackConsumer>
45-
);
46-
}
47-
}
41+
export default withSandpack(Preview);

packages/react-sandpack/src/components/SandpackProvider/SandpackProvider.tsx

Lines changed: 38 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import * as React from 'react';
22
import { Broadcast } from 'react-broadcast';
33
import { Manager } from 'sandpack';
4+
import { listen } from 'codesandbox-api';
45

5-
import { IFile, IFiles } from '../../types';
6+
import { IFile, IFiles, IManagerState } from '../../types';
67

78
export interface State {
89
files: IFiles;
910
browserPath: string;
1011
openedPath: string;
12+
iframe: HTMLIFrameElement | null;
13+
managerState: IManagerState | undefined;
1114
}
1215

1316
export interface Props {
@@ -34,6 +37,7 @@ export default class SandpackProvider extends React.PureComponent<
3437

3538
manager?: Manager;
3639
iframe?: HTMLIFrameElement;
40+
listener: Function;
3741

3842
constructor(props: Props) {
3943
super(props);
@@ -46,9 +50,19 @@ export default class SandpackProvider extends React.PureComponent<
4650
),
4751
browserPath: props.initialPath || '/',
4852
openedPath: props.entry || '/index.js',
53+
iframe: null,
54+
managerState: undefined,
4955
};
56+
57+
this.listener = listen(this.handleMessage);
5058
}
5159

60+
handleMessage = (message: any) => {
61+
if (message.type === 'success') {
62+
this.setState({ managerState: message.state });
63+
}
64+
};
65+
5266
createMissingPackageJSON(
5367
files: IFiles,
5468
dependencies?: {
@@ -88,11 +102,21 @@ export default class SandpackProvider extends React.PureComponent<
88102
}
89103

90104
setupFrame = (el: HTMLIFrameElement) => {
91-
this.manager = new Manager(el, this.props.files, {
92-
skipEval: this.props.skipEval,
93-
});
105+
this.manager = new Manager(
106+
el,
107+
this.createMissingPackageJSON(
108+
this.props.files,
109+
this.props.dependencies,
110+
this.props.entry
111+
),
112+
{
113+
skipEval: this.props.skipEval,
114+
}
115+
);
94116

95117
this.iframe = el;
118+
119+
this.setState({ iframe: el });
96120
};
97121

98122
updateFiles = (files: IFiles) => {
@@ -119,28 +143,35 @@ export default class SandpackProvider extends React.PureComponent<
119143
}
120144
}
121145

146+
componentWillUnmount() {
147+
this.listener();
148+
}
149+
122150
openFile = (path: string) => {
123151
this.setState({ openedPath: path });
124152
};
125153

126154
render() {
127155
const { children } = this.props;
128-
const { files, browserPath, openedPath } = this.state;
156+
const { iframe, files, browserPath, openedPath, managerState } = this.state;
129157

130158
return (
131159
<Broadcast
132160
channel="sandpack"
133161
value={{
134162
files,
135163
openedPath,
164+
managerState,
136165
openFile: this.openFile,
137-
browserFrame: this.iframe,
166+
browserFrame: iframe,
138167
updateFiles: this.updateFiles,
139168
sandboxUrl: this.props.sandboxUrl,
140169
}}
141170
>
142171
<div className="sandpack">
143-
{/* We create a hidden iframe, the bundler will live in this. */}
172+
{/* We create a hidden iframe, the bundler will live in this.
173+
We expose this iframe to the Consumer, so other components can show the full
174+
iframe for preview. An implementation can be found in `Preview` component. */}
144175
<iframe
145176
ref={this.setupFrame}
146177
style={{

0 commit comments

Comments
 (0)